eclipse / eclipse-collections

Eclipse Collections is a collections framework for Java with optimized data structures and a rich, functional and fluent API.

Home Page:http://www.eclipse.org/collections

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The result types in sumOfPrimitiveProcedure.stg need to be widened to prevent overflow

donraab opened this issue · comments

I discovered there is an overflow error using sumOfInt in the following code when the batch size exceeded 536

This code returned the correct sum:

long sum = Interval.oneTo(2_000_000)
        .toList()
        .asParallel(Executors.newSingleThreadExecutor(), 536)
        .sumOfInt(each -> each * 2);

This code returned the incorrect sum:

long sum = Interval.oneTo(2_000_000)
        .toList()
        .asParallel(Executors.newSingleThreadExecutor(), 537)
        .sumOfInt(each -> each * 2);

I tracked the problem down to SumOfIntProcedure returning int instead of long for the result. This Procedure must be used in the individual batches and if a batch gets large enough with large values being summed, overflow is occurring.

https://github.com/eclipse/eclipse-collections/blob/master/eclipse-collections-code-generator/src/main/resources/impl/block/procedure/sumOfPrimitiveProcedure.stg#L33

Hi, I would like to take up this issue. Could you assign it to me? Thank you!

Thanks for volunteering @rzrobin213 ! I have assigned the issue to you.

@donraab File eclipse-collections/src/main/java/org/eclipse/collections/impl/block/procedure/SumOfIntProcedure.java seems not to be auto-generated. OTOH, file eclipse-collections/target/generated-sources/java/org/eclipse/collections/impl/block/procedure/SumOfByteProcedure.java says that it is generated from the sumOfPrimitiveProcedure.stg.

Hi! Could you assign this issue to me, since rzrobin123 do not seem to be working on this.

hello!! I have made a pull request regarding this issue..please review it.
@donraab