Skip to content

Commit 64f126f

Browse files
thomasmulvaneyswannodette
authored andcommitted
CLJS-1876: Faster reduce for PV, Subvec and ChunkedSeq
1 parent 028fd47 commit 64f126f

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5016,6 +5016,22 @@ reduces them without incurring seq initialization"
50165016
(unchecked-array-for v i))
50175017
v start end)))
50185018

5019+
(defn- pv-reduce
5020+
([pv f start end]
5021+
(if (< start end)
5022+
(pv-reduce pv f (nth pv start) (inc start) end)
5023+
(f)))
5024+
([pv f init start end]
5025+
(loop [acc init i start arr (unchecked-array-for pv start)]
5026+
(if (< i end)
5027+
(let [j (bit-and i 0x01f)
5028+
arr (if (zero? j) (unchecked-array-for pv i) arr)
5029+
nacc (f acc (aget arr j))]
5030+
(if (reduced? nacc)
5031+
@nacc
5032+
(recur nacc (inc i) arr)))
5033+
acc))))
5034+
50195035
(declare tv-editable-root tv-editable-tail TransientVector deref
50205036
pr-sequential-writer pr-writer chunked-seq)
50215037

@@ -5162,7 +5178,7 @@ reduces them without incurring seq initialization"
51625178

51635179
IReduce
51645180
(-reduce [v f]
5165-
(ci-reduce v f))
5181+
(pv-reduce v f 0 cnt))
51665182
(-reduce [v f init]
51675183
(loop [i 0 init init]
51685184
(if (< i cnt)
@@ -5334,10 +5350,10 @@ reduces them without incurring seq initialization"
53345350

53355351
IReduce
53365352
(-reduce [coll f]
5337-
(ci-reduce (subvec vec (+ i off) (count vec)) f))
5353+
(pv-reduce vec f (+ i off) (count vec)))
53385354

53395355
(-reduce [coll f start]
5340-
(ci-reduce (subvec vec (+ i off) (count vec)) f start)))
5356+
(pv-reduce vec f start (+ i off) (count vec))))
53415357

53425358
(es6-iterable ChunkedSeq)
53435359

@@ -5447,9 +5463,9 @@ reduces them without incurring seq initialization"
54475463

54485464
IReduce
54495465
(-reduce [coll f]
5450-
(ci-reduce coll f))
5451-
(-reduce [coll f start]
5452-
(ci-reduce coll f start))
5466+
(pv-reduce v f start end))
5467+
(-reduce [coll f init]
5468+
(pv-reduce v f init start end))
54535469

54545470
IKVReduce
54555471
(-kv-reduce [coll f init]

0 commit comments

Comments
 (0)