On Wed, 8 Feb 2023 00:07:14 GMT, Claes Redestad <redes...@openjdk.org> wrote:
>> This patch adds special-cases to `Arrays.copyOf` and `Arrays.copyOfRange` to >> clone arrays when `newLength` or range inputs span the input array. 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: > > Minimize, force inline, generalize I'm also curious if returning the new length from `checkLength` would be worthwhile src/java.base/share/classes/java/util/Arrays.java line 3823: > 3821: if (to - from < 0) { > 3822: throw new IllegalArgumentException(from + " > " + to); > 3823: } Would it be beneficial to return the difference of `to` and `from` for caller use? Suggestion: private static int checkLength(int from, int to) { int newLength = to - from; if (newLength < 0) { throw new IllegalArgumentException(from + " > " + to); } return newLength; src/java.base/share/classes/java/util/Arrays.java line 3870: > 3868: private static byte[] copyOfRangeGeneric(byte[] original, int from, > int to) { > 3869: checkLength(from, to); > 3870: int newLength = to - from; ```suggestion int newLength = checkLength(from, to); src/java.base/share/classes/java/util/Arrays.java line 3914: > 3912: private static short[] copyOfRangeGeneric(short[] original, int > from, int to) { > 3913: checkLength(from, to); > 3914: int newLength = to - from; Suggestion: int newLength = checkLength(from, to); src/java.base/share/classes/java/util/Arrays.java line 3958: > 3956: private static int[] copyOfRangeGeneric(int[] original, int from, > int to) { > 3957: checkLength(from, to); > 3958: int newLength = to - from; Suggestion: int newLength = checkLength(from, to); src/java.base/share/classes/java/util/Arrays.java line 4002: > 4000: private static long[] copyOfRangeGeneric(long[] original, int from, > int to) { > 4001: checkLength(from, to); > 4002: int newLength = to - from; Suggestion: int newLength = checkLength(from, to); src/java.base/share/classes/java/util/Arrays.java line 4046: > 4044: private static char[] copyOfRangeGeneric(char[] original, int from, > int to) { > 4045: checkLength(from, to); > 4046: int newLength = to - from; Suggestion: int newLength = checkLength(from, to); src/java.base/share/classes/java/util/Arrays.java line 4091: > 4089: private static float[] copyOfRangeGeneric(float[] original, int > from, int to) { > 4090: checkLength(from, to); > 4091: int newLength = to - from; Suggestion: int newLength = checkLength(from, to); src/java.base/share/classes/java/util/Arrays.java line 4142: > 4140: private static double[] copyOfRangeGeneric(double[] original, int > from, int to) { > 4141: checkLength(from, to); > 4142: int newLength = to - from; Suggestion: int newLength = checkLength(from, to); src/java.base/share/classes/java/util/Arrays.java line 4193: > 4191: private static boolean[] copyOfRangeGeneric(boolean[] original, int > from, int to) { > 4192: checkLength(from, to); > 4193: int newLength = to - from; Suggestion: int newLength = checkLength(from, to); ------------- PR: https://git.openjdk.org/jdk/pull/12453