RFR: 8305774: String.join(CharSequence, Iterable) can be optimized if Iterable is a Collection
Tingjun Yuan
duke at openjdk.org
Sun Apr 9 02:28:38 UTC 2023
On Fri, 7 Apr 2023 08:27:18 GMT, Tingjun Yuan <duke 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.
I've just added the benchmark code, and here is the result tested on my computer.
Before change:
Benchmark Mode Cnt Score Error Units
StringJoinWithIterable.joinWithArrayList avgt 30 146820983.745 ± 6654877.812 ns/op
StringJoinWithIterable.joinWithIterable avgt 30 145236859.688 ± 5683967.478 ns/op
StringJoinWithIterable.joinWithLinkedBlockingDeque avgt 30 250139433.967 ± 14381158.389 ns/op
StringJoinWithIterable.joinWithLinkedHashSet avgt 30 167718287.313 ± 7394905.875 ns/op
StringJoinWithIterable.joinWithLinkedTransferQueue avgt 30 153245459.832 ± 7972716.070 ns/op
After change:
Benchmark Mode Cnt Score Error Units
StringJoinWithIterable.joinWithArrayList avgt 30 138343729.406 ± 5959140.498 ns/op
StringJoinWithIterable.joinWithIterable avgt 30 149566458.195 ± 9755905.704 ns/op
StringJoinWithIterable.joinWithLinkedBlockingDeque avgt 30 247384811.927 ± 14647765.134 ns/op
StringJoinWithIterable.joinWithLinkedHashSet avgt 30 153244492.767 ± 4589884.301 ns/op
StringJoinWithIterable.joinWithLinkedTransferQueue avgt 30 157162685.594 ± 4330658.539 ns/op
It seems that the improvement is not notable :-)
-------------
PR Comment: https://git.openjdk.org/jdk/pull/13383#issuecomment-1501022549
More information about the core-libs-dev
mailing list