Skip to content

Commit 9245905

Browse files
faviladnolen
authored andcommitted
CLJS-1761: Allow parallel Transit analysis cache writes
Pre-assemble the full read and write handler maps for transit types so we avoid any race conditions in transit's handler caching.
1 parent 309de72 commit 9245905

1 file changed

Lines changed: 37 additions & 34 deletions

File tree

src/main/clojure/cljs/analyzer.cljc

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,41 +58,48 @@
5858

5959
#?(:clj
6060
(def transit-read-opts
61-
(util/compile-if (import '[com.cognitect.transit ReadHandler])
62-
{:handlers
63-
{"cljs/js"
64-
(reify com.cognitect.transit.ReadHandler
65-
(fromRep [_ v] (JSValue. v)))
66-
"cljs/regex"
67-
(reify com.cognitect.transit.ReadHandler
68-
(fromRep [_ v] (Pattern/compile v)))}})))
61+
(try
62+
(require '[cognitect.transit])
63+
(when-some [ns (find-ns 'cognitect.transit)]
64+
(let [read-handler @(ns-resolve ns 'read-handler)
65+
read-handler-map @(ns-resolve ns 'read-handler-map)]
66+
{:handlers
67+
(read-handler-map
68+
{"cljs/js" (read-handler (fn [_ v] (JSValue. v)))
69+
"cljs/regex" (read-handler (fn [_ v] (Pattern/compile v)))})}))
70+
(catch Throwable t
71+
nil))))
6972

7073
#?(:clj
7174
(def transit-write-opts
72-
(util/compile-if (import '[com.cognitect.transit WriteHandler])
73-
{:handlers
74-
{JSValue
75-
(reify com.cognitect.transit.WriteHandler
76-
(tag [_ _] "cljs/js")
77-
(rep [_ js] (.val ^JSValue js))
78-
(stringRep [_ _] nil))
79-
Pattern
80-
(reify com.cognitect.transit.WriteHandler
81-
(tag [_ _] "cljs/regex")
82-
(rep [_ pat] (.pattern ^Pattern pat))
83-
(stringRep [_ _] nil))}})))
75+
(try
76+
(require '[cognitect.transit])
77+
(when-some [ns (find-ns 'cognitect.transit)]
78+
(let [write-handler @(ns-resolve ns 'write-handler)
79+
write-handler-map @(ns-resolve ns 'write-handler-map)]
80+
{:handlers
81+
(write-handler-map
82+
{JSValue
83+
(write-handler
84+
(fn [_ _] "cljs/js")
85+
(fn [_ js] (.val ^JSValue js)))
86+
Pattern
87+
(write-handler
88+
(fn [_ _] "cljs/regex")
89+
(fn [_ pat] (.pattern ^Pattern pat)))})}))
90+
(catch Throwable t
91+
nil))))
8492

8593
#?(:clj
8694
(def transit
8795
(delay
8896
(try
8997
(require '[cognitect.transit])
90-
(let [ns (find-ns 'cognitect.transit)]
91-
(when ns
92-
{:writer @(ns-resolve ns 'writer)
93-
:reader @(ns-resolve ns 'reader)
94-
:write @(ns-resolve ns 'write)
95-
:read @(ns-resolve ns 'read)}))
98+
(when-some [ns (find-ns 'cognitect.transit)]
99+
{:writer @(ns-resolve ns 'writer)
100+
:reader @(ns-resolve ns 'reader)
101+
:write @(ns-resolve ns 'write)
102+
:read @(ns-resolve ns 'read)})
96103
(catch Throwable t
97104
nil)))))
98105

@@ -3113,9 +3120,6 @@
31133120
true
31143121
(util/changed? src cache)))))))
31153122

3116-
#?(:clj
3117-
(def transit-write-mutex (Object.)))
3118-
31193123
#?(:clj
31203124
(defn write-analysis-cache
31213125
([ns cache-file]
@@ -3130,11 +3134,10 @@
31303134
(str ";; Analyzed by ClojureScript " (util/clojurescript-version) "\n"))
31313135
(pr-str analysis)))
31323136
"json" (when-let [{:keys [writer write]} @transit]
3133-
(locking transit-write-mutex
3134-
(write
3135-
(writer (FileOutputStream. cache-file) :json
3136-
transit-write-opts)
3137-
analysis)))))
3137+
(write
3138+
(writer (FileOutputStream. cache-file) :json
3139+
transit-write-opts)
3140+
analysis))))
31383141
(when src
31393142
(.setLastModified ^File cache-file (util/last-modified src))))))
31403143

0 commit comments

Comments
 (0)