Skip to content

Commit ff573d1

Browse files
author
dnolen
committed
CLJS-2607: Shared AOT cache: Analyzing cached files
Make transit-clj a direct dep, to ensure we can load analysis quickly. Make analysis global AOT cache aware. This mostly benefits REPLs where there is not necessarily a build in dependency order and we have to analyze as we go.
1 parent 265f398 commit ff573d1

File tree

4 files changed

+76
-61
lines changed

4 files changed

+76
-61
lines changed

pom.template.xml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@
5353
<version>1.3.0-alpha3</version>
5454
</dependency>
5555
<dependency>
56-
<groupId>org.clojure</groupId>
57-
<artifactId>test.check</artifactId>
58-
<version>0.10.0-alpha2</version>
59-
<scope>test</scope>
56+
<groupId>com.cognitect</groupId>
57+
<artifactId>transit-clj</artifactId>
58+
<version>0.8.300</version>
6059
<exclusions>
6160
<exclusion>
6261
<groupId>org.clojure</groupId>
@@ -65,10 +64,10 @@
6564
</exclusions>
6665
</dependency>
6766
<dependency>
68-
<groupId>com.cognitect</groupId>
69-
<artifactId>transit-clj</artifactId>
70-
<version>0.8.300</version>
71-
<scope>provided</scope>
67+
<groupId>org.clojure</groupId>
68+
<artifactId>test.check</artifactId>
69+
<version>0.10.0-alpha2</version>
70+
<scope>test</scope>
7271
<exclusions>
7372
<exclusion>
7473
<groupId>org.clojure</groupId>

src/main/clojure/cljs/analyzer.cljc

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3833,25 +3833,75 @@
38333833
([format]
38343834
(if (and (= format :transit) @transit) "json" "edn"))))
38353835

3836+
#?(:clj
3837+
(defn build-affecting-options [opts]
3838+
(select-keys opts
3839+
[:static-fns :fn-invoke-direct :optimize-constants :elide-asserts :target
3840+
:cache-key :checked-arrays :language-out])))
3841+
3842+
#?(:clj
3843+
(defn build-affecting-options-sha [path opts]
3844+
(let [m (assoc (build-affecting-options opts) :path path)]
3845+
(util/content-sha (pr-str m) 7))))
3846+
3847+
#?(:clj
3848+
(defn ^File cache-base-path
3849+
([path]
3850+
(cache-base-path path nil))
3851+
([path opts]
3852+
(io/file (System/getProperty "user.home")
3853+
".cljs" ".aot_cache" (util/clojurescript-version)
3854+
(build-affecting-options-sha path opts)))))
3855+
3856+
#?(:clj
3857+
(defn cacheable-files
3858+
([rsrc ext]
3859+
(cacheable-files rsrc ext nil))
3860+
([rsrc ext opts]
3861+
(let [{:keys [ns]} (parse-ns rsrc)
3862+
path (cache-base-path (util/path rsrc) opts)
3863+
name (util/ns->relpath ns nil File/separatorChar)]
3864+
(into {}
3865+
(map
3866+
(fn [[k v]]
3867+
[k (io/file path
3868+
(if (and (= (str "cljs" File/separatorChar "core$macros") name)
3869+
(= :source k))
3870+
(str "cljs" File/separatorChar "core.cljc")
3871+
(str name v)))]))
3872+
{:source (str "." ext)
3873+
:output-file ".js"
3874+
:source-map ".js.map"
3875+
:analysis-cache-edn (str "." ext ".cache.edn")
3876+
:analysis-cache-json (str "." ext ".cache.json")})))))
3877+
38363878
#?(:clj
38373879
(defn cache-file
38383880
"Given a ClojureScript source file returns the read/write path to the analysis
38393881
cache file. Defaults to the read path which is usually also the write path."
38403882
([src] (cache-file src "out"))
38413883
([src output-dir] (cache-file src (parse-ns src) output-dir))
38423884
([src ns-info output-dir]
3843-
(cache-file src (parse-ns src) output-dir :read))
3885+
(cache-file src ns-info output-dir :read nil))
38443886
([src ns-info output-dir mode]
3887+
(cache-file src ns-info output-dir mode nil))
3888+
([src ns-info output-dir mode opts]
38453889
{:pre [(map? ns-info)]}
38463890
(let [ext (cache-analysis-ext)]
38473891
(if-let [core-cache
38483892
(and (= mode :read)
38493893
(= (:ns ns-info) 'cljs.core)
38503894
(io/resource (str "cljs/core.cljs.cache.aot." ext)))]
38513895
core-cache
3852-
(let [target-file (util/to-target-file output-dir ns-info
3853-
(util/ext (:source-file ns-info)))]
3854-
(io/file (str target-file ".cache." ext))))))))
3896+
(let [aot-cache-file
3897+
(when (util/url? src)
3898+
((keyword (str "analysis-cache-" ext))
3899+
(cacheable-files src (util/ext src) opts)))]
3900+
(if (and aot-cache-file (.exists ^File aot-cache-file))
3901+
aot-cache-file
3902+
(let [target-file (util/to-target-file output-dir ns-info
3903+
(util/ext (:source-file ns-info)))]
3904+
(io/file (str target-file ".cache." ext))))))))))
38553905

