Permalink
Browse files

CLJS-567: Optimized ClojureScript reader by switching to a more perfo…

…rmant StringPushbackReader. Added another reader benchmark which reads a substantially bigger datastructure.
  • Loading branch information...
1 parent a749ab6 commit d19cc6c6138b950bbb6514881cc0688702cdb145 @jonase jonase committed with swannodette Aug 3, 2013
Showing with 24 additions and 11 deletions.
  1. +15 −0 benchmark/cljs/benchmark_runner.cljs
  2. +9 −11 src/cljs/cljs/reader.cljs
View
15 benchmark/cljs/benchmark_runner.cljs
@@ -227,7 +227,20 @@
(println)
(println ";;; reader")
+(def strings
+ (take 10 (iterate (fn [s] (str s "string")) "string")))
+
+(def big-str-data
+ (pr-str {:nils (repeat 10 nil)
+ :bools (concat (repeat 5 false) (repeat 5 true))
+ :ints (range 10000 10100)
+ :floats (map #(float (/ % 7)) (range 0 100))
+ :keywords (map keyword strings)
+ :symbols (map symbol strings)
+ :strings strings}))
+
(simple-benchmark [s "{:foo [1 2 3]}"] (reader/read-string s) 1000)
+(simple-benchmark [s big-str-data] (reader/read-string s) 1000)
(println)
(println ";;; range")
@@ -256,3 +269,5 @@
(println)
(println "\n")
+
+
View
20 src/cljs/cljs/reader.cljs
@@ -14,22 +14,20 @@
nil if the end of stream has been reached")
(unread [reader ch] "Push back a single character on to the stream"))
-; Using two atoms is less idomatic, but saves the repeat overhead of map creation
-(deftype StringPushbackReader [s index-atom buffer-atom]
+(deftype StringPushbackReader [s buffer ^:mutable idx]
PushbackReader
(read-char [reader]
- (if (empty? @buffer-atom)
- (let [idx @index-atom]
- (swap! index-atom inc)
- (aget s idx))
- (let [buf @buffer-atom]
- (swap! buffer-atom rest)
- (first buf))))
- (unread [reader ch] (swap! buffer-atom #(cons ch %))))
+ (if (zero? (alength buffer))
+ (do
+ (set! idx (inc idx))
+ (aget s idx))
+ (.pop buffer)))
+ (unread [reader ch]
+ (.push buffer ch)))
(defn push-back-reader [s]
"Creates a StringPushbackReader from a given string"
- (StringPushbackReader. s (atom 0) (atom nil)))
+ (StringPushbackReader. s (array) -1))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; predicates

0 comments on commit d19cc6c

Please sign in to comment.