Skip to content

Commit 5435801

Browse files
authored
Merge pull request #234 from valerauko/develop/boxed-math
Resolve boxed math warnings
2 parents b1e0613 + db1e927 commit 5435801

File tree

11 files changed

+132
-119
lines changed

11 files changed

+132
-119
lines changed

project.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
:dependencies [[org.clojure/clojure "1.11.1" :scope "provided"]
88
[org.clojure/tools.logging "1.2.4" :exclusions [org.clojure/clojure]]
99
[org.clj-commons/dirigiste "1.0.3"]
10+
[org.clj-commons/primitive-math "1.0.0"]
1011
[riddley "0.2.0"]
1112
[org.clojure/core.async "1.6.673" :scope "provided"]
1213
[potemkin "0.4.6"]]
13-
:profiles {:dev {:dependencies [[criterium "0.4.6"]]}
14+
:profiles {:dev {:dependencies [[criterium "0.4.6"]]
15+
:global-vars {*warn-on-reflection* true
16+
*unchecked-math* :warn-on-boxed}}
1417
;; core.async moved around some internal functions go-off relies on; this profile
1518
;; helps test that go-off still works both with the new namespaces and the old
1619
:older-core-async {:dependencies [[org.clojure/core.async "1.5.648" :scope "provided"]]}}

src/manifold/bus.clj

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
[stream :as s]
77
[deferred :as d]
88
[utils :refer [definterface+]]]
9-
[potemkin.types :refer [deftype+]])
9+
[potemkin.types :refer [deftype+]]
10+
[clj-commons.primitive-math :as p])
1011
(:import
1112
[java.util.concurrent
1213
ConcurrentHashMap]
@@ -53,25 +54,25 @@
5354
(if (nil? ary)
5455
(object-array [x])
5556
(let [len (Array/getLength ary)
56-
ary' (object-array (inc len))]
57+
ary' (object-array (p/inc len))]
5758
(System/arraycopy ary 0 ary' 0 len)
5859
(aset ^objects ary' len x)
5960
ary')))
6061

6162
(defn- disj' [^objects ary x]
6263
(let [len (Array/getLength ary)]
6364
(if-let [idx (loop [i 0]
64-
(if (<= len i)
65+
(if (p/<= len i)
6566
nil
6667
(if (identical? x (aget ary i))
6768
i
68-
(recur (inc i)))))]
69-
(let [idx (long idx)]
70-
(if (== 1 len)
69+
(recur (p/inc i)))))]
70+
(let [idx (p/long idx)]
71+
(if (p/== 1 len)
7172
nil
72-
(let [ary' (object-array (dec len))]
73+
(let [ary' (object-array (p/dec len))]
7374
(System/arraycopy ary 0 ary' 0 idx)
74-
(System/arraycopy ary (inc idx) ary' idx (- len idx 1))
75+
(System/arraycopy ary (p/inc idx) ary' idx (p/- len idx 1))
7576
ary')))
7677
ary)))
7778

src/manifold/deferred.clj

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
[manifold.time :as time]
1111
[manifold.utils :as utils :refer [assert-some definterface+]]
1212
[potemkin.types :refer [def-abstract-type reify+ defprotocol+ deftype+]]
13+
[clj-commons.primitive-math :as p]
1314
[riddley.compiler :as compiler]
1415
[riddley.walk :as walk])
1516
(:import
@@ -689,7 +690,7 @@
689690
([]
690691
(deferred (ex/executor)))
691692
([executor]
692-
(if (and (zero? (rem (.incrementAndGet created) 1024))
693+
(if (and (p/zero? (rem (.incrementAndGet created) 1024))
693694
debug/*dropped-error-logging-enabled?*)
694695
(LeakAwareDeferred. nil ::unset nil (utils/mutex) (LinkedList.) nil false executor)
695696
(Deferred. nil ::unset nil (utils/mutex) (LinkedList.) nil false executor)))))
@@ -703,8 +704,8 @@
703704
#_{:inline
704705
(fn [val]
705706
(cond
706-
(true? val) 'manifold.deferred/true-deferred-
707-
(false? val) 'manifold.deferred/false-deferred-
707+
(p/true? val) 'manifold.deferred/true-deferred-
708+
(p/false? val) 'manifold.deferred/false-deferred-
708709
(nil? val) 'manifold.deferred/nil-deferred-
709710
:else `(SuccessDeferred. ~val nil nil)))
710711
:inline-arities #{1}}
@@ -1026,7 +1027,7 @@
10261027
"Like `chain`, but does not coerce deferrable values. This is useful both when coercion
10271028
is undesired, or for 2-4x better performance than `chain`."
10281029
{:inline (fn [& args]
1029-
(if false #_(< 3 (count args))
1030+
(if false #_(p/< 3 (count args))
10301031
(apply unroll-chain 'manifold.deferred/unwrap' args)
10311032
`(chain'- nil ~@args)))}
10321033
([x]
@@ -1046,12 +1047,12 @@
10461047
The returned deferred will only be realized once all functions have been applied and their
10471048
return values realized.
10481049
1049-
@(chain 1 inc #(future (inc %))) => 3
1050+
@(chain 1 inc #(future (p/inc %))) => 3
10501051
10511052
@(chain (future 1) inc inc) => 3
10521053
"
10531054
{:inline (fn [& args]
1054-
(if false #_(< 3 (count args))
1055+
(if false #_(p/< 3 (count args))
10551056
(apply unroll-chain 'manifold.deferred/unwrap args)
10561057
`(chain- nil ~@args)))}
10571058
([x]
@@ -1153,7 +1154,7 @@
11531154
(defn finally
11541155
"An equivalent of the finally clause, which takes a no-arg side-effecting function that executes
11551156
no matter what the result.
1156-
1157+
11571158
Returns x unless a Throwable is thrown in f, in which case it returns an error-deferred."
11581159
[x f]
11591160
(if-let [d (->deferred x nil)]
@@ -1174,7 +1175,7 @@
11741175

11751176
;; no further results, decrement the counter one last time
11761177
;; and return the result if everything else has been realized
1177-
(if (zero? (.get counter))
1178+
(if (p/zero? (.get counter))
11781179
(success-deferred (or (seq ary) (list)))
11791180
d)
11801181

@@ -1196,7 +1197,7 @@
11961197
(on-realized (chain' x)
11971198
(fn [val]
11981199
(aset ary idx val)
1199-
(when (zero? (.decrementAndGet counter))
1200+
(when (p/zero? (.decrementAndGet counter))
12001201
(success! d (seq ary))))
12011202
(fn [err]
12021203
(error! d err)))
@@ -1228,10 +1229,10 @@
12281229
(clojure.core/loop [i 1]
12291230
(if (= i n)
12301231
a
1231-
(let [j (rand-int (inc i))]
1232+
(let [j (rand-int (p/inc i))]
12321233
(aset a i (aget a j))
12331234
(aset a j i)
1234-
(recur (inc i)))))))
1235+
(recur (p/inc i)))))))
12351236

12361237
(defn alt'
12371238
"Like `alt`, but only unwraps Manifold deferreds."
@@ -1240,7 +1241,7 @@
12401241
cnt (count vals)
12411242
^ints idxs (random-array cnt)]
12421243
(clojure.core/loop [i 0]
1243-
(when (< i cnt)
1244+
(when (p/< i cnt)
12441245
(let [i' (aget idxs i)
12451246
x (nth vals i')]
12461247
(if (deferred? x)
@@ -1250,7 +1251,7 @@
12501251
(do (on-realized (chain' x)
12511252
#(success! d %)
12521253
#(error! d %))
1253-
(recur (inc i))))
1254+
(recur (p/inc i))))
12541255
(success! d x)))))
12551256
d))
12561257

@@ -1324,9 +1325,9 @@
13241325
13251326
(loop [i 1e6]
13261327
(chain (future i)
1327-
#(if (zero? %)
1328+
#(if (p/zero? %)
13281329
%
1329-
(recur (dec %)))))"
1330+
(recur (p/dec %)))))"
13301331
[bindings & body]
13311332
(let [vars (->> bindings (partition 2) (map first))
13321333
vals (->> bindings (partition 2) (map second))
@@ -1479,15 +1480,15 @@
14791480
Returns a deferred value, representing the value returned by the body.
14801481
14811482
(let-flow [x (future 1)]
1482-
(+ x 1))
1483+
(p/+ x 1))
14831484
14841485
(let-flow [x (future 1)
1485-
y (future (+ x 1))]
1486-
(+ y 1))
1486+
y (future (p/+ x 1))]
1487+
(p/+ y 1))
14871488
14881489
(let [x (future 1)]
1489-
(let-flow [y (future (+ x 1))]
1490-
(+ y 1)))"
1490+
(let-flow [y (future (p/+ x 1))]
1491+
(p/+ y 1)))"
14911492
[bindings & body]
14921493
(expand-let-flow
14931494
'manifold.deferred/chain

src/manifold/executor.clj

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns manifold.executor
22
(:require
3-
[clojure.tools.logging :as log])
3+
[clojure.tools.logging :as log]
4+
[clj-commons.primitive-math :as p])
45
(:import
56
[io.aleph.dirigiste
67
Executors
@@ -87,9 +88,9 @@
8788
(when (contains? stats Stats$Metric/QUEUE_LENGTH)
8889
{:queue-length (q #(.getQueueLength s %))})
8990
(when (contains? stats Stats$Metric/QUEUE_LATENCY)
90-
{:queue-latency (q #(double (/ (.getQueueLatency s %) 1e6)))})
91+
{:queue-latency (q #(p/double (p// (.getQueueLatency s %) 1e6)))})
9192
(when (contains? stats Stats$Metric/TASK_LATENCY)
92-
{:task-latency (q #(double (/ (.getTaskLatency s %) 1e6)))})
93+
{:task-latency (q #(p/double (p// (.getTaskLatency s %) 1e6)))})
9394
(when (contains? stats Stats$Metric/TASK_ARRIVAL_RATE)
9495
{:task-arrival-rate (q #(.getTaskArrivalRate s %))})
9596
(when (contains? stats Stats$Metric/TASK_COMPLETION_RATE)
@@ -115,7 +116,7 @@
115116
| `initial-thread-count` | the number of threads that the pool should begin with.
116117
| `onto?` | if true, all streams and deferred generated in the scope of this executor will also be 'on' this executor."
117118
[{:keys [thread-factory
118-
queue-length
119+
^long queue-length
119120
stats-callback
120121
sample-period
121122
control-period
@@ -147,7 +148,7 @@
147148
(Executor.
148149
thread-factory
149150
(if (and queue-length (pos? queue-length))
150-
(if (<= queue-length 1024)
151+
(if (p/<= queue-length 1024)
151152
(ArrayBlockingQueue. queue-length false)
152153
(LinkedBlockingQueue. (int queue-length)))
153154
(SynchronousQueue. false))
@@ -169,17 +170,17 @@
169170
"Returns an executor which has a fixed number of threads."
170171
([num-threads]
171172
(fixed-thread-executor num-threads nil))
172-
([num-threads options]
173+
([^long num-threads options]
173174
(instrumented-executor
174175
(-> options
175176
(update-in [:queue-length] #(or % Integer/MAX_VALUE))
176177
(assoc
177178
:max-threads num-threads
178179
:controller (reify Executor$Controller
179180
(shouldIncrement [_ n]
180-
(< n num-threads))
181+
(p/< n num-threads))
181182
(adjustment [_ s]
182-
(- num-threads (.getNumWorkers s)))))))))
183+
(p/- num-threads (.getNumWorkers s)))))))))
183184

184185
(defn utilization-executor
185186
"Returns an executor which sizes the thread pool according to target utilization, within

0 commit comments

Comments
 (0)