From 7277bbfd535e87e02f72e5dfc7c25cc7391c2928 Mon Sep 17 00:00:00 2001 From: Brandon Bloom Date: Sun, 24 Jun 2012 17:45:30 -0700 Subject: [PATCH 1/3] defn hash-set --- src/cljs/cljs/core.cljs | 15 ++++++++++----- test/cljs/cljs/core_test.cljs | 3 +++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/cljs/cljs/core.cljs b/src/cljs/cljs/core.cljs index d7ede71..2b13ed6 100644 --- a/src/cljs/cljs/core.cljs +++ b/src/cljs/cljs/core.cljs @@ -5696,14 +5696,19 @@ reduces them without incurring seq initialization" (set! cljs.core.PersistentTreeSet/EMPTY (PersistentTreeSet. nil (sorted-map) 0)) +(defn hash-set + ([] cljs.core.PersistentHashSet/EMPTY) + ([& keys] + (loop [in (seq keys) + out (transient cljs.core.PersistentHashSet/EMPTY)] + (if (seq in) + (recur (next in) (conj! out (first in))) + (persistent! out))))) + (defn set "Returns a set of the distinct elements of coll." [coll] - (loop [in (seq coll) - out (transient cljs.core.PersistentHashSet/EMPTY)] - (if (seq in) - (recur (next in) (conj! out (first in))) - (persistent! out)))) + (apply hash-set coll)) (defn sorted-set "Returns a new sorted set with supplied keys." diff --git a/test/cljs/cljs/core_test.cljs b/test/cljs/cljs/core_test.cljs index 8f7668a..a80f8a7 100644 --- a/test/cljs/cljs/core_test.cljs +++ b/test/cljs/cljs/core_test.cljs @@ -488,8 +488,11 @@ (assert (set [])) (assert (= #{} (set []))) + (assert (= #{} (hash-set))) + (assert (identical? cljs.core.PersistentHashSet (type (hash-set)))) (assert (= #{"foo"} (set ["foo"]))) + (assert (= #{"foo"} (hash-set "foo"))) (assert (= #{1 2 3} #{1 3 2})) (assert (= #{#{1 2 3} [4 5 6] {7 8} 9 10} #{10 9 [4 5 6] {7 8} #{1 2 3}})) -- 1.7.9.1 From 9ca568139dceeac5f0ec9c8d91e499855da70878 Mon Sep 17 00:00:00 2001 From: Brandon Bloom Date: Sun, 24 Jun 2012 17:51:06 -0700 Subject: [PATCH 2/3] Add set construction benchmarks --- benchmark/cljs/benchmark_runner.cljs | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-) diff --git a/benchmark/cljs/benchmark_runner.cljs b/benchmark/cljs/benchmark_runner.cljs index 410b262..c7a4668 100644 --- a/benchmark/cljs/benchmark_runner.cljs +++ b/benchmark/cljs/benchmark_runner.cljs @@ -109,6 +109,12 @@ (simple-benchmark [coll cljs.core.PersistentHashMap/EMPTY] (assoc coll :f0 1) 1000000) (println) +(println ";;; set ops") +(simple-benchmark [] #{} 100000) +(simple-benchmark [] #{1 2 3} 100000) +(simple-benchmark [coll #{1 2 3}] (conj coll 4) 100000) +(println) + (println ";;; seq ops") (simple-benchmark [coll (range 500000)] (reduce + coll) 1) (println) @@ -135,4 +141,4 @@ (simple-benchmark [r r] (last r) 1) (println) -(println "\n") \ No newline at end of file +(println "\n") -- 1.7.9.1 From 4e35410ee99f58b9637d1e87d04f175fd9dc7102 Mon Sep 17 00:00:00 2001 From: Brandon Bloom Date: Sun, 24 Jun 2012 17:56:26 -0700 Subject: [PATCH 3/3] Faster set construction --- src/clj/cljs/compiler.clj | 6 ++++-- src/cljs/cljs/core.cljs | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/clj/cljs/compiler.clj b/src/clj/cljs/compiler.clj index dac3ca3..b33a8d2 100644 --- a/src/clj/cljs/compiler.clj +++ b/src/clj/cljs/compiler.clj @@ -249,8 +249,10 @@ (defmethod emit :set [{:keys [items env]}] (emit-wrap env - (emits "cljs.core.set([" - (comma-sep items) "])"))) + (if (empty? items) + (emits "cljs.core.PersistentHashSet.EMPTY") + (emits "cljs.core.PersistentHashSet.fromArray([" + (comma-sep items) "])")))) (defmethod emit :constant [{:keys [form env]}] diff --git a/src/cljs/cljs/core.cljs b/src/cljs/cljs/core.cljs index 2b13ed6..46e4684 100644 --- a/src/cljs/cljs/core.cljs +++ b/src/cljs/cljs/core.cljs @@ -5589,6 +5589,15 @@ reduces them without incurring seq initialization" (set! cljs.core.PersistentHashSet/EMPTY (PersistentHashSet. nil (hash-map) 0)) +(set! cljs.core.PersistentHashSet/fromArray + (fn [items] + (let [len (count items)] + (loop [i 0 + out (transient cljs.core.PersistentHashSet/EMPTY)] + (if (< i len) + (recur (inc i) (conj! out (aget items i))) + (persistent! out)))))) + (deftype TransientHashSet [^:mutable transient-map] ITransientCollection (-conj! [tcoll o] -- 1.7.9.1