Performance is interrupted when memory is exhausted. If you keep waiting, most likely you will encounter a memory error. Creating a gear keeps the collection head in close. Thus, a huge lazy sequence links memory as it is realized.
That's what happens distilled
user=> (def n 100000000)
Now the same thing, but from within the closure
user=> (defn foo [x] (fn [f] (fx)))
Compare with
(time (do (doall (range n)) nil)) OutOfMemoryError GC overhead limit exceeded ... (sometime later)
Suspicious circuit in gearboxes
user=> (source r/folder) (defn folder "Given a foldable collection, [...]" {:added "1.5"} ([coll xf] (reify clojure.core.protocols/CollReduce (coll-reduce [_ f1] (clojure.core.protocols/coll-reduce coll (xf f1) (f1))) ...
Christophe Grand has a good post on how to make gears in a lazy fashion.
source share