RFR: 8301958: Reduce Arrays.copyOf/-Range overheads [v9]

Alan Bateman alanb at openjdk.org
Fri Feb 10 17:02:56 UTC 2023


On Wed, 8 Feb 2023 10:53:22 GMT, Claes Redestad <redestad at openjdk.org> wrote:

>> This patch adds special-cases to `Arrays.copyOf` and `Arrays.copyOfRange` to copy arrays more efficiently when exactly the whole input array is to be copied. This helps eliminate range checks and has been verified to help various String operations. Example:
>> 
>> Baseline
>> 
>> Benchmark                                            (size)  Mode  Cnt   Score   Error  Units
>> StringConstructor.newStringFromArray                      7  avgt   15  16.817 ± 0.369  ns/op
>> StringConstructor.newStringFromArrayWithCharset           7  avgt   15  16.866 ± 0.449  ns/op
>> StringConstructor.newStringFromArrayWithCharsetName       7  avgt   15  22.198 ± 0.396  ns/op
>> 
>> Patch: 
>> 
>> Benchmark                                            (size)  Mode  Cnt   Score   Error  Units
>> StringConstructor.newStringFromArray                      7  avgt   15  14.666 ± 0.336  ns/op
>> StringConstructor.newStringFromArrayWithCharset           7  avgt   15  14.582 ± 0.288  ns/op
>> StringConstructor.newStringFromArrayWithCharsetName       7  avgt   15  20.339 ± 0.328  ns/op
>
> Claes Redestad has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Add offset param to micro, reduce number of configurations and variants

The nudge from John and others to move this to Arrays.copyOf is much better. It looks good.

We may have to come back to the SB::toLength issue from the original proposal.

src/java.base/share/classes/java/util/Arrays.java line 29:

> 27: 
> 28: import jdk.internal.util.ArraysSupport;
> 29: import jdk.internal.util.Preconditions;

I don't think Preconditions is needed now.

src/java.base/share/classes/java/util/Arrays.java line 3909:

> 3907: 
> 3908:     @ForceInline
> 3909:     private static byte[] copyOfRangeGeneric(byte[] original, int from, int to) {

Maybe copyOfRange{Short,Int,Long,...} might be better than a "Generic" suffix.

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

Marked as reviewed by alanb (Reviewer).

PR: https://git.openjdk.org/jdk/pull/12453


More information about the core-libs-dev mailing list