由gshayban发表的评论
重现代码
(def C (atom 0))
;;重新扩展以进行工具化
(extend-protocol clojure.core.protocols/IKVReduce
;;慢速路径默认
clojure.lang.IPersistentMap
(kv-reduce
[amap f init]
(swap! C inc)
(reduce (fn [ret [k v]] (f ret k v)) init amap)))
(reduce-kv (fn [_ _ _]) nil (array-map 1 2 3 4))
(println "slowpaths" @C)
(reduce-kv (fn [_ _ _]) nil (hash-map 1 2 3 4))
(println "slowpaths" @C)
重现输出
➜ dev /usr/lib/jvm/java-8-openjdk/bin/java -cp $(clojure -Spath) clojure.main kvfastpath.clj
slowpaths 1
slowpaths 2
➜ dev /usr/lib/jvm/java-11-openjdk/bin/java -cp $(clojure -Spath) clojure.main kvfastpath.clj
slowpaths 0
slowpaths 0