Skip to content

Commit 991ceeb

Browse files
committed
optimize Symbol construction - including precomputing the str rep and
calculating the hashcode. add array-map w/ symbols benchmarks where we don't avoid the cost of constructing symbols everytime, seems ~1.5X slower under v8
1 parent 46eb862 commit 991ceeb

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

benchmark/cljs/benchmark_runner.cljs

+8
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@
141141
(simple-benchmark [coll (array-map a b c d e f)] (-lookup coll e) 1000000))
142142
(println)
143143

144+
(println ";;; array-map w/ inline symbols")
145+
(simple-benchmark [coll (array-map)] (assoc coll 'foo 'bar) 1000000)
146+
(simple-benchmark [coll (array-map 'foo 'bar)] (-lookup coll 'foo) 1000000)
147+
(simple-benchmark [coll (array-map 'foo 'bar)] (assoc coll 'baz 'woz) 1000000)
148+
(simple-benchmark [coll (array-map 'foo 'bar 'baz 'woz)] (-lookup coll 'baz) 1000000)
149+
(simple-benchmark [coll (array-map 'foo 'bar 'baz 'woz 'lol 'rofl)] (-lookup coll 'lol) 1000000)
150+
(println)
151+
144152
(def data-atom (atom {:x 0}))
145153

146154
(println ";;; map / record ops")

src/clj/cljs/compiler.clj

+23-6
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,31 @@
160160
(name x)
161161
\"))
162162

163-
;; TODO: we could optimize, call type constructor & inline all the properties of x - David
163+
(def ^:const goog-hash-max 0x100000000)
164+
165+
(defn goog-string-hash [s]
166+
(reduce
167+
(fn [r c]
168+
(mod (+ (* 31 r) (int c)) goog-hash-max))
169+
0 s))
164170

165171
(defmethod emit-constant clojure.lang.Symbol [x]
166-
(emits "cljs.core.symbol(")
167-
(emit-constant (namespace x))
168-
(emits ",")
169-
(emit-constant (name x))
170-
(emits ")"))
172+
(let [ns (namespace x)
173+
name (name x)
174+
symstr (if-not (nil? ns)
175+
(str ns "/" name)
176+
name)]
177+
(emits "new cljs.core.Symbol(")
178+
(emit-constant ns)
179+
(emits ",")
180+
(emit-constant name)
181+
(emits ",")
182+
(emit-constant symstr)
183+
(emits ",")
184+
(emit-constant (goog-string-hash symstr))
185+
(emits ",")
186+
(emit-constant nil)
187+
(emits ")")))
171188

172189
(defn- emit-meta-constant [x & body]
173190
(if (meta x)

0 commit comments

Comments
 (0)