eclipse-collections icon indicating copy to clipboard operation
eclipse-collections copied to clipboard

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

Open donraab opened this issue 2 years ago • 5 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

donraab avatar Apr 12 '23 05:04 donraab

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

rzrobin213 avatar Apr 12 '23 17:04 rzrobin213

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

donraab avatar Apr 12 '23 18:04 donraab

@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.

TheJavaGuy avatar May 17 '23 15:05 TheJavaGuy

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

lightgrey7 avatar Dec 01 '23 19:12 lightgrey7

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

lightgrey7 avatar Dec 06 '23 13:12 lightgrey7