Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by

When calling seq on an eduction when the source of eduction is a chunked-seq, the returned seq won't use the "chunked functions" to traverse the collection.

2 Answers

0 votes
_Comment made by: petterik_

I've experimented with making eduction use chunked-seq optimizations and it's about twice as fast. The same code works with clojure.core/sequence and I see the same performance speed up there as well.

I don't know how we feel about external links but here's a gist with:
* Code
* Tests
* Output from running (-main)
* One-line commands to test it out with clj and cljs.main


The code for sequence looks like this:

(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))
                           (self v next-chunk))
                           (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))))
0 votes
Reference: https://clojure.atlassian.net/browse/CLJ-2356 (reported by petterik)