Skip to content

Commit b5546b2

Browse files
committed
Reuse parse options for identical ctx and opts
1 parent 4aafd93 commit b5546b2

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

src/sci/impl/interpreter.cljc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,9 @@
9797
#?@(:clj [utils/warn-on-reflection-var @utils/warn-on-reflection-var
9898
utils/unchecked-math-var @utils/unchecked-math-var])}
9999
(let [reader (r/indexing-push-back-reader (r/string-push-back-reader s))
100-
eval-string+? (when opts (:sci.impl/eval-string+ opts))
101-
parse-opts (parser/parse-opts ctx nil)]
100+
eval-string+? (when opts (:sci.impl/eval-string+ opts))]
102101
(loop [ret nil]
103-
(let [expr (parser/parse-next* reader parse-opts)]
102+
(let [expr (parser/parse-next ctx reader)]
104103
(if (utils/kw-identical? parser/eof expr)
105104
(if eval-string+?
106105
{:val ret

src/sci/impl/load.cljc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
:clj (instance? clojure.tools.reader.reader_types.IndexingReader reader)
2121
:cljs (implements? r/IndexingReader reader))
2222
reader
23-
(r/indexing-push-back-reader reader))
24-
parse-opts (parser/parse-opts ctx nil)]
23+
(r/indexing-push-back-reader reader))]
2524
(loop [ret nil]
26-
(let [x (parser/parse-next* reader parse-opts)]
25+
(let [x (parser/parse-next ctx reader)]
2726
(if (utils/kw-identical? parser/eof x)
2827
ret
2928
(recur (utils/eval ctx x)))))))

src/sci/impl/parser.cljc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,23 @@
188188
:file @utils/current-file)
189189
e)))))
190190

191+
;; The last ctx and opts with their parse options, so callers that parse form
192+
;; by form with the same ctx and opts reuse them
193+
(def last-parse-opts (volatile! nil))
194+
191195
(defn parse-next
192196
([ctx r]
193197
(parse-next ctx r nil))
194198
([ctx r opts]
195-
(parse-next* r (parse-opts ctx opts))))
199+
(let [cached @last-parse-opts
200+
edamame-opts (if (and cached
201+
(identical? ctx (nth cached 0))
202+
(identical? opts (nth cached 1)))
203+
(nth cached 2)
204+
(let [edamame-opts (parse-opts ctx opts)]
205+
(vreset! last-parse-opts [ctx opts edamame-opts])
206+
edamame-opts))]
207+
(parse-next* r edamame-opts))))
196208

197209
(defn reader [x]
198210
(edamame/reader x))

0 commit comments

Comments
 (0)