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

Attachments

1

Activity

Show:

Alex Miller August 3, 2015 at 2:27 AM

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

Alex Miller July 11, 2013 at 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 at 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 at 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 at 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

Details

Assignee

Reporter

Labels

Patch

Code

Priority

Affects versions

Created July 9, 2013 at 7:36 AM
Updated August 3, 2015 at 2:27 AM
Resolved August 3, 2015 at 2:27 AM