RFR: 8305774: String.join(CharSequence, Iterable) can be optimized if Iterable is a Collection
Tingjun Yuan
duke at openjdk.org
Sat Apr 8 23:31:40 UTC 2023
On Sat, 8 Apr 2023 23:18:04 GMT, Chen Liang <liach at openjdk.org> wrote:
>> In the current implementation of `String.join(CharSequence, Iterable)`, the temp array `elems` is always initialized with a length of 8. It will cause many array recreations when the `Iterable` contains more than 8 elements. Furthermore, it's very common that an `Iterable` is also a `Collection`. So if the `Iterable` is an instance of `Collection`, the initial length of the array can be `((Collection<?>)elements).size()`. It will not change the current behavior even if the `Collection` is modified asynchronously.
>>
>> I don't know whether this change requires a CSR request.
>
> Have you considered the performance impact on concurrent collections, that their size() computation is non-constant, and the array still may need resizing later (which you've kept, good)
@liach Thanks for reminding that. Some collections such as `java.util.concurrent.ConcurrentLinkedDeque` require O(n) time to compute the `size`. I will benchmark it as well.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/13383#issuecomment-1500996891
More information about the core-libs-dev
mailing list