Top Description Fields Constructors Methods
jdk.internal.util

public Class ArraysSupport

extends Object
Class Inheritance
Imports
java.util.Arrays, .Collection, .Objects, jdk.internal.access.JavaLangAccess, .SharedSecrets, jdk.internal.misc.Unsafe, jdk.internal.vm.annotation.IntrinsicCandidate

Utility methods to work with arrays. This includes a set of methods to find a mismatch between two primitive arrays. Also included is a method to calculate the new length of an array to be reallocated.

Array equality and lexicographical comparison can be built on top of array mismatch functionality.

The mismatch method implementation, vectorizedMismatch, leverages vector-based techniques to access and compare the contents of two arrays. The Java implementation uses Unsafe.getLongUnaligned to access the content of an array, thus access is supported on platforms that do not support unaligned access. For a byte[] array, 8 bytes (64 bits) can be accessed and compared as a unit rather than individually, which increases the performance when the method is compiled by the HotSpot VM. On supported platforms the mismatch implementation is intrinsified to leverage SIMD instructions. So for a byte[] array, 16 bytes (128 bits), 32 bytes (256 bits), and perhaps in the future even 64 bytes (512 bits), platform permitting, can be accessed and compared as a unit, which further increases the performance over the Java implementation.

None of the mismatch methods perform array bounds checks. It is the responsibility of the caller (direct or otherwise) to perform such checks before calling this method.

Field Summary

Modifier and TypeField and Description
private static final boolean
private static final JavaLangAccess
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
private static final int
public static final int
SOFT_MAX_ARRAY_LENGTH

A soft maximum array length imposed by array growth computations.

private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
pack-priv static final Unsafe
U

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static int
exactLog2(int scale)

public static int

Returns:

the calculated hash value
hashCode
(int[]
the array
a
,
int
the first index of the subrange of the array
fromIndex
,
int
the number of elements in the subrange
length
,
int
the initial hash value, typically 0 or 1
initialValue
)

Calculates the hash code for the subrange of an integer array.

public static int

Returns:

the calculated hash value
hashCode
(short[]
the array
a
,
int
the first index of the subrange of the array
fromIndex
,
int
the number of elements in the subrange
length
,
int
the initial hash value, typically 0 or 1
initialValue
)

Calculates the hash code for the subrange of a short array.

public static int

Returns:

the calculated hash value
hashCode
(char[]
the array
a
,
int
the first index of the subrange of the array
fromIndex
,
int
the number of elements in the subrange
length
,
int
the initial hash value, typically 0 or 1
initialValue
)

Calculates the hash code for the subrange of a char array.

public static int

Returns:

the calculated hash value
hashCode
(byte[]
the array
a
,
int
the first index of the subrange of the array
fromIndex
,
int
the number of elements in the subrange
length
,
int
the initial hash value, typically 0 or 1
initialValue
)

Calculates the hash code for the subrange of a byte array.

public static int

Returns:

the calculated hash value
hashCode
(Object[]
the array
a
,
int
the first index of the subrange of the array
fromIndex
,
int
the number of elements in the subrange
length
,
int
the initial hash value, typically 0 or 1
initialValue
)

Calculates the hash code for the subrange of an object array.

private static int
hashCode(int result, byte[] a, int fromIndex, int length)

private static int
hashCode(int result, char[] a, int fromIndex, int length)

private static int
hashCode(int result, short[] a, int fromIndex, int length)

private static int
hashCode(int result, int[] a, int fromIndex, int length)

public static int

Returns:

the calculated hash value
hashCodeOfUnsigned
(byte[]
the array
a
,
int
the first index of the subrange of the array
fromIndex
,
int
the number of elements in the subrange
length
,
int
the initial hash value, typically 0 or 1
initialValue
)

Calculates the hash code for the subrange of a byte array whose elements are treated as unsigned bytes.

public static int

Returns:

