RFR: 8371164: ArrayList.addAll() optimizations [v2]

Claes Redestad redestad at openjdk.org
Tue Nov 4 20:20:56 UTC 2025


On Tue, 4 Nov 2025 20:03:28 GMT, jengebr <duke at openjdk.org> wrote:

>>> cl4es: Consider simplifying this to reduce code duplication:
>> 
>> or even simpler?
>> 
>> 
>> Object[] a = c.getClass() == ArrayList.class ? ((ArrayList<?>)c).elementData : a.toArray();
>> int newNum = a.length;
>> ...
>> 
>> 
>> 
>>> jengebr: Interestingly, this hurt the fast-path about 5%, but the control case about 35%.
>> 
>> what is the control case, LinkedList.addAll() ?
>
> No, ArrayList.addAll(LinkedList).  This is the slow path that ideally will perform the same as before this change - and the initial change achieves that.  Rewriting to reduce duplication increased runtime of both the optimized and control.
> 
> I'll benchmark your suggestion as well.

That variant is incorrect since `size <= a.length`, potentially adding a surprising tail of `null` elements to the list.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/28116#discussion_r2491936650


More information about the core-libs-dev mailing list