Early overflow on resize GapList

25 views
Skip to first unread message

Mythos

unread,
Nov 3, 2021, 7:54:24 AM11/3/21
to brownies-collections
Hi Thomas,

thanks for this great work! :-)

I found the following issue that leads to unnecessary bad resize behaviour for large lists:

'GapList#calculateNewCapacity(int)' produces an (too early) overflow for 'oldCapacity'. For all capacities larger than 'Integer.MAX_VALUE / 3 + 1' the GapList will be resized by one (!) element. This has significant impact on adding new elements performance.

int oldCapacity = Integer.MAX_VALUE / 3 + 1;
int newCapacity = (oldCapacity * 3) / 2 + 1;
System.out.println(newCapacity); // -> -1073741822 -> OVERFLOW

A possible improvement looks like that ...

int oldCapacity = Integer.MAX_VALUE / 3 + 1;
int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
System.out.println(newCapacity); // -> 1073741825

Kind Regards!

Thomas Mauch

unread,
Jul 29, 2022, 1:01:24 PM7/29/22
to brownies-collections
Thanks for pointing this out!
Is is implemented in the newest 0.9.18 release, see https://github.com/magicwerk/brownies-collections/

Thanks,
Thomas
Reply all
Reply to author
Forward
0 new messages