From db5cbfd68849f9633f8c3d6c20143ba6a3cc81c6 Mon Sep 17 00:00:00 2001 From: Paula Gearon Date: Tue, 14 Oct 2025 08:14:44 -0400 Subject: [PATCH 1/2] CLJS-3454: New set instances are created when redundant data is added --- src/main/cljs/cljs/core.cljs | 20 ++++++++++++++++---- src/test/cljs/cljs/core_test.cljs | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/main/cljs/cljs/core.cljs b/src/main/cljs/cljs/core.cljs index 27464f4f0..1e96c24f6 100644 --- a/src/main/cljs/cljs/core.cljs +++ b/src/main/cljs/cljs/core.cljs @@ -9304,7 +9304,10 @@ reduces them without incurring seq initialization" ICollection (-conj [coll o] - (PersistentHashSet. meta (assoc hash-map o nil) nil)) + (let [m (-assoc hash-map o nil)] + (if (identical? m hash-map) + coll + (PersistentHashSet. meta m nil)))) IEmptyableCollection (-empty [coll] (-with-meta (.-EMPTY PersistentHashSet) meta)) @@ -9341,7 +9344,10 @@ reduces them without incurring seq initialization" ISet (-disjoin [coll v] - (PersistentHashSet. meta (-dissoc hash-map v) nil)) + (let [m (-dissoc hash-map v)] + (if (identical? m hash-map) + coll + (PersistentHashSet. meta m nil)))) IFn (-invoke [coll k] @@ -9459,7 +9465,10 @@ reduces them without incurring seq initialization" ICollection (-conj [coll o] - (PersistentTreeSet. meta (assoc tree-map o nil) nil)) + (let [m (-assoc tree-map o nil)] + (if (identical? m tree-map) + coll + (PersistentTreeSet. meta m nil)))) IEmptyableCollection (-empty [coll] (PersistentTreeSet. meta (-empty tree-map) 0)) @@ -9513,7 +9522,10 @@ reduces them without incurring seq initialization" ISet (-disjoin [coll v] - (PersistentTreeSet. meta (dissoc tree-map v) nil)) + (let [m (-dissoc tree-map v)] + (if (identical? m tree-map) + coll + (PersistentTreeSet. meta m nil)))) IFn (-invoke [coll k] diff --git a/src/test/cljs/cljs/core_test.cljs b/src/test/cljs/cljs/core_test.cljs index d92fb5105..9ce1a9976 100644 --- a/src/test/cljs/cljs/core_test.cljs +++ b/src/test/cljs/cljs/core_test.cljs @@ -737,6 +737,27 @@ (is (= #{1 2} (hash-set 1 2 2))) (is (= #{1 2} (apply hash-set [1 2 2])))) +(deftest test-ordered-set + (is (= #{1 2} (sorted-set 1 2 2))) + (is (= [1 2 3] (seq (sorted-set 2 3 1)))) + (is (= #{1 2} (apply sorted-set [1 2 2])))) + +(deftest test-3454-conj + (is (= #{1 2 3} (conj #{1 2} 3))) + (is (= #{1 2 3} (conj (sorted-set 1 2) 3))) + (let [s #{1 2} + ss (sorted-set 1 2)] + (is (identical? s (conj s 2))) + (is (identical? ss (conj ss 2))))) + +(deftest test-3454-disj + (is (= #{1 2} (disj #{1 2 3} 3))) + (is (= #{1 2} (disj (sorted-set 1 2 3) 3))) + (let [s #{1 2} + ss (sorted-set 1 2)] + (is (identical? s (disj s 3))) + (is (identical? ss (disj ss 3))))) + (deftest test-585 (is (= (last (map identity (into [] (range 32)))) 31)) (is (= (into #{} (range 32)) From 4eff03dd809120578326573e5de325dd67cc4885 Mon Sep 17 00:00:00 2001 From: davidnolen Date: Sun, 19 Oct 2025 11:41:10 -0400 Subject: [PATCH 2/2] add missing simple Set disjoin identical? check --- src/main/cljs/cljs/core.cljs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/cljs/cljs/core.cljs b/src/main/cljs/cljs/core.cljs index d889a7ca9..82bd82c6b 100644 --- a/src/main/cljs/cljs/core.cljs +++ b/src/main/cljs/cljs/core.cljs @@ -13014,7 +13014,10 @@ reduces them without incurring seq initialization" ISet (-disjoin [coll v] - (Set. meta (-dissoc hash-map v) nil)) + (let [new-hash-map (-dissoc hash-map v)] + (if (identical? new-hash-map hash-map) + coll + (Set. meta new-hash-map nil)))) IEditableCollection (-as-transient [coll]