count silently overflows for sequences with more than Integer/MAX_VALUE elements

Description

Found by John Jacobsen: https://mail.google.com/mail/?shva=1#label/clojure/13fbba0c3e4ba6b7

user> (time (count (range (*' 1000 1000 1000 3))))
"Elapsed time: 375225.663 msecs"
-1294967296

Environment

None

Activity

Show:
Alex Miller
August 3, 2015, 2:27 AM

Dupe to (similar change logged at Rich's request)

Alex Miller
July 11, 2013, 9:26 PM

Perhaps of interest, Java's Collection.size() returns Integer.MAX_VALUE if the size of the collection > MAX_VALUE. I can't say that either that behavior or overflow is particularly helpful in practice of course.

John Jacobsen
July 9, 2013, 6:52 PM

FWIW I did apply the patch, build, and test manually:

user=> (count (range (* 1000 1000 1000 3)))
ArithmeticException integer overflow clojure.lang.RT.countFrom (RT.java:549)
user=>

John Jacobsen
July 9, 2013, 6:35 PM

I agree that Mike's approach is nicer overall, but think Andy's patch is an immediate improvement over what we have now, and could be implemented until someone takes the time to correctly make all the detailed changes Mike is suggesting.

Andy Fingerhut
July 9, 2013, 4:15 PM

Mike, unless I am missing something, that would require changing the method count() in the Counted interface to return a long, and that in turn requires little changes throughout the code base wherever Counted is used. It could be done, of course, but it is not a small change.

Duplicate
Your pinned fields
Click on the next to a field label to start pinning.

Assignee

Unassigned

Reporter

Andy Fingerhut

Labels

Patch

Code

Priority

Minor

Affects versions