38563906
#?(:clj
38573907
(defn requires-analysis?
@@ -3861,8 +3911,10 @@
38613911
([src] (requires-analysis? src "out"))
38623912
([src output-dir]
38633913
(let [cache (cache-file src output-dir)]
3864-
(requires-analysis? src cache output-dir)))
3914+
(requires-analysis? src cache output-dir nil)))
38653915
([src cache output-dir]
3916+
(requires-analysis? src cache output-dir nil))
3917+
([src cache output-dir opts]
38663918
(cond
38673919
(util/url? cache)
38683920
(let [path (.getPath ^URL cache)]
@@ -3876,10 +3928,12 @@
38763928
true
38773929

38783930
:else
3879-
(let [out-src (util/to-target-file output-dir (parse-ns src))]
3880-
(if (not (.exists out-src))
3931+
(let [out-src (util/to-target-file output-dir (parse-ns src))
3932+
cache-src (:output-file (cacheable-files src (util/ext src) opts))]
3933+
(if (and (not (.exists out-src))
3934+
(not (.exists ^File cache-src)))
38813935
true
3882-
(util/changed? src cache)))))))
3936+
(or (not cache) (util/changed? src cache))))))))
38833937

38843938
#?(:clj
38853939
(defn- get-spec-vars
@@ -4049,9 +4103,9 @@
40494103
(.getPath ^File res)
40504104
(.getPath ^URL res))
40514105
cache (when (:cache-analysis opts)
4052-
(cache-file res ns-info output-dir))]
4106+
(cache-file res ns-info output-dir :read opts))]
40534107
(when-not (get-in @env/*compiler* [::namespaces (:ns ns-info) :defs])
4054-
(if (or skip-cache (not cache) (requires-analysis? res output-dir))
4108+
(if (or skip-cache (not cache) (requires-analysis? res cache output-dir opts))
40554109
(binding [*cljs-ns* 'cljs.user
40564110
*cljs-file* path
40574111
reader/*alias-map* (or reader/*alias-map* {})]

src/main/clojure/cljs/closure.clj

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -569,39 +569,6 @@
569569
(map compiled-file
570570
(comp/compile-root src-dir out-dir opts))))
571571

572-
(defn build-affecting-options-sha [path opts]
573-
(let [m (assoc (comp/build-affecting-options opts) :path path)]
574-
(util/content-sha (pr-str m) 7)))
575-
576-
(defn ^File cache-base-path
577-
([path]
578-
(cache-base-path path nil))
579-
([path opts]
580-
(io/file (System/getProperty "user.home")
581-
".cljs" ".aot_cache" (util/clojurescript-version)
582-
(build-affecting-options-sha path opts))))
583-
584-
(defn cacheable-files
585-
([rsrc ext]
586-
(cacheable-files rsrc ext nil))
587-
([rsrc ext opts]
588-
(let [{:keys [ns]} (ana/parse-ns rsrc)
589-
path (cache-base-path (util/path rsrc) opts)
590-
name (util/ns->relpath ns nil File/separatorChar)]
591-
(into {}
592-
(map
593-
(fn [[k v]]
594-
[k (io/file path
595-
(if (and (= (str "cljs" File/separatorChar "core$macros") name)
596-
(= :source k))
597-
(str "cljs" File/separatorChar "core.cljc")
598-
(str name v)))]))
599-
{:source (str "." ext)
600-
:output-file ".js"
601-
:source-map ".js.map"
602-
:analysis-cache-edn (str "." ext ".cache.edn")
603-
:analysis-cache-json (str "." ext ".cache.json")}))))
604-
605572
(defn ^String path-from-jarfile
606573
"Given the URL of a file within a jar, return the path of the file
607574
from the root of the jar."
@@ -632,7 +599,7 @@
632599
[jar-file {:keys [output-file] :as opts}]
633600
(let [out-file (when output-file
634601
(io/file (util/output-directory opts) output-file))
635-
cacheable (cacheable-files jar-file (util/ext jar-file) opts)]
602+
cacheable (ana/cacheable-files jar-file (util/ext jar-file) opts)]
636603
(when (or (nil? out-file)
637604
(not (.exists ^File out-file))
638605
(not= (util/compiled-by-version out-file)
@@ -641,7 +608,7 @@
641608
;; actually compile from JAR
642609
(if-not (:aot-cache opts)
643610
(-compile (jar-file-to-disk jar-file (util/output-directory opts) opts) opts)
644-
(let [cache-path (cache-base-path (util/path jar-file) opts)]
611+
(let [cache-path (ana/cache-base-path (util/path jar-file) opts)]
645612
(when-not (.exists (:output-file cacheable))
646613
(-compile (jar-file-to-disk jar-file cache-path opts)
647614
(assoc opts :output-dir (util/path cache-path))))

src/main/clojure/cljs/compiler.cljc

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,11 +1288,6 @@
12881288
(defn url-path [^File f]
12891289
(.getPath (.toURL (.toURI f)))))
12901290

1291-
(defn build-affecting-options [opts]
1292-
(select-keys opts
1293-
[:static-fns :fn-invoke-direct :optimize-constants :elide-asserts :target
1294-
:cache-key :checked-arrays :language-out]))
1295-
12961291
#?(:clj
12971292
(defn compiled-by-string
12981293
([]
@@ -1303,7 +1298,7 @@
13031298
(str "// Compiled by ClojureScript "
13041299
(util/clojurescript-version)
13051300
(when opts
1306-
(str " " (pr-str (build-affecting-options opts))))))))
1301+
(str " " (pr-str (ana/build-affecting-options opts))))))))
13071302

13081303
#?(:clj
13091304
(defn cached-core [ns ext opts]
@@ -1491,8 +1486,8 @@
14911486
(and version (not= version version')))
14921487
(and opts
14931488
(not (and (io/resource "cljs/core.aot.js") (= 'cljs.core ns)))
1494-
(not= (build-affecting-options opts)
1495-
(build-affecting-options (util/build-options dest))))
1489+
(not= (ana/build-affecting-options opts)
1490+
(ana/build-affecting-options (util/build-options dest))))
14961491
(and opts (:source-map opts)
14971492
(if (= (:optimizations opts) :none)
14981493
(not (.exists (io/file (str (.getPath dest) ".map"))))

0 commit comments

Comments
 (0)