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:
Andy Fingerhut
July 10, 2013, 2:15 AM

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.

John Jacobsen
July 10, 2013, 4:35 AM

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.

John Jacobsen
July 10, 2013, 4:52 AM

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=>

Alex Miller
July 12, 2013, 7:26 AM

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.

Alex Miller
August 3, 2015, 12:27 PM

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

Duplicate

Assignee

Unassigned

Reporter

Andy Fingerhut

Labels

Approval

None

Patch

Code

Affects versions

Priority

Minor