Skip to content

Commit 605b570

Browse files
author
dnolen
committed
CLJS-1067: Shared AOT cache for dependencies in JARs
also memoize parse-ns
1 parent f2a888a commit 605b570

File tree

2 files changed

+58
-37
lines changed

2 files changed

+58
-37
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3723,7 +3723,7 @@
37233723
(symbol (str "cljs.user." name (util/content-sha full-name 7)))))))
37243724

37253725
#?(:clj
3726-
(defn parse-ns
3726+
(defn ^:dynamic parse-ns
37273727
"Helper for parsing only the essential namespace information from a
37283728
ClojureScript source file and returning a cljs.closure/IJavaScript compatible
37293729
map _not_ a namespace AST node.

src/main/clojure/cljs/closure.clj

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

572+
(defn build-affecting-options-sha [opts]
573+
(util/content-sha (pr-str (comp/build-affecting-options opts)) 7))
574+
575+
(defn ^File cache-base-path
576+
([]
577+
(cache-base-path nil))
578+
([opts]
579+
(io/file (System/getProperty "user.home")
580+
".cljs" ".aot_cache" (util/clojurescript-version)
581+
(build-affecting-options-sha opts))))
582+
583+
(defn cacheable-files
584+
([ns ext]
585+
(cacheable-files ns ext nil))
586+
([ns ext opts]
587+
(let [path (cache-base-path opts)
588+
name (util/ns->relpath ns nil)]
589+
(into {}
590+
(map
591+
(fn [[k v]]
592+
[k (io/file path (str name v))]))
593+
{:source (str "." ext)
594+
:output-file ".js"
595+
:source-map ".js.map"
596+
:analysis-cache-edn ".cljs.cache.edn"
597+
:analysis-cache-json ".cljs.cache.json"}))))
598+
572599
(defn ^String path-from-jarfile
573600
"Given the URL of a file within a jar, return the path of the file
574601
from the root of the jar."
@@ -592,50 +619,43 @@
592619
(.setLastModified ^File out-file (util/last-modified url))
593620
out-file)))
594621

595-
(defn build-affecting-options-sha [opts]
596-
(util/content-sha (pr-str (comp/build-affecting-options opts)) 7))
597-
598-
(defn cache-base-path
599-
([]
600-
(cache-base-path nil))
601-
([opts]
602-
(io/file (System/getProperty "user.home")
603-
".cljs" ".aot_cache" (util/clojurescript-version)
604-
(build-affecting-options-sha opts))))
605-
606-
(defn cacheable-files
607-
([ns]
608-
(cacheable-files ns nil))
609-
([ns opts]
610-
(let [path (cache-base-path opts)
611-
name (util/ns->relpath ns nil)]
612-
(map #(io/file path (str name %))
613-
[".js" ".cljs" ".cljs.cache.edn" ".cljs.cache.json" ".js.map"]))))
614-
615622
;; TODO: it would be nice if we could consolidate requires-compilation?
616623
;; logic - David
617624
(defn compile-from-jar
618625
"Compile a file from a jar if necessary. Returns IJavaScript."
619626
[jar-file {:keys [output-file] :as opts}]
620-
(let [out-file (when output-file
621-
(io/file (util/output-directory opts) output-file))]
622-
(if (or (nil? out-file)
623-
(not (.exists ^File out-file))
624-
(not= (util/compiled-by-version out-file)
625-
(util/clojurescript-version))
626-
(util/changed? jar-file out-file))
627+
(let [{:keys [ns]} (ana/parse-ns jar-file)
628+
out-file (when output-file
629+
(io/file (util/output-directory opts) output-file))
630+
cacheable (cacheable-files ns (util/ext jar-file) opts)]
631+
(when (or (nil? out-file)
632+
(not (.exists ^File out-file))
633+
(not= (util/compiled-by-version out-file)
634+
(util/clojurescript-version))
635+
(util/changed? jar-file out-file))
627636
;; actually compile from JAR
628-
(let [file-on-disk (jar-file-to-disk jar-file (util/output-directory opts) opts)]
629-
(-compile file-on-disk opts))
630-
;; have to call compile-file as it includes more IJavaScript
631-
;; information than ana/parse-ns
632-
(compile-file
633-
(io/file (util/output-directory opts)
634-
(last (string/split (.getPath ^URL jar-file) #"\.jar!/")))
635-
opts))))
637+
(let [cache-path (cache-base-path opts)]
638+
(when-not (.exists (:output-file cacheable))
639+
(-compile (jar-file-to-disk jar-file cache-path opts) opts))
640+
(doseq [[k ^File f] cacheable]
641+
(when (.exists f)
642+
(let [target (io/file (util/output-directory opts)
643+
(-> (.getAbsolutePath f)
644+
(string/replace (.getAbsolutePath cache-path) "")
645+
(subs 1)))]
646+
(when (and ana/*verbose* (= :source k))
647+
(util/debug-prn (str "Copying cached " f " to " target)))
648+
(spit target (slurp f))
649+
(.setLastModified target (util/last-modified jar-file)))))))
650+
;; have to call compile-file as it includes more IJavaScript
651+
;; information than ana/parse-ns for now
652+
(compile-file
653+
(io/file (util/output-directory opts)
654+
(last (string/split (.getPath ^URL jar-file) #"\.jar!/")))
655+
opts)))
636656

637657
(defn find-jar-sources
638-
[this opts]
658+
[this opts] ()
639659
[(comp/find-source (jar-file-to-disk this (util/output-directory opts)))])
640660

641661
(extend-protocol Compilable
@@ -2736,6 +2756,7 @@
27362756
(binding [comp/*recompiled* (when-not (false? (:recompile-dependents opts))
27372757
(atom #{}))
27382758
ana/*checked-arrays* checked-arrays
2759+
ana/parse-ns (memoize ana/parse-ns)
27392760
ana/*cljs-static-fns* static-fns?
27402761
ana/*fn-invoke-direct* (or (and static-fns?
27412762
(:fn-invoke-direct opts))

0 commit comments

Comments
 (0)