range should return something that implements IReduce
range should implement IReduce for fast reduction.
Patch: clj-1515-14.patch - Java range with long optimizations
Approach: The clj-1515-14 patch revives the unused clojure.lang.Range class. This class is similar in implementation to ChunkedCons. It differs by lazily constructing the first chunk (this is done in the existing impl by using a LazySeq wrapper) and by caching the next reference like other seq impls. The latter is done because range is frequently used in both chunked and unchunked traversal.
Additionally, 1515-14 contains an optimized version of range (LongRange) for the extremely common case where start, end, and step are all longs. This version uses primitive longs and primitive math and a customized version of ArrayChunk for greater performance.
The special case of (range) is just handled with (iterate inc' 0) (which was further optimized for reduce in CLJ-1603).
CLJ-1515-deftype3.patch took the approach of using deftype to create a seqable, reducible entity that was not actually a seq. Based on work for CLJ-1603, it was established that due to historical requirements, this is not viable and the entity returned for range should implement the ISeq and Collection interfaces directly.
clj-1515-11.patch uses the approach of a "split" implementation - a LazySeq that has a fast reduce path. This is a minimal patch that provides no perf difference for sequence usage but a moderate improvement for reduce paths. One important difference vs clj-1515-12 is that the fast reduce path is only obtained on the initial range. Once you walk off the head, the performance will be the same as prior (seq perf).
criterium quick-bench with java 1.8.0-b132
(count (range (* 1024 1024)))
(reduce + (map inc (range (* 1024 1024))))
(reduce + (map inc (map inc (range (* 1024 1024)))))
(count (keep odd? (range (* 1024 1024))))
(transduce (comp (map inc) (map inc)) + (range (* 1024 1024)))
(reduce + 0 (range (* 2048 1024)))
(reduce + 0 (rest (range (* 2048 1024))))
(doall (range 0 31))
(doall (range 0 32))
(doall (range 0 4096))
(into  (map inc (range 31)))
(into  (map inc) (range 31))
(into  (range 128))
(doall (range 1/2 1000 1/3))
(doall (range 0.5 1000 0.33))
(into  (range 1/2 1000 1/3))
(into  (range 0.5 1000 0.33))
(count (filter odd? (take (* 1024 1024) (range))))
(transduce (take (* 1024 1024)) + (range))
1515-14 (Java) - significant improvements in virtually all use cases, both seq and reduce. The final two cases with (range) leverage optimizations from CLJ-1603.
1515-11 (split) - a much smaller patch that gives no seq benefits and smaller reduction benefits. Only the head of the range will receive the perf benefits (see (reduce + 0 (rest (range (* 2048 1024))))).
(range) and supports auto-promotion towards infinity in this patch, which seems to be implied by the doc string but was not actually implemented or tested correctly afaict.
The latest patch (-14) handles the extraneous allocations from -12. All tests run and timings verified.
The -14 patch just switches to leverage Repeat now that 1603 has been applied.
New patch -13 removes the array allocation and performs better, still need to update timings and consider the non-long case.
Screened. Though the amount of duplicated code saddens me, I don't hold it against the implementer. It's a fairly straight-forward counting Impl with a chunk cache.
current direction is pending results of where CLJ-1603 goes