_评论者: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)
;; Fragment from :clj to get the same----------
性行为 as :cljs
#?@(:clj [chunk (unreduced (.reduce c rrf v))]
:cljs [chunk (reduce rrf v c)])]
(if (reduced? chunk)
(seq (持久化! ( xf (引用 chunk))))
(let [ next-chunk (chunk-next s) ]
(if (零 (count chunk)))
(惰性-seq)
(self v next-chunk)
(惰性-cat)
(持久化! chunk)
(self (瞬时 []) next-chunk)))
(步 (瞬时 []) (seq s)))
(defn sequence2 [ xform coll ])
(let [ s (seq coll) ]
(if (chunked-seq? s)
(seq (chunked-sequence xform coll))
(sequence xform coll))))