the calculated hash value
hashCodeOfUTF16
(byte[]
the array
a
,
int
the first index of a char in the subrange of the array
fromIndex
,
int
the number of chars in the subrange
length
,
int
the initial hash value, typically 0 or 1
initialValue
)

Calculates the hash code for the subrange of a byte array whose contents are treated as UTF-16 chars.

private static int
hugeLength(int oldLength, int minGrowth)

public static int
mismatch(boolean[] a, boolean[] b, int length)

public static int
mismatch(boolean[] a, int aFromIndex, boolean[] b, int bFromIndex, int length)

public static int

Returns:

the index of a mismatch between the two arrays, otherwise -1 if no mismatch. The index will be within the range of (inclusive) 0 to (exclusive) the smaller of the two array lengths.
mismatch
(byte[]
the first array to be tested for a mismatch
a
,
byte[]
the second array to be tested for a mismatch
b
,
int
the number of bytes from each array to check
length
)

Find the index of a mismatch between two arrays.

public static int

Returns:

the relative index of a mismatch between the two arrays, otherwise -1 if no mismatch. The index will be within the range of (inclusive) 0 to (exclusive) the smaller of the two array bounds.
mismatch
(byte[]
the first array to be tested for a mismatch
a
,
int
the index of the first element (inclusive) in the first array to be compared
aFromIndex
,
byte[]
the second array to be tested for a mismatch
b
,
int
the index of the first element (inclusive) in the second array to be compared
bFromIndex
,
int
the number of bytes from each array to check
length
)

Find the relative index of a mismatch between two arrays starting from given indexes.

public static int
mismatch(char[] a, char[] b, int length)

public static int
mismatch(char[] a, int aFromIndex, char[] b, int bFromIndex, int length)

public static int
mismatch(short[] a, short[] b, int length)

public static int
mismatch(short[] a, int aFromIndex, short[] b, int bFromIndex, int length)

public static int
mismatch(int[] a, int[] b, int length)

public static int
mismatch(int[] a, int aFromIndex, int[] b, int bFromIndex, int length)

public static int
mismatch(float[] a, float[] b, int length)

public static int
mismatch(float[] a, int aFromIndex, float[] b, int bFromIndex, int length)

public static int
mismatch(long[] a, long[] b, int length)

public static int
mismatch(long[] a, int aFromIndex, long[] b, int bFromIndex, int length)

public static int
mismatch(double[] a, double[] b, int length)

public static int
mismatch(double[] a, int aFromIndex, double[] b, int bFromIndex, int length)

public static int

Returns:

the new array length
newLength
(int
current length of the array (must be nonnegative)
oldLength
,
int
minimum required growth amount (must be positive)
minGrowth
,
int
preferred growth amount
prefGrowth
)

Computes a new array length given an array's current length, a minimum growth amount, and a preferred growth amount.

public static <
the array component type
T
>
T[]

Returns:

the reversed array, always the same array as the argument
reverse
(T[]
the array to be reversed
a
)

Reverses the elements of an array in-place.

public static <T> T[]
toArrayReversed(Collection<?> coll, T[] array)

Dump the contents of the given collection into the given array, in reverse order.

private static int
unsignedHashCode(int result, byte[] a, int fromIndex, int length)

private static int
utf16hashCode(int result, byte[] value, int fromIndex, int length)

private static int

Returns:

the calculated hash value
vectorizedHashCode
(Object
for which to calculate hash code
array
,
int
start index, scaled to basicType
fromIndex
,
int
number of elements to include in the hash
length
,
int
the initial value for the hash (typically constant 0 or 1)
initialValue
,
int
type constant denoting how to interpret the array content. T_BOOLEAN is used to signify unsigned bytes, and T_CHAR might be used even if array is a byte[].
basicType
)

Calculate the hash code for an array in a way that enables efficient vectorization.

public static int

Returns:

