Browse files

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
  • Loading branch information...
1 parent 46eb862 commit 991ceebd3d6670d15a97fd4e4148c379f911a5b4 @swannodette swannodette committed Apr 30, 2013
Showing with 31 additions and 6 deletions.
  1. +8 −0 benchmark/cljs/benchmark_runner.cljs
  2. +23 −6 src/clj/cljs/compiler.clj
View
8 benchmark/cljs/benchmark_runner.cljs
@@ -141,6 +141,14 @@
(simple-benchmark [coll (array-map a b c d e f)] (-lookup coll e) 1000000))
(println)
+(println ";;; array-map w/ inline symbols")
+(simple-benchmark [coll (array-map)] (assoc coll 'foo 'bar) 1000000)
+(simple-benchmark [coll (array-map 'foo 'bar)] (-lookup coll 'foo) 1000000)
+(simple-benchmark [coll (array-map 'foo 'bar)] (assoc coll 'baz 'woz) 1000000)
+(simple-benchmark [coll (array-map 'foo 'bar 'baz 'woz)] (-lookup coll 'baz) 1000000)
+(simple-benchmark [coll (array-map 'foo 'bar 'baz 'woz 'lol 'rofl)] (-lookup coll 'lol) 1000000)
+(println)
+
(def data-atom (atom {:x 0}))
(println ";;; map / record ops")
View
29 src/clj/cljs/compiler.clj
@@ -160,14 +160,31 @@
(name x)
\"))
-;; TODO: we could optimize, call type constructor & inline all the properties of x - David
+(def ^:const goog-hash-max 0x100000000)
+
+(defn goog-string-hash [s]
+ (reduce
+ (fn [r c]
+ (mod (+ (* 31 r) (int c)) goog-hash-max))
+ 0 s))
(defmethod emit-constant clojure.lang.Symbol [x]
- (emits "cljs.core.symbol(")
- (emit-constant (namespace x))
- (emits ",")
- (emit-constant (name x))
- (emits ")"))
+ (let [ns (namespace x)
+ name (name x)
+ symstr (if-not (nil? ns)
+ (str ns "/" name)
+ name)]
+ (emits "new cljs.core.Symbol(")
+ (emit-constant ns)
+ (emits ",")
+ (emit-constant name)
+ (emits ",")
+ (emit-constant symstr)
+ (emits ",")
+ (emit-constant (goog-string-hash symstr))
+ (emits ",")
+ (emit-constant nil)
+ (emits ")")))
(defn- emit-meta-constant [x & body]
(if (meta x)

0 comments on commit 991ceeb

Please sign in to comment.