Skip to content

Commit 86c7726

Browse files
authored
Merge pull request #64 from valerauko/develop/boxed-math
Resolve boxed math warnings
2 parents e48da8b + bde5da2 commit 86c7726

File tree

4 files changed

+36
-32
lines changed

4 files changed

+36
-32
lines changed

project.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
:profiles {:dev {:dependencies [[org.clojure/clojure "1.11.1"]
1414
[org.clojure/test.check "1.1.1"]
1515
[rhizome "0.2.9"]
16-
[criterium "0.4.6"]]}
16+
[criterium "0.4.6"]]
17+
:global-vars {*warn-on-reflection* true
18+
*unchecked-math* :warn-on-boxed}}
1719
:ci {:dependencies [[org.clojure/clojure "1.11.1"]
1820
[org.clojure/test.check "1.1.1"]]}}
1921
:test-selectors {:stress :stress

src/byte_streams/graph.clj

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
[manifold.stream :as s]
1010
[byte-streams
1111
[utils :refer [defprotocol+ defrecord+ deftype+]]
12-
[protocols :as p]])
12+
[protocols :as proto]]
13+
[clj-commons.primitive-math :as p])
1314
(:import
1415
[java.util
1516
LinkedList
@@ -25,7 +26,7 @@
2526
(identical? f (.f ^Conversion x))
2627
(== cost (.cost ^Conversion x))))
2728
(hashCode [_]
28-
(bit-xor (System/identityHashCode f) (unchecked-int cost))))
29+
(p/bit-xor (System/identityHashCode f) (unchecked-int cost))))
2930

3031
(deftype+ Type [wrapper type]
3132
Object
@@ -35,7 +36,7 @@
3536
(= wrapper (.wrapper ^Type x))
3637
(= type (.type ^Type x))))
3738
(hashCode [_]
38-
(bit-xor
39+
(p/bit-xor
3940
(hash wrapper)
4041
(hash type)))
4142
(toString [this]
@@ -131,7 +132,7 @@
131132
(assoc-in [(Type. 'seq src) (Type. 'seq dst)]
132133
(Conversion. (fn [x options] (map #(f % options) x)) cost))
133134
(assoc-in [(Type. 'stream src) (Type. 'stream dst)]
134-
(Conversion. (fn [x options] (s/map #(f % options) x)) (+ cost 0.1)))))
135+
(Conversion. (fn [x options] (s/map #(f % options) x)) (p/+ ^double cost 0.1)))))
135136
m')]
136137
(ConversionGraph. m')))
137138
(possible-sources [_]
@@ -160,7 +161,7 @@
160161

161162
;;;
162163

163-
(defrecord+ ConversionPath [path fns visited? cost]
164+
(defrecord+ ConversionPath [path fns visited? ^double cost]
164165
Comparable
165166
(compareTo [_ x]
166167
(let [cmp (compare cost (.cost ^ConversionPath x))]
@@ -173,7 +174,7 @@
173174
(conj (.path p) [src dst])
174175
(conj (.fns p) (.f c))
175176
(conj (.visited? p) dst)
176-
(+ (.cost p) (.cost c))))
177+
(p/+ (.cost p) (.cost c))))
177178

178179
(def conversion-path
179180
(memoize
@@ -242,11 +243,11 @@
242243
0 (fn [x _] x)
243244

244245
1 (let [f (->> path :fns first)]
245-
(if (p/closeable? src)
246+
(if (proto/closeable? src)
246247
(fn [x options]
247248
(let [x' (f x options)]
248-
(when-not (p/closeable? x')
249-
(p/close x))
249+
(when-not (proto/closeable? x')
250+
(proto/close x))
250251
x'))
251252
f))
252253

@@ -258,12 +259,12 @@
258259
(fn [x f]
259260

260261
;; keep track of everything that needs to be closed once the bytes are exhausted
261-
(when (p/closeable? x)
262-
(.add close-fns #(p/close x)))
262+
(when (proto/closeable? x)
263+
(.add close-fns #(proto/close x)))
263264
(f x options))
264265
x
265266
fns)]
266-
(if-let [close-fn (when-not (or (p/closeable? result)
267+
(if-let [close-fn (when-not (or (proto/closeable? result)
267268
(.isEmpty close-fns))
268269
#(loop []
269270
(when-let [f (.poll close-fns)]
@@ -284,7 +285,7 @@
284285
;; we assume that if the end-result is closeable, it will take care of all the intermediate
285286
;; objects beneath it. I think this is true as long as we're not doing multiple streaming
286287
;; reads, but this might need to be revisited.
287-
(when-not (p/closeable? result)
288+
(when-not (proto/closeable? result)
288289
(close-fn))
289290
result))
290291
result)))))))

src/clj_commons/byte_streams.clj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,15 +429,15 @@
429429

430430
;; byte-buffer => sequence of byte-buffers
431431
(def-conversion ^{:cost 0} [ByteBuffer (vector-of ByteBuffer)]
432-
[buf {:keys [chunk-size]}]
432+
[buf {:keys [^long chunk-size]}]
433433
(if chunk-size
434434
(let [lim (.limit buf)
435435
indices (range (.position buf) lim chunk-size)]
436436
(mapv
437437
#(-> buf
438438
.duplicate
439439
^ByteBuffer (.position (int %))
440-
^ByteBuffer (.limit (int (min lim (+ (int %) chunk-size))))
440+
^ByteBuffer (.limit (int (min lim (p/+ (int %) chunk-size))))
441441
.slice)
442442
indices))
443443
[buf]))
@@ -587,7 +587,7 @@
587587
;;
588588
(def-conversion ^{:cost 0
589589
:doc "Assumes the file size is static."} [File (seq-of ByteBuffer)]
590-
[file {:keys [chunk-size writable?] :or {chunk-size (int 2e9), writable? false}}]
590+
[file {:keys [^long chunk-size writable?] :or {chunk-size (int 2e9), writable? false}}]
591591
(let [option-array (into-array StandardOpenOption
592592
(cond-> [StandardOpenOption/READ]
593593
writable?
@@ -597,7 +597,7 @@
597597
(.toPath)
598598
(FileChannel/open option-array))
599599
close-fn #(.close fc)
600-
buf-seq (fn buf-seq [offset]
600+
buf-seq (fn buf-seq [^long offset]
601601
(when (and (.isOpen fc)
602602
(> (.size fc) offset))
603603
(let [remaining (- (.size fc) offset)
@@ -732,7 +732,7 @@
732732
(.flip buf))))
733733

734734
ByteBuffer
735-
(take-bytes! [this n _]
735+
(take-bytes! [this ^long n _]
736736
(when (pos? (.remaining this))
737737
(let [n (int (min (.remaining this) n))
738738
buf (-> this
@@ -853,7 +853,7 @@
853853

854854
(defn ^String to-string
855855
"Converts the object to a string.
856-
Will consume all input it can.
856+
Will consume all input it can.
857857
Consider using to-reader, to-line-seq, or to-char-sequence for lazy / partial input consumption."
858858
([x]
859859
(to-string x nil))

src/clj_commons/byte_streams/graph.clj

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
[manifold.stream :as s]
55
[clj-commons.byte-streams
66
[utils :refer [defprotocol+ defrecord+ deftype+]]
7-
[protocols :as p]])
7+
[protocols :as proto]]
8+
[clj-commons.primitive-math :as p])
89
(:import
910
[java.util
1011
LinkedList
@@ -20,7 +21,7 @@
2021
(identical? f (.f ^Conversion x))
2122
(== cost (.cost ^Conversion x))))
2223
(hashCode [_]
23-
(bit-xor (System/identityHashCode f) (unchecked-int cost))))
24+
(p/bit-xor (System/identityHashCode f) (unchecked-int cost))))
2425

2526
(deftype+ Type [wrapper type]
2627
Object
@@ -30,7 +31,7 @@
3031
(= wrapper (.wrapper ^Type x))
3132
(= type (.type ^Type x))))
3233
(hashCode [_]
33-
(bit-xor
34+
(p/bit-xor
3435
(hash wrapper)
3536
(hash type)))
3637
(toString [this]
@@ -126,7 +127,7 @@
126127
(assoc-in [(Type. 'seq src) (Type. 'seq dst)]
127128
(Conversion. (fn [x options] (map #(f % options) x)) cost))
128129
(assoc-in [(Type. 'stream src) (Type. 'stream dst)]
129-
(Conversion. (fn [x options] (s/map #(f % options) x)) (+ cost 0.1)))))
130+
(Conversion. (fn [x options] (s/map #(f % options) x)) (p/+ ^double cost 0.1)))))
130131
m')]
131132
(ConversionGraph. m')))
132133
(possible-sources [_]
@@ -155,7 +156,7 @@
155156

156157
;;;
157158

158-
(defrecord+ ConversionPath [path fns visited? cost]
159+
(defrecord+ ConversionPath [path fns visited? ^double cost]
159160
Comparable
160161
(compareTo [_ x]
161162
(let [cmp (compare cost (.cost ^ConversionPath x))]
@@ -234,11 +235,11 @@
234235
0 (fn [x _] x)
235236

236237
1 (let [f (->> path :fns first)]
237-
(if (p/closeable? src)
238+
(if (proto/closeable? src)
238239
(fn [x options]
239240
(let [x' (f x options)]
240-
(when-not (p/closeable? x')
241-
(p/close x))
241+
(when-not (proto/closeable? x')
242+
(proto/close x))
242243
x'))
243244
f))
244245

@@ -250,12 +251,12 @@
250251
(fn [x f]
251252

252253
;; keep track of everything that needs to be closed once the bytes are exhausted
253-
(when (p/closeable? x)
254-
(.add close-fns #(p/close x)))
254+
(when (proto/closeable? x)
255+
(.add close-fns #(proto/close x)))
255256
(f x options))
256257
x
257258
fns)]
258-
(if-let [close-fn (when-not (or (p/closeable? result)
259+
(if-let [close-fn (when-not (or (proto/closeable? result)
259260
(.isEmpty close-fns))
260261
#(loop []
261262
(when-let [f (.poll close-fns)]
@@ -276,7 +277,7 @@
276277
;; we assume that if the end-result is closeable, it will take care of all the intermediate
277278
;; objects beneath it. I think this is true as long as we're not doing multiple streaming
278279
;; reads, but this might need to be revisited.
279-
(when-not (p/closeable? result)
280+
(when-not (proto/closeable? result)
280281
(close-fn))
281282
result))
282283
result)))))))

0 commit comments

Comments
 (0)