if a mismatch is found a relative index, between 0 (inclusive) and length (exclusive), of the first mismatching pair of elements in the two arrays. Otherwise, if a mismatch is not found the bitwise compliment of the number of remaining pairs of elements to be checked in the tail of the two arrays.
vectorizedMismatch
(Object
the first array to be tested for mismatch, or null for direct memory access
a
,
long
the relative offset, in bytes, from the base address of the first array to test from, otherwise if the first array is null, an absolute address pointing to the first element to test.
aOffset
,
Object
the second array to be tested for mismatch, or null for direct memory access
b
,
long
the relative offset, in bytes, from the base address of the second array to test from, otherwise if the second array is null, an absolute address pointing to the first element to test.
bOffset
,
int
the number of array elements to test
length
,
int
log2 of the array index scale, that corresponds to the size, in bytes, of an array element.
log2ArrayIndexScale
)

Find the relative index of the first mismatching pair of elements in two primitive arrays of the same component type.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BIG_ENDIANback to summary
private static final boolean BIG_ENDIAN
JLAback to summary
private static final JavaLangAccess JLA
LOG2_ARRAY_BOOLEAN_INDEX_SCALEback to summary
public static final int LOG2_ARRAY_BOOLEAN_INDEX_SCALE
LOG2_ARRAY_BYTE_INDEX_SCALEback to summary
public static final int LOG2_ARRAY_BYTE_INDEX_SCALE
LOG2_ARRAY_CHAR_INDEX_SCALEback to summary
public static final int LOG2_ARRAY_CHAR_INDEX_SCALE
LOG2_ARRAY_DOUBLE_INDEX_SCALEback to summary
public static final int LOG2_ARRAY_DOUBLE_INDEX_SCALE
LOG2_ARRAY_FLOAT_INDEX_SCALEback to summary
public static final int LOG2_ARRAY_FLOAT_INDEX_SCALE
LOG2_ARRAY_INT_INDEX_SCALEback to summary
public static final int LOG2_ARRAY_INT_INDEX_SCALE
LOG2_ARRAY_LONG_INDEX_SCALEback to summary
public static final int LOG2_ARRAY_LONG_INDEX_SCALE
LOG2_ARRAY_SHORT_INDEX_SCALEback to summary
public static final int LOG2_ARRAY_SHORT_INDEX_SCALE
LOG2_BYTE_BIT_SIZEback to summary
private static final int LOG2_BYTE_BIT_SIZE
SOFT_MAX_ARRAY_LENGTHback to summary
public static final int SOFT_MAX_ARRAY_LENGTH

A soft maximum array length imposed by array growth computations. Some JVMs (such as HotSpot) have an implementation limit that will cause OutOfMemoryError("Requested array size exceeds VM limit") to be thrown if a request is made to allocate an array of some length near Integer.MAX_VALUE, even if there is sufficient heap available. The actual limit might depend on some JVM implementation-specific characteristics such as the object header size. The soft maximum value is chosen conservatively so as to be smaller than any implementation limit that is likely to be encountered.

T_BOOLEANback to summary
private static final int T_BOOLEAN
T_BYTEback to summary
private static final int T_BYTE
T_CHARback to summary
private static final int T_CHAR
T_DOUBLEback to summary
private static final int T_DOUBLE
T_FLOATback to summary
private static final int T_FLOAT
T_INTback to summary
private static final int T_INT
T_LONGback to summary
private static final int T_LONG
T_SHORTback to summary
private static final int T_SHORT
Uback to summary
pack-priv static final Unsafe U

Constructor Detail

ArraysSupportback to summary
private ArraysSupport()

Method Detail

exactLog2back to summary
private static int exactLog2(int scale)
hashCodeback to summary
public static int hashCode(int[] a, int fromIndex, int length, int initialValue)

Calculates the hash code for the subrange of an integer array.

This method does not perform type checks or bounds checks. It is the responsibility of the caller to perform such checks before calling this method.

Parameters
a:int[]

the array

fromIndex:int

the first index of the subrange of the array

length:int

the number of elements in the subrange

initialValue:int

the initial hash value, typically 0 or 1

Returns:int

the calculated hash value

hashCodeback to summary
public static int hashCode(short[] a, int fromIndex, int length, int initialValue)

Calculates the hash code for the subrange of a short array.

This method does not perform type checks or bounds checks. It is the responsibility of the caller to perform such checks before calling this method.

Parameters
a:short[]

the array

fromIndex:int

the first index of the subrange of the array

length:int

the number of elements in the subrange

initialValue:int

the initial hash value, typically 0 or 1

Returns:int

the calculated hash value

hashCodeback to summary
public static int hashCode(char[] a, int fromIndex, int length, int initialValue)

Calculates the hash code for the subrange of a char array.

This method does not perform type checks or bounds checks. It is the responsibility of the caller to perform such checks before calling this method.

Parameters
a:char[]

the array

fromIndex:int

the first index of the subrange of the array

length:int

the number of elements in the subrange

initialValue:int

the initial hash value, typically 0 or 1

Returns:int

the calculated hash value

hashCodeback to summary
public static int hashCode(byte[] a, int fromIndex, int length, int initialValue)

Calculates the hash code for the subrange of a byte array.

This method does not perform type checks or bounds checks. It is the responsibility of the caller to perform such checks before calling this method.

Parameters
a:byte[]

the array

fromIndex:int

the first index of the subrange of the array

length:int

the number of elements in the subrange

initialValue:int

the initial hash value, typically 0 or 1

Returns:int

the calculated hash value

hashCodeback to summary
public static int hashCode(Object[] a, int fromIndex, int length, int initialValue)

Calculates the hash code for the subrange of an object array.

This method does not perform type checks or bounds checks. It is the responsibility of the caller to perform such checks before calling this method.

Parameters
a:Object[]

the array

fromIndex:int

the first index of the subrange of the array

length:int

the number of elements in the subrange

initialValue:int

the initial hash value, typically 0 or 1

Returns:int

the calculated hash value

hashCodeback to summary
private static int hashCode(int result, byte[] a, int fromIndex, int length)
hashCodeback to summary
private static int hashCode(int result, char[] a, int fromIndex, int length)
hashCodeback to summary
private static int hashCode(int result, short[] a, int fromIndex, int length)
hashCodeback to summary
private static int hashCode(int result, int[] a, int fromIndex, int length)
hashCodeOfUnsignedback to summary
public static int hashCodeOfUnsigned(byte[] a, int fromIndex, int length, int initialValue)

Calculates the hash code for the subrange of a byte array whose elements are treated as unsigned bytes.

This method does not perform type checks or bounds checks. It is the responsibility of the caller to perform such checks before calling this method.

Parameters
a:byte[]

the array

fromIndex:int

the first index of the subrange of the array

length:int

the number of elements in the subrange

initialValue:int

the initial hash value, typically 0 or 1

Returns:int

the calculated hash value

hashCodeOfUTF16back to summary
public static int hashCodeOfUTF16(byte[] a, int fromIndex, int length, int initialValue)

Calculates the hash code for the subrange of a byte array whose contents are treated as UTF-16 chars.

This method does not perform type checks or bounds checks. It is the responsibility of the caller to perform such checks before calling this method.

fromIndex and length must be scaled down to char indexes.

Parameters
a:byte[]

the array

fromIndex:int

the first index of a char in the subrange of the array

length:int

the number of chars in the subrange

initialValue:int

the initial hash value, typically 0 or 1

Returns:int

the calculated hash value

hugeLengthback to summary
private static int hugeLength(int oldLength, int minGrowth)
mismatchback to summary
public static int mismatch(boolean[] a, boolean[] b, int length)
mismatchback to summary
public static int mismatch(boolean[] a, int aFromIndex, boolean[] b, int bFromIndex, int length)
mismatchback to summary
public static int mismatch(byte[] a, byte[] b, int length)

Find the index of a mismatch between two arrays.

This method does not perform bounds checks. It is the responsibility of the caller to perform such bounds checks before calling this method.

Parameters
a:byte[]

the first array to be tested for a mismatch

b:byte[]

the second array to be tested for a mismatch

length:int

the number of bytes from each array to check

Returns:int

the index of a mismatch between the two arrays, otherwise -1 if no mismatch. The index will be within the range of (inclusive) 0 to (exclusive) the smaller of the two array lengths.

mismatchback to summary
public static int mismatch(byte[] a, int aFromIndex, byte[] b, int bFromIndex, int length)

Find the relative index of a mismatch between two arrays starting from given indexes.

This method does not perform bounds checks. It is the responsibility of the caller to perform such bounds checks before calling this method.

Parameters
a:byte[]

the first array to be tested for a mismatch

aFromIndex:int

the index of the first element (inclusive) in the first array to be compared

b:byte[]

the second array to be tested for a mismatch

bFromIndex:int

the index of the first element (inclusive) in the second array to be compared

length:int

the number of bytes from each array to check

Returns:int

the relative index of a mismatch between the two arrays, otherwise -1 if no mismatch. The index will be within the range of (inclusive) 0 to (exclusive) the smaller of the two array bounds.

mismatchback to summary
public static int mismatch(char[] a, char[] b, int length)
mismatchback to summary
public static int mismatch(char[] a, int aFromIndex, char[] b, int bFromIndex, int length)
mismatchback to summary
public static int mismatch(short[] a, short[] b, int length)
mismatchback to summary
public static int mismatch(short[] a, int aFromIndex, short[] b, int bFromIndex, int length)
mismatchback to summary
public static int mismatch(int[] a, int[] b, int length)
mismatchback to summary
public static int mismatch(int[] a, int aFromIndex, int[] b, int bFromIndex, int length)
mismatchback to summary
public static int mismatch(float[] a, float[] b, int length)
mismatchback to summary
public static int mismatch(float[] a, int aFromIndex, float[] b, int bFromIndex, int length)
mismatchback to summary
public static int mismatch(long[] a, long[] b, int length)
mismatchback to summary
public static int mismatch(long[] a, int aFromIndex, long[] b, int bFromIndex, int length)
mismatchback to summary
public static int mismatch(double[] a, double[] b, int length)
mismatchback to summary
public static int mismatch(double[] a, int aFromIndex, double[] b, int bFromIndex, int length)
newLengthback to summary
public static int newLength(int oldLength, int minGrowth, int prefGrowth)

Computes a new array length given an array's current length, a minimum growth amount, and a preferred growth amount. The computation is done in an overflow-safe fashion. This method is used by objects that contain an array that might need to be grown in order to fulfill some immediate need (the minimum growth amount) but would also like to request more space (the preferred growth amount) in order to accommodate potential future needs. The returned length is usually clamped at the soft maximum length in order to avoid hitting the JVM implementation limit. However, the soft maximum will be exceeded if the minimum growth amount requires it. If the preferred growth amount is less than the minimum growth amount, the minimum growth amount is used as the preferred growth amount. The preferred length is determined by adding the preferred growth amount to the current length. If the preferred length does not exceed the soft maximum length (SOFT_MAX_ARRAY_LENGTH) then the preferred length is returned. If the preferred length exceeds the soft maximum, we use the minimum growth amount. The minimum required length is determined by adding the minimum growth amount to the current length. If the minimum required length exceeds Integer.MAX_VALUE, then this method throws OutOfMemoryError. Otherwise, this method returns the greater of the soft maximum or the minimum required length. Note that this method does not do any array allocation itself; it only does array length growth computations. However, it will throw OutOfMemoryError as noted above. Note also that this method cannot detect the JVM's implementation limit, and it may compute and return a length value up to and including Integer.MAX_VALUE that might exceed the JVM's implementation limit. In that case, the caller will likely attempt an array allocation with that length and encounter an OutOfMemoryError. Of course, regardless of the length value returned from this method, the caller may encounter OutOfMemoryError if there is insufficient heap to fulfill the request.

Parameters
oldLength:int

current length of the array (must be nonnegative)

minGrowth:int

minimum required growth amount (must be positive)

prefGrowth:int

preferred growth amount

Returns:int

the new array length

Exceptions
OutOfMemoryError:
if the new length would exceed Integer.MAX_VALUE
reverseback to summary
public static <T> T[] reverse(T[] a)

Reverses the elements of an array in-place.

Parameters
<T>
the array component type
a:T[]

the array to be reversed

Returns:T[]

the reversed array, always the same array as the argument

toArrayReversedback to summary
public static <T> T[] toArrayReversed(Collection<?> coll, T[] array)

Dump the contents of the given collection into the given array, in reverse order. This mirrors the semantics of Collection.toArray(T[]) in regard to reusing the given array, appending null if necessary, or allocating a new array of the same component type.

A constraint is that this method should issue exactly one method call on the collection to obtain the elements and the size. Having a separate size() call or using an Iterator could result in errors if the collection changes size between calls. This implies that the elements need to be obtained via a single call to one of the toArray() methods. This further implies allocating memory proportional to the number of elements and making an extra copy, but this seems unavoidable.

An obvious approach would be simply to call coll.toArray(array) and then reverse the order of the elements. This doesn't work, because if given array is sufficiently long, we cannot tell how many elements were copied into it and thus there is no way to reverse the right set of elements while leaving the remaining array elements undisturbed.

Exceptions
ArrayStoreException:
if coll contains elements that can't be stored in the array
unsignedHashCodeback to summary
private static int unsignedHashCode(int result, byte[] a, int fromIndex, int length)
utf16hashCodeback to summary
private static int utf16hashCode(int result, byte[] value, int fromIndex, int length)
vectorizedHashCodeback to summary
private static int vectorizedHashCode(Object array, int fromIndex, int length, int initialValue, int basicType)

Calculate the hash code for an array in a way that enables efficient vectorization.

This method does not perform type checks or bounds checks. It is the responsibility of the caller to perform such checks before calling this method.

Implementation Note

currently basicType must be constant at the call site for this method to be intrinsified.

Parameters
array:Object

for which to calculate hash code

fromIndex:int

start index, scaled to basicType

length:int

number of elements to include in the hash

initialValue:int

the initial value for the hash (typically constant 0 or 1)

basicType:int

type constant denoting how to interpret the array content. T_BOOLEAN is used to signify unsigned bytes, and T_CHAR might be used even if array is a byte[].

Returns:int

the calculated hash value

Annotations
@IntrinsicCandidate
vectorizedMismatchback to summary
public static int vectorizedMismatch(Object a, long aOffset, Object b, long bOffset, int length, int log2ArrayIndexScale)

Find the relative index of the first mismatching pair of elements in two primitive arrays of the same component type. Pairs of elements will be tested in order relative to given offsets into both arrays.

This method does not perform type checks or bounds checks. It is the responsibility of the caller to perform such checks before calling this method.

The given offsets, in bytes, need not be aligned according to the given log2 size the array elements. More specifically, an offset modulus the size need not be zero.

Parameters
a:Object

the first array to be tested for mismatch, or null for direct memory access

aOffset:long

the relative offset, in bytes, from the base address of the first array to test from, otherwise if the first array is null, an absolute address pointing to the first element to test.

b:Object

the second array to be tested for mismatch, or null for direct memory access

bOffset:long

the relative offset, in bytes, from the base address of the second array to test from, otherwise if the second array is null, an absolute address pointing to the first element to test.

length:int

the number of array elements to test

log2ArrayIndexScale:int

log2 of the array index scale, that corresponds to the size, in bytes, of an array element.

Returns:int

if a mismatch is found a relative index, between 0 (inclusive) and length (exclusive), of the first mismatching pair of elements in the two arrays. Otherwise, if a mismatch is not found the bitwise compliment of the number of remaining pairs of elements to be checked in the tail of the two arrays.

Annotations
@IntrinsicCandidate