Skip to content

Commit 0685a0c

Browse files
committed
[nop] Update ThreadLocal consumers
1 parent 1a88936 commit 0685a0c

File tree

1 file changed

+53
-51
lines changed

1 file changed

+53
-51
lines changed

src/taoensso/encore.cljc

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,7 +2671,7 @@
26712671

26722672
(comment (format-inst (now-inst)))
26732673

2674-
;;;; Date & time
2674+
;;;; M/secs
26752675

26762676
(defn secs->ms ^long [secs] (* (long secs) 1000))
26772677
(defn ms->secs ^long [ms] (quot (long ms) 1000))
@@ -2718,48 +2718,6 @@
27182718

27192719
(comment (macroexpand '(msecs :weeks 3)))
27202720

2721-
#?(:clj
2722-
(defn- -simple-date-format
2723-
"Returns a SimpleDateFormat ThreadLocal proxy."
2724-
[pattern locale timezone]
2725-
(let [pattern
2726-
(case pattern
2727-
:iso8601 "yyyy-MM-dd'T'HH:mm:ss.SSSX"
2728-
:rss2 "EEE, dd MMM yyyy HH:mm:ss z"
2729-
pattern)
2730-
2731-
locale
2732-
(if (identical-kw? locale :jvm-default)
2733-
nil ; (Locale/getDefault)
2734-
locale)
2735-
2736-
timezone
2737-
(if (identical-kw? timezone :jvm-default)
2738-
nil ; (TimeZone/getDefault)
2739-
(if (identical-kw? timezone :utc)
2740-
(TimeZone/getTimeZone "UTC")
2741-
timezone))]
2742-
2743-
(threadlocal
2744-
(let [^SimpleDateFormat sdf
2745-
(if locale
2746-
(SimpleDateFormat. ^String pattern ^Locale locale)
2747-
(SimpleDateFormat. ^String pattern))]
2748-
(when timezone (.setTimeZone sdf ^TimeZone timezone))
2749-
sdf)))))
2750-
2751-
#?(:clj
2752-
(defn simple-date-format*
2753-
^java.text.SimpleDateFormat [pattern locale timezone]
2754-
(.get ^ThreadLocal (-simple-date-format pattern locale timezone))))
2755-
2756-
#?(:clj
2757-
(defn simple-date-format "Returns a thread-local `java.text.SimpleDateFormat`."
2758-
^java.text.SimpleDateFormat [pattern & [{:keys [locale timezone] :as opts}]]
2759-
(.get ^ThreadLocal (-simple-date-format pattern locale timezone))))
2760-
2761-
(comment (qb 1e5 (.format (simple-date-format "yyyy-MMM-dd") (Date.))))
2762-
27632721
;;;; Strings
27642722

27652723
(def ^:const a-utf8-str
@@ -3139,7 +3097,7 @@
31393097
"Private, don't use."
31403098
[n-min-fd n-max-fd]
31413099
#?(:clj
3142-
(let [nf-proxy
3100+
(let [tl:nf
31433101
(threadlocal
31443102
(let [nf (java.text.NumberFormat/getInstance java.util.Locale/US)]
31453103
(when (instance? java.text.DecimalFormat nf)
@@ -3152,7 +3110,7 @@
31523110
(.setDecimalSeparator \.)
31533111
(.setGroupingSeparator \,)))))))]
31543112

3155-
(fn [n] (.format ^java.text.NumberFormat (.get nf-proxy) n)))
3113+
(fn [n] (.format ^java.text.NumberFormat (.get tl:nf) n)))
31563114

31573115
:cljs
31583116
(let [nf
@@ -3632,12 +3590,11 @@
36323590
(ReseedingSRNG. (java.security.SecureRandom/getInstanceStrong) 0)
36333591
(ReseedingSRNG. (java.security.SecureRandom/getInstance "SHA1SRNG") 0)))
36343592

3635-
(def ^:private rsrng* (threadlocal (reseeding-srng)))
3636-
3637-
(defn secure-rng
3638-
"Returns an auto-reseeding thread-local `java.security.SecureRandom`.
3639-
Favours security over performance. May block while waiting on entropy!"
3640-
^java.security.SecureRandom [] ((.get ^ThreadLocal rsrng*)))
3593+
(let [tl:rsrng (threadlocal (reseeding-srng))]
3594+
(defn secure-rng
3595+
"Returns an auto-reseeding thread-local `java.security.SecureRandom`.
3596+
Favours security over performance. May block while waiting on entropy!"
3597+
^java.security.SecureRandom [] ((.get tl:rsrng))))
36413598

36423599
(defn secure-rng-mock!!!
36433600
"Returns **INSECURE** `java.security.SecureRandom` mock instance backed by
@@ -4993,6 +4950,51 @@
49934950

49944951
(comment (let [rl (rolling-list 3), c (counter)] [(qb 1e6 (rl (c))) (vec (rl))])) ; 98.36
49954952

4953+
;;;; Date & time
4954+
4955+
#?(:clj
4956+
(let [get-tl
4957+
(fmemoize
4958+
(fn [pattern locale timezone]
4959+
(threadlocal
4960+
(let [pattern
4961+
(case pattern
4962+
:iso8601 "yyyy-MM-dd'T'HH:mm:ss.SSSX"
4963+
:rss2 "EEE, dd MMM yyyy HH:mm:ss z"
4964+
pattern)
4965+
4966+
locale
4967+
(if (identical-kw? locale :jvm-default)
4968+
nil #_(Locale/getDefault)
4969+
locale)
4970+
4971+
timezone
4972+
(if (identical-kw? timezone :jvm-default)
4973+
nil #_(TimeZone/getDefault)
4974+
(if (identical-kw? timezone :utc)
4975+
(TimeZone/getTimeZone "UTC")
4976+
timezone))
4977+
4978+
^SimpleDateFormat sdf
4979+
(if locale
4980+
(SimpleDateFormat. ^String pattern ^Locale locale)
4981+
(SimpleDateFormat. ^String pattern))]
4982+
4983+
(when timezone (.setTimeZone sdf ^TimeZone timezone))
4984+
(do sdf)))))]
4985+
4986+
(defn simple-date-format*
4987+
"Returns a thread-local `java.text.SimpleDateFormat`."
4988+
^java.text.SimpleDateFormat [pattern locale timezone]
4989+
(.get ^ThreadLocal (get-tl pattern locale timezone)))
4990+
4991+
(defn simple-date-format
4992+
"Returns a thread-local `java.text.SimpleDateFormat`."
4993+
^java.text.SimpleDateFormat [pattern & [{:keys [locale timezone] :as opts}]]
4994+
(.get ^ThreadLocal (get-tl pattern locale timezone)))))
4995+
4996+
(comment (qb 1e5 (.format (simple-date-format "yyyy-MMM-dd") (Date.))))
4997+
49964998
;;;; Sorting
49974999

49985000
#?(:cljs (defn rcompare "Reverse comparator." [x y] (compare y x))

0 commit comments

Comments
 (0)