_评论者:petterik_
我在使 eduction 使用 chunked-seq 优化方面进行了实验,并且它大约快了两倍。同样的代码与 clojure.core/sequence 一起使用,我在那里也看到了相同的性能提升。
关于外部链接我们有何看法我不知道,这里有一个包含以下内容的 gist:
* 代码
* 测试
* 运行 (-main) 的输出
* 使用 clj 和 cljs.main 测试的一行命令
https://gist.github.com/petterik/0e0c9bcc4b223c01bbb1442e820ea503
sequence 代码如下
(defn- chunked-sequence [xform s]
(let [xf (xform conj!)
rrf #?(:clj (#'clojure.core/preserving-reduced xf)
:cljs (#'cljs.core/preserving-reduced xf))
step (fn self [v s]
(if-not s
(persistent! (xf v))
(let [c (chunk-first s)
;; Calling unreduced for :clj to get the same
;; behaviour as :cljs
#?@(:clj [chunk (unreduced (.reduce c rrf v))]
:cljs [chunk (reduce rrf v c)])]
(if (reduced? chunk)
(seq (persistent! (xf (deref chunk))))
(let [[next-chunk (chunk-next s)]
(if (zero? (count chunk))
(lazy-seq
(self v next-chunk))
(lazy-cat
(persistent! chunk)
(self (transient []) next-chunk))))))))
(step (transient []) (seq s))))
(defn sequence2 [xform coll]
(let [s (seq coll)]
(if (chunked-seq? s)
(seq (chunked-sequence xform coll))
(sequence xform coll))))