8309665: Simplify Arrays.copyOf/-Range methods

Reviewed-by: jlaskey, rriggs, alanb
This commit is contained in:
Claes Redestad 2023-06-09 09:57:39 +00:00
parent 307085618d
commit dc842e8587

View file

@ -26,7 +26,6 @@
package java.util; package java.util;
import jdk.internal.util.ArraysSupport; import jdk.internal.util.ArraysSupport;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.IntrinsicCandidate; import jdk.internal.vm.annotation.IntrinsicCandidate;
import java.io.Serializable; import java.io.Serializable;
@ -3804,8 +3803,9 @@ public final class Arrays {
@IntrinsicCandidate @IntrinsicCandidate
public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) { public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
int newLength = to - from; int newLength = to - from;
if (newLength < 0) if (newLength < 0) {
throw new IllegalArgumentException(from + " > " + to); throw new IllegalArgumentException(from + " > " + to);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class) T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength] ? (T[]) new Object[newLength]
@ -3815,13 +3815,6 @@ public final class Arrays {
return copy; return copy;
} }
@ForceInline
private static void checkLength(int from, int to) {
if (to < from) {
throw new IllegalArgumentException(from + " > " + to);
}
}
/** /**
* Copies the specified range of the specified array into a new array. * Copies the specified range of the specified array into a new array.
* The initial index of the range ({@code from}) must lie between zero * The initial index of the range ({@code from}) must lie between zero
@ -3849,20 +3842,16 @@ public final class Arrays {
* @since 1.6 * @since 1.6
*/ */
public static byte[] copyOfRange(byte[] original, int from, int to) { public static byte[] copyOfRange(byte[] original, int from, int to) {
// Tickle the JIT to fold special cases optimally if (from == 0 && to == original.length) {
if (from != 0 || to != original.length)
return copyOfRangeByte(original, from, to);
else // from == 0 && to == original.length
return original.clone(); return original.clone();
} }
@ForceInline
private static byte[] copyOfRangeByte(byte[] original, int from, int to) {
checkLength(from, to);
int newLength = to - from; int newLength = to - from;
if (newLength < 0) {
throw new IllegalArgumentException(from + " > " + to);
}
byte[] copy = new byte[newLength]; byte[] copy = new byte[newLength];
System.arraycopy(original, from, copy, 0, System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength)); Math.min(original.length - from, newLength));
return copy; return copy;
} }
@ -3893,17 +3882,13 @@ public final class Arrays {
* @since 1.6 * @since 1.6
*/ */
public static short[] copyOfRange(short[] original, int from, int to) { public static short[] copyOfRange(short[] original, int from, int to) {
// Tickle the JIT to fold special cases optimally if (from == 0 && to == original.length) {
if (from != 0 || to != original.length)
return copyOfRangeShort(original, from, to);
else // from == 0 && to == original.length
return original.clone(); return original.clone();
} }
@ForceInline
private static short[] copyOfRangeShort(short[] original, int from, int to) {
checkLength(from, to);
int newLength = to - from; int newLength = to - from;
if (newLength < 0) {
throw new IllegalArgumentException(from + " > " + to);
}
short[] copy = new short[newLength]; short[] copy = new short[newLength];
System.arraycopy(original, from, copy, 0, System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength)); Math.min(original.length - from, newLength));
@ -3937,17 +3922,13 @@ public final class Arrays {
* @since 1.6 * @since 1.6
*/ */
public static int[] copyOfRange(int[] original, int from, int to) { public static int[] copyOfRange(int[] original, int from, int to) {
// Tickle the JIT to fold special cases optimally if (from == 0 && to == original.length) {
if (from != 0 || to != original.length)
return copyOfRangeInt(original, from, to);
else // from == 0 && to == original.length
return original.clone(); return original.clone();
} }
@ForceInline
private static int[] copyOfRangeInt(int[] original, int from, int to) {
checkLength(from, to);
int newLength = to - from; int newLength = to - from;
if (newLength < 0) {
throw new IllegalArgumentException(from + " > " + to);
}
int[] copy = new int[newLength]; int[] copy = new int[newLength];
System.arraycopy(original, from, copy, 0, System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength)); Math.min(original.length - from, newLength));
@ -3981,17 +3962,13 @@ public final class Arrays {
* @since 1.6 * @since 1.6
*/ */
public static long[] copyOfRange(long[] original, int from, int to) { public static long[] copyOfRange(long[] original, int from, int to) {
// Tickle the JIT to fold special cases optimally if (from == 0 && to == original.length) {
if (from != 0 || to != original.length)
return copyOfRangeLong(original, from, to);
else // from == 0 && to == original.length
return original.clone(); return original.clone();
} }
@ForceInline
private static long[] copyOfRangeLong(long[] original, int from, int to) {
checkLength(from, to);
int newLength = to - from; int newLength = to - from;
if (newLength < 0) {
throw new IllegalArgumentException(from + " > " + to);
}
long[] copy = new long[newLength]; long[] copy = new long[newLength];
System.arraycopy(original, from, copy, 0, System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength)); Math.min(original.length - from, newLength));
@ -4025,17 +4002,13 @@ public final class Arrays {
* @since 1.6 * @since 1.6
*/ */
public static char[] copyOfRange(char[] original, int from, int to) { public static char[] copyOfRange(char[] original, int from, int to) {
// Tickle the JIT to fold special cases optimally if (from == 0 && to == original.length) {
if (from != 0 || to != original.length)
return copyOfRangeChar(original, from, to);
else // from == 0 && to == original.length
return original.clone(); return original.clone();
} }
@ForceInline
private static char[] copyOfRangeChar(char[] original, int from, int to) {
checkLength(from, to);
int newLength = to - from; int newLength = to - from;
if (newLength < 0) {
throw new IllegalArgumentException(from + " > " + to);
}
char[] copy = new char[newLength]; char[] copy = new char[newLength];
System.arraycopy(original, from, copy, 0, System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength)); Math.min(original.length - from, newLength));
@ -4069,17 +4042,13 @@ public final class Arrays {
* @since 1.6 * @since 1.6
*/ */
public static float[] copyOfRange(float[] original, int from, int to) { public static float[] copyOfRange(float[] original, int from, int to) {
// Tickle the JIT to fold special cases optimally if (from == 0 && to == original.length) {
if (from != 0 || to != original.length)
return copyOfRangeFloat(original, from, to);
else // from == 0 && to == original.length
return original.clone(); return original.clone();
} }
@ForceInline
private static float[] copyOfRangeFloat(float[] original, int from, int to) {
checkLength(from, to);
int newLength = to - from; int newLength = to - from;
if (newLength < 0) {
throw new IllegalArgumentException(from + " > " + to);
}
float[] copy = new float[newLength]; float[] copy = new float[newLength];
System.arraycopy(original, from, copy, 0, System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength)); Math.min(original.length - from, newLength));
@ -4113,17 +4082,13 @@ public final class Arrays {
* @since 1.6 * @since 1.6
*/ */
public static double[] copyOfRange(double[] original, int from, int to) { public static double[] copyOfRange(double[] original, int from, int to) {
// Tickle the JIT to fold special cases optimally if (from == 0 && to == original.length) {
if (from != 0 || to != original.length)
return copyOfRangeDouble(original, from, to);
else // from == 0 && to == original.length
return original.clone(); return original.clone();
} }
@ForceInline
private static double[] copyOfRangeDouble(double[] original, int from, int to) {
checkLength(from, to);
int newLength = to - from; int newLength = to - from;
if (newLength < 0) {
throw new IllegalArgumentException(from + " > " + to);
}
double[] copy = new double[newLength]; double[] copy = new double[newLength];
System.arraycopy(original, from, copy, 0, System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength)); Math.min(original.length - from, newLength));
@ -4157,17 +4122,13 @@ public final class Arrays {
* @since 1.6 * @since 1.6
*/ */
public static boolean[] copyOfRange(boolean[] original, int from, int to) { public static boolean[] copyOfRange(boolean[] original, int from, int to) {
// Tickle the JIT to fold special cases optimally if (from == 0 && to == original.length) {
if (from != 0 || to != original.length)
return copyOfRangeBoolean(original, from, to);
else // from == 0 && to == original.length
return original.clone(); return original.clone();
} }
@ForceInline
private static boolean[] copyOfRangeBoolean(boolean[] original, int from, int to) {
checkLength(from, to);
int newLength = to - from; int newLength = to - from;
if (newLength < 0) {
throw new IllegalArgumentException(from + " > " + to);
}
boolean[] copy = new boolean[newLength]; boolean[] copy = new boolean[newLength];
System.arraycopy(original, from, copy, 0, System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength)); Math.min(original.length - from, newLength));