Welcome! Please see the About page for a little more info on how this works.

0 votes
ago in Sequences by
retagged ago by

Clojure 1.12.7, JDK 21.

reduce over a dropped vector-of never returns once the vector has 65505 or more elements. A regular vector works.

(reduce + (drop 1 (into (vector-of :long) (range 65504))))
;; => 2145354256
(reduce + (drop 1 (into (vector-of :long) (range 65505))))
;; never returns
(reduce + (drop 1 (vec (range 65505))))
;; => 2145419760

The same happens on other paths that reduce a VecSeq wrapped in a LazySeq (into, transduce, run!, reduce after lazy-seq). (reduce f (seq v)) goes through the iterator and is not affected.

The cause is the chunk step in VecSeq's internal-reduce, gvec.clj line 79:

(recur result (bit-and 0xffe0 (+ aidx 32)))

The mask is meant to round down to a multiple of 32, but 0xffe0 also clears bit 16 and above. After the chunk that starts at index 65504, the next chunk start should be 65536, but the mask turns it into 0, so the reduce starts over from the beginning forever. A mask that only clears the low 5 bits, such as (bit-not 0x1f), keeps the index.

The mask has been there since commit 5b281880 (2010), which added internal-reduce.

This bug is reported at https://s.conao3.com/blog/2026/e4f733

Please log in or register to answer this question.

...