Skip to content

Commit 1252c58

Browse files
committed
ASYNC-248 Dynamically load core.async go loop analyzer only when needed
Signed-off-by: Alex Miller <[email protected]>
1 parent 6450a57 commit 1252c58

File tree

6 files changed

+161
-125
lines changed

6 files changed

+161
-125
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</scm>
3232

3333
<properties>
34-
<clojure.version>1.9.0</clojure.version>
34+
<clojure.version>1.10.0</clojure.version>
3535
</properties>
3636

3737
<dependencies>

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
:license {:name "Eclipse Public License"
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
66
:parent [org.clojure/pom.contrib "1.0.0"]
7-
:dependencies [[org.clojure/clojure "1.9.0"]
7+
:dependencies [[org.clojure/clojure "1.10.0"]
88
[org.clojure/tools.analyzer.jvm "1.2.2"]
99
[org.clojure/clojurescript "1.10.597" :scope "provided"]]
1010
:global-vars {*warn-on-reflection* true}

src/main/clojure/clojure/core/async.clj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ to catch and handle."
2626
[clojure.core.async.impl.buffers :as buffers]
2727
[clojure.core.async.impl.timers :as timers]
2828
[clojure.core.async.impl.dispatch :as dispatch]
29-
[clojure.core.async.impl.ioc-macros :as ioc]
29+
[clojure.core.async.impl.runtime :as ioc]
3030
[clojure.core.async.impl.mutex :as mutex]
3131
[clojure.core.async.impl.concurrent :as conc]
3232
)
@@ -417,7 +417,7 @@ to catch and handle."
417417

418418
(defn ioc-alts! [state cont-block ports & {:as opts}]
419419
(ioc/aset-all! state ioc/STATE-IDX cont-block)
420-
(when-let [cb (clojure.core.async/do-alts
420+
(when-let [cb (do-alts
421421
(fn [val]
422422
(ioc/aset-all! state ioc/VALUE-IDX val)
423423
(ioc/run-state-machine-wrapped state))
@@ -462,7 +462,8 @@ to catch and handle."
462462
(dispatch/run
463463
(^:once fn* []
464464
(let [~@(mapcat (fn [[l sym]] [sym `(^:once fn* [] ~(vary-meta l dissoc :tag))]) crossing-env)
465-
f# ~(ioc/state-machine `(do ~@body) 1 [crossing-env &env] ioc/async-custom-terminators)
465+
f# ~((requiring-resolve 'clojure.core.async.impl.ioc-macros/state-machine)
466+
`(do ~@body) 1 [crossing-env &env] ioc/async-custom-terminators)
466467
state# (-> (f#)
467468
(ioc/aset-all! ioc/USER-START-IDX c#
468469
ioc/BINDINGS-IDX captured-bindings#))]

src/main/clojure/clojure/core/async/impl/ioc_macros.clj

Lines changed: 25 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,14 @@
2020
[clojure.tools.analyzer.passes.jvm.warn-on-reflection :refer [warn-on-reflection]]
2121
[clojure.tools.analyzer.jvm :as an-jvm]
2222
[clojure.core.async.impl.protocols :as impl]
23+
[clojure.core.async.impl.runtime :as rt]
2324
[clojure.set :as set])
24-
(:import [java.util.concurrent.locks Lock]
25-
[java.util.concurrent.atomic AtomicReferenceArray]))
25+
(:import [java.util.concurrent.atomic AtomicReferenceArray]))
2626

2727
(defn debug [x]
2828
(pprint x)
2929
x)
3030

31-
(def ^{:const true :tag 'long} FN-IDX 0)
32-
(def ^{:const true :tag 'long} STATE-IDX 1)
33-
(def ^{:const true :tag 'long} VALUE-IDX 2)
34-
(def ^{:const true :tag 'long} BINDINGS-IDX 3)
35-
(def ^{:const true :tag 'long} EXCEPTION-FRAMES 4)
36-
(def ^{:const true :tag 'long} USER-START-IDX 5)
37-
38-
(defn aset-object [^AtomicReferenceArray arr ^long idx o]
39-
(.set arr idx o))
40-
41-
(defn aget-object [^AtomicReferenceArray arr ^long idx]
42-
(.get arr idx))
43-
44-
(defmacro aset-all!
45-
[arr & more]
46-
(assert (even? (count more)) "Must give an even number of args to aset-all!")
47-
(let [bindings (partition 2 more)
48-
arr-sym (gensym "statearr-")]
49-
`(let [~arr-sym ~arr]
50-
~@(map
51-
(fn [[idx val]]
52-
`(aset-object ~arr-sym ~idx ~val))
53-
bindings)
54-
~arr-sym)))
55-
5631
;; State monad stuff, used only in SSA construction
5732

5833
(defmacro gen-plan
@@ -217,7 +192,7 @@
217192
IEmittableInstruction
218193
(emit-instruction [this state-sym]
219194
(if (= value ::value)
220-
`[~(:id this) (aget-object ~state-sym ~VALUE-IDX)]
195+
`[~(:id this) (rt/aget-object ~state-sym ~rt/VALUE-IDX)]
221196
`[~(:id this) ~value])))
222197

223198
(defrecord RawCode [ast locals]
@@ -317,11 +292,10 @@
317292
(terminate-block [_this state-sym _]
318293
`(do (case ~val-id
319294
~@(concat (mapcat (fn [test blk]
320-
`[~test (aset-all! ~state-sym
321-
~STATE-IDX ~blk)])
295+
`[~test (rt/aset-all! ~state-sym ~rt/STATE-IDX ~blk)])
322296
test-vals jmp-blocks)
323297
(when default-block
324-
`[(do (aset-all! ~state-sym ~STATE-IDX ~default-block)
298+
`[(do (rt/aset-all! ~state-sym ~rt/STATE-IDX ~default-block)
325299
:recur)])))
326300
:recur)))
327301

@@ -352,7 +326,7 @@
352326
(block-references [_this] [block])
353327
ITerminator
354328
(terminate-block [_this state-sym _]
355-
`(do (aset-all! ~state-sym ~VALUE-IDX ~value ~STATE-IDX ~block)
329+
`(do (rt/aset-all! ~state-sym ~rt/VALUE-IDX ~value ~rt/STATE-IDX ~block)
356330
:recur)))
357331

358332
(defrecord Return [value]
@@ -365,9 +339,7 @@
365339
(terminate-block [this state-sym custom-terminators]
366340
(if-let [f (get custom-terminators (terminator-code this))]
367341
`(~f ~state-sym ~value)
368-
`(do (aset-all! ~state-sym
369-
~VALUE-IDX ~value
370-
~STATE-IDX ::finished)
342+
`(do (rt/aset-all! ~state-sym ~rt/VALUE-IDX ~value ~rt/STATE-IDX ::finished)
371343
nil))))
372344

373345
(defrecord CondBr [test then-block else-block]
@@ -378,10 +350,8 @@
378350
ITerminator
379351
(terminate-block [_this state-sym _]
380352
`(do (if ~test
381-
(aset-all! ~state-sym
382-
~STATE-IDX ~then-block)
383-
(aset-all! ~state-sym
384-
~STATE-IDX ~else-block))
353+
(rt/aset-all! ~state-sym ~rt/STATE-IDX ~then-block)
354+
(rt/aset-all! ~state-sym ~rt/STATE-IDX ~else-block))
385355
:recur)))
386356

387357
(defrecord PushTry [catch-block]
@@ -391,7 +361,7 @@
391361
(block-references [_this] [catch-block])
392362
IEmittableInstruction
393363
(emit-instruction [_this state-sym]
394-
`[~'_ (aset-all! ~state-sym ~EXCEPTION-FRAMES (cons ~catch-block (aget-object ~state-sym ~EXCEPTION-FRAMES)))]))
364+
`[~'_ (rt/aset-all! ~state-sym ~rt/EXCEPTION-FRAMES (cons ~catch-block (rt/aget-object ~state-sym ~rt/EXCEPTION-FRAMES)))]))
395365

396366
(defrecord PopTry []
397367
IInstruction
@@ -400,7 +370,7 @@
400370
(block-references [_this] [])
401371
IEmittableInstruction
402372
(emit-instruction [_this state-sym]
403-
`[~'_ (aset-all! ~state-sym ~EXCEPTION-FRAMES (rest (aget-object ~state-sym ~EXCEPTION-FRAMES)))]))
373+
`[~'_ (rt/aset-all! ~state-sym ~rt/EXCEPTION-FRAMES (rest (rt/aget-object ~state-sym ~rt/EXCEPTION-FRAMES)))]))
404374

405375
(defrecord CatchHandler [catches]
406376
IInstruction
@@ -410,10 +380,10 @@
410380
ITerminator
411381
(terminate-block [_this state-sym _]
412382
(let [ex (gensym 'ex)]
413-
`(let [~ex (aget-object ~state-sym ~VALUE-IDX)]
383+
`(let [~ex (rt/aget-object ~state-sym ~rt/VALUE-IDX)]
414384
(cond
415385
~@(for [[handler-idx type] catches
416-
i [`(instance? ~type ~ex) `(aset-all! ~state-sym ~STATE-IDX ~handler-idx)]]
386+
i [`(instance? ~type ~ex) `(rt/aset-all! ~state-sym ~rt/STATE-IDX ~handler-idx)]]
417387
i)
418388
:else (throw ~ex))
419389
:recur))))
@@ -893,7 +863,7 @@
893863
(if (empty? args)
894864
[]
895865
(mapcat (fn [sym]
896-
`[~sym (aget-object ~state-sym ~(id-for-inst local-map sym))])
866+
`[~sym (rt/aget-object ~state-sym ~(id-for-inst local-map sym))])
897867
args))))
898868

899869
(defn- build-block-body [state-sym blk]
@@ -910,27 +880,27 @@
910880
blk)
911881
results (interleave (map (partial id-for-inst local-map) results) results)]
912882
(if-not (empty? results)
913-
[state-sym `(aset-all! ~state-sym ~@results)]
883+
[state-sym `(rt/aset-all! ~state-sym ~@results)]
914884
[])))
915885

916886
(defn- emit-state-machine [machine num-user-params custom-terminators]
917887
(let [index (index-state-machine machine)
918888
state-sym (with-meta (gensym "state_")
919889
{:tag 'objects})
920-
local-start-idx (+ num-user-params USER-START-IDX)
890+
local-start-idx (+ num-user-params rt/USER-START-IDX)
921891
state-arr-size (+ local-start-idx (count-persistent-values index))
922892
local-map (atom {::next-idx local-start-idx})
923893
block-catches (:block-catches machine)]
924894
`(fn state-machine#
925-
([] (aset-all! (AtomicReferenceArray. ~state-arr-size)
926-
~FN-IDX state-machine#
927-
~STATE-IDX ~(:start-block machine)))
895+
([] (rt/aset-all! (AtomicReferenceArray. ~state-arr-size)
896+
~rt/FN-IDX state-machine#
897+
~rt/STATE-IDX ~(:start-block machine)))
928898
([~state-sym]
929899
(let [old-frame# (clojure.lang.Var/getThreadBindingFrame)
930900
ret-value# (try
931-
(clojure.lang.Var/resetThreadBindingFrame (aget-object ~state-sym ~BINDINGS-IDX))
901+
(clojure.lang.Var/resetThreadBindingFrame (rt/aget-object ~state-sym ~rt/BINDINGS-IDX))
932902
(loop []
933-
(let [result# (case (int (aget-object ~state-sym ~STATE-IDX))
903+
(let [result# (case (int (rt/aget-object ~state-sym ~rt/STATE-IDX))
934904
~@(mapcat
935905
(fn [[id blk]]
936906
[id `(let [~@(concat (build-block-preamble local-map index state-sym blk)
@@ -942,77 +912,18 @@
942912
(recur)
943913
result#)))
944914
(catch Throwable ex#
945-
(aset-all! ~state-sym ~VALUE-IDX ex#)
946-
(if (seq (aget-object ~state-sym ~EXCEPTION-FRAMES))
947-
(aset-all! ~state-sym ~STATE-IDX (first (aget-object ~state-sym ~EXCEPTION-FRAMES)))
915+
(rt/aset-all! ~state-sym ~rt/VALUE-IDX ex#)
916+
(if (seq (rt/aget-object ~state-sym ~rt/EXCEPTION-FRAMES))
917+
(rt/aset-all! ~state-sym ~rt/STATE-IDX (first (rt/aget-object ~state-sym ~rt/EXCEPTION-FRAMES)))
948918
(throw ex#))
949919
:recur)
950920
(finally
951-
(aset-object ~state-sym ~BINDINGS-IDX (clojure.lang.Var/getThreadBindingFrame))
921+
(rt/aset-object ~state-sym ~rt/BINDINGS-IDX (clojure.lang.Var/getThreadBindingFrame))
952922
(clojure.lang.Var/resetThreadBindingFrame old-frame#)))]
953923
(if (identical? ret-value# :recur)
954924
(recur ~state-sym)
955925
ret-value#))))))
956926

957-
(defn finished?
958-
"Returns true if the machine is in a finished state"
959-
[state-array]
960-
(identical? (aget-object state-array STATE-IDX) ::finished))
961-
962-
(defn- fn-handler
963-
[f]
964-
(reify
965-
Lock
966-
(lock [_])
967-
(unlock [_])
968-
969-
impl/Handler
970-
(active? [_] true)
971-
(blockable? [_] true)
972-
(lock-id [_] 0)
973-
(commit [_] f)))
974-
975-
976-
(defn run-state-machine [state]
977-
((aget-object state FN-IDX) state))
978-
979-
(defn run-state-machine-wrapped [state]
980-
(try
981-
(run-state-machine state)
982-
(catch Throwable ex
983-
(impl/close! (aget-object state USER-START-IDX))
984-
(throw ex))))
985-
986-
(defn take! [state blk c]
987-
(if-let [cb (impl/take! c (fn-handler
988-
(fn [x]
989-
(aset-all! state VALUE-IDX x STATE-IDX blk)
990-
(run-state-machine-wrapped state))))]
991-
(do (aset-all! state VALUE-IDX @cb STATE-IDX blk)
992-
:recur)
993-
nil))
994-
995-
(defn put! [state blk c val]
996-
(if-let [cb (impl/put! c val (fn-handler (fn [ret-val]
997-
(aset-all! state VALUE-IDX ret-val STATE-IDX blk)
998-
(run-state-machine-wrapped state))))]
999-
(do (aset-all! state VALUE-IDX @cb STATE-IDX blk)
1000-
:recur)
1001-
nil))
1002-
1003-
(defn return-chan [state value]
1004-
(let [c (aget-object state USER-START-IDX)]
1005-
(when-not (nil? value)
1006-
(impl/put! c value (fn-handler (fn [_] nil))))
1007-
(impl/close! c)
1008-
c))
1009-
1010-
(def async-custom-terminators
1011-
{'clojure.core.async/<! `take!
1012-
'clojure.core.async/>! `put!
1013-
'clojure.core.async/alts! 'clojure.core.async/ioc-alts!
1014-
:Return `return-chan})
1015-
1016927
(defn mark-transitions
1017928
{:pass-info {:walk :post :depends #{} :after an-jvm/default-passes}}
1018929
[{:keys [op fn] :as ast}]

0 commit comments

Comments
 (0)