Skip to content

Commit b4655ef

Browse files
committed
Related #32: asynchronously load implementation
saves 200ms on my machine before: user=> (time ((requiring-resolve 'java-time/instant))) "Elapsed time: 1363.621694 msecs" after: user=> (time ((requiring-resolve 'java-time/instant))) "Elapsed time: 1113.720916 msecs"
1 parent 5ae7e89 commit b4655ef

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
* #81(terop): Remove clj-tuple - no advantages over Clojure vector anymore
99
* Remove `java-time.util/get-static-fields-of-type`
1010
* set Java property `java-time.util.get-static-fields-of-type=true` to revert
11-
* #32: Lazily load implementation
11+
* #32: Asynchronously load implementation
1212
* new function `java-time/load-java-time` to force loading
13+
* by default, implementation is loaded asynchronously, use system property `java-time.no-async-load=true`
14+
to disable
1315

1416
## 0.3.3
1517

src/java_time.cljc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(ns java-time (:refer-clojure :exclude (zero? range iterate max min contains? format abs)) (:require [java-time.util :as jt.u] java-time.clock java-time.util))
33
(let [lock (Object.) do-load (delay (locking lock (require 'java-time.adjuster 'java-time.amount 'java-time.chrono 'java-time.clock 'java-time.convert 'java-time.core 'java-time.format 'java-time.interval 'java-time.joda 'java-time.local 'java-time.pre-java8 'java-time.properties 'java-time.seqs 'java-time.single-field 'java-time.sugar 'java-time.temporal 'java-time.zone #?@(:bb [] :default ['java-time.mock]))))]
44
(defn load-java-time "Load java-time implementation" [] @do-load))
5-
(when *compile-files* (load-java-time))
5+
(if *compile-files* (load-java-time) (when-not (= "true" (System/getProperty "java-time.no-async-load")) (.start (Thread. (fn [] (load-java-time))))))
66
(defmacro with-clock {:doc "Executes the given `forms` in the scope of the provided `clock`.\n\n All the temporal entities that get created without parameters will inherit\n their values from the clock:\n\n (with-clock (system-clock \"Europe/London\")\n (zone-id))\n => #<java.time.ZoneRegion Europe/London>"} ([c & forms] (list* (quote java-time.clock/with-clock) c forms)))
77
(defmacro when-joda-time-loaded {:doc "Execute the `body` when Joda-Time classes are found on the classpath.\n\n Take care - when AOT-compiling code using this macro, the Joda-Time classes\n must be on the classpath at compile time!"} ([& body] (list* (quote java-time.util/when-joda-time-loaded) body)))
88
(let [+impl+ (delay (load-java-time) (deref (resolve (quote java-time.clock/with-clock-fn))))] (defn with-clock-fn {:doc "Executes the given function in the scope of the provided clock. All the\n temporal entities that get created without parameters will inherit their\n values from the clock."} ([c f] ((deref +impl+) c f))))

test/java_time/dev/gen.clj

+4-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@
173173
~@(sort require-macros)))
174174
(format "(let [lock (Object.) do-load (delay (locking lock (require %s #?@(:bb [] :default ['java-time.mock]))))]\n (defn load-java-time \"Load java-time implementation\" [] @do-load))"
175175
(str/join " " (map #(str "'" %) (sort (disj require-fns 'java-time.mock)))))
176-
'(when *compile-files* (load-java-time))]
176+
'(if *compile-files*
177+
(load-java-time)
178+
(when-not (= "true" (System/getProperty "java-time.no-async-load"))
179+
(.start (Thread. (fn [] (load-java-time))))))]
177180
(apply import-vars (:macros impl-info))
178181
(apply import-vars (:fns impl-info))
179182
(map #(list 'jt.u/when-threeten-extra %) (apply import-vars (:threeten-extra-fns impl-info))))))

0 commit comments

Comments
 (0)