Reducer (and folder) instances hold onto the head of seqs


  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Completed
  • Affects Version/s: Release 1.5
  • Fix Version/s: Release 1.8
  • Component/s: None
  • Patch:
    Code and Test
  • Approval:


Problem Statement
A shared function #'clojure.core.reducers/reducer holds on to the head of a reducible collection, causing it to blow up when the collection is a lazy sequence.

Reproduction steps:
Compare the following calls:

(time (reduce + 0 (map identity (range 1e8))))
(time (reduce + 0 (r/map identity (range 1e8))))

The second call should fail on a normal or small heap.

(If reducers are faster than seqs, increase the range.)

Cause: #'reducer closes over a collection when in order to reify CollReduce, and the closed-over is never cleared. When code attempts to reduce over this anonymous transformed collection, it will realize the tail while the head is stored in the closed-over.

Patch: clj-1250-2.patch


Clear the reference to 'this' on the stack just before a tail call occurs

Removes calls to emitClearLocals(), which is a no-op.

When the context is RETURN (indicating a tail call), and the operation
is an InvokeExpr, StaticMethodExpr, or InstanceMethodExpr, clear the
reference to 'this' which is in slot 0 of the locals.

Edge-case: Inside the body of a try block, we cannot clear 'this' at the tail
position as we might need to keep refs around for use in the catch or finally
clauses. Introduces another truthy dynamic binding var to track position being
inside a try block.

Adds two helpers to emitClearThis and inTailCall.

Advantages: Fixes this case with no user code changes. Enables GC to do reclaim closed-overs references earlier.
Disadvantages: A compiler change.

Screened by: Alex Miller

Alternate Approaches:

1) Reimplement the #'reducer (and #'folder) transformation fns similar to the manner that Christophe proposes here:

(defrecord Reducer [coll xf])

      (coll-reduce [r f1]
                   (clojure.core.protocols/coll-reduce r f1 (f1)))
      (coll-reduce [r f1 init]
                   (clojure.core.protocols/coll-reduce (:coll r) ((:xf r) f1) init)))

(def rreducer ->Reducer) 

(defn rmap [f coll]
  (rreducer coll (fn [g] 
                   (fn [acc x]
                     (g acc (f x))))))

Advantages: Relatively non-invasive change.
Disadvantages: Not evident code. Additional protocol dispatch, though only incurred once

2) Alternate approach

from Christophe Grand:
Another way would be to enhance the local clearing mechanism to also clear "this" but it's complex since it may be needed to keep a reference to "this" for caches long after obvious references to "this" are needed.

Advantages: Fine-grained
Disadvantages: Complex, invasive, and the compiler is hard to hack on.

Avoid reducing on lazy-seqs and instead operate on vectors / maps, or custom reifiers of CollReduce or CollFold. This could be easier with some implementations of common collection functions being available (like iterate and partition).

See for previous discussion.

  1. after-change.txt
    11/Dec/13 11:17 PM
    4 kB
    Ghadi Shayban
  2. before-change.txt
    11/Dec/13 11:17 PM
    4 kB
    Ghadi Shayban
  3. clj1250.patch
    22/Jun/15 9:40 PM
    16 kB
    Ghadi Shayban
  4. CLJ-1250-08-29.patch
    29/Aug/14 3:06 PM
    15 kB
    Ghadi Shayban
  5. CLJ-1250-08-29-ws.patch
    26/Sep/14 12:51 PM
    14 kB
    Ghadi Shayban
  6. clj-1250-2.patch
    23/Jun/15 7:37 AM
    14 kB
    Alex Miller
  7. CLJ-1250-20131211.patch
    11/Dec/13 11:08 PM
    4 kB
    Ghadi Shayban
  8. CLJ-1250-AllInvokeSites-20140204.patch
    04/Feb/14 3:50 PM
    14 kB
    Ghadi Shayban
  9. CLJ-1250-AllInvokeSites-20140320.patch
    20/Mar/14 4:39 PM
    15 kB
    Ghadi Shayban
  10. do-not-clear-this-when-direct-linked.patch
    04/Aug/15 5:18 PM
    2 kB
    Ghadi Shayban



Vote (10)
Watch (14)


  • Created: