Skip to content

Commit 094d7f9

Browse files
roman01laswannodette
authored andcommitted
CLJS-3456: Improve -equiv performance for PersistentVector <> ChunkedSeq
1 parent e46a486 commit 094d7f9

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5677,6 +5677,37 @@ reduces them without incurring seq initialization"
56775677
(defprotocol APersistentVector
56785678
"Marker protocol")
56795679

5680+
(defn- ^boolean pv-arr-range-eq
5681+
[a off-a b off-b len]
5682+
(loop [j 0]
5683+
(if (< j len)
5684+
(let [x (aget a (+ off-a j))
5685+
y (aget b (+ off-b j))]
5686+
(if (= x y)
5687+
(recur (unchecked-inc j))
5688+
false))
5689+
true)))
5690+
5691+
(defn- ^boolean pv-range-eq*
5692+
[a ia b ib cnt-a]
5693+
(loop [ra cnt-a
5694+
ia ia
5695+
ib ib]
5696+
(if (zero? ra)
5697+
true
5698+
(let [arr-a (unchecked-array-for a ia)
5699+
arr-b (unchecked-array-for b ib)
5700+
off-a (bit-and ia 0x1f)
5701+
off-b (bit-and ib 0x1f)
5702+
rem-a (- 32 off-a)
5703+
rem-b (- 32 off-b)
5704+
m (min rem-a rem-b)
5705+
len (min ra m)]
5706+
(if (or (identical? arr-a arr-b)
5707+
(pv-arr-range-eq arr-a off-a arr-b off-b len))
5708+
(recur (- ra len) (+ ia len) (+ ib len))
5709+
false)))))
5710+
56805711
(deftype PersistentVector [meta cnt shift root tail ^:mutable __hash]
56815712
Object
56825713
(toString [coll]
@@ -5747,7 +5778,8 @@ reduces them without incurring seq initialization"
57475778
ISequential
57485779
IEquiv
57495780
(-equiv [coll other]
5750-
(if (instance? PersistentVector other)
5781+
(cond
5782+
(instance? PersistentVector other)
57515783
(if (== cnt (count other))
57525784
(let [me-iter (-iterator coll)
57535785
you-iter (-iterator other)]
@@ -5760,6 +5792,17 @@ reduces them without incurring seq initialization"
57605792
false))
57615793
true)))
57625794
false)
5795+
5796+
(and (instance? ChunkedSeq other)
5797+
(instance? PersistentVector (.-vec other)))
5798+
(let [v (.-vec other)
5799+
iother (+ (.-i other) (.-off other))
5800+
ra (- (.-cnt v) iother)]
5801+
(if (== cnt ra)
5802+
(pv-range-eq* coll 0 v iother cnt)
5803+
false))
5804+
5805+
:else
57635806
(equiv-sequential coll other)))
57645807

57655808
IHash
@@ -5960,7 +6003,10 @@ reduces them without incurring seq initialization"
59606003

59616004
ISequential
59626005
IEquiv
5963-
(-equiv [coll other] (equiv-sequential coll other))
6006+
(-equiv [coll other]
6007+
(if (instance? PersistentVector other)
6008+
(-equiv other coll)
6009+
(equiv-sequential coll other)))
59646010

59656011
ASeq
59666012
ISeq

src/test/cljs/cljs/primitives_test.cljs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@
807807
(is (= "cafrogbd" (let [jumble (fn [a b] (str (apply str (reverse (str a))) b))]
808808
(reduce jumble "frog" "abcd"))))
809809
(is (= [3] (nthnext [1 2 3] 2)))
810+
(is (= (nthnext [1 2 3] 2) [3]))
810811
(assert (not= 1 2))
811812
(is (not (not= 1 1)))
812813
(is (not (not-empty [])))

0 commit comments

Comments
 (0)