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.
Modifier and Type | Field 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 |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
private static int | |
public static int | Returns: the calculated hash valuethe 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 valuethe 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 valuethe 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 valuethe 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 | |
private static int | |
private static int | |
private static int | |
private static int | |
public static int | Returns: the calculated hash valuethe 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 valuethe 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 | |
public static int | |
public static int | |
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.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.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 | |
public static int | |
public static int | |
public static int | |
public static int | |
public static int | |
public static int | |
public static int | |
public static int | |
public static int | |
public static int | |
public static int | |
public static int | Returns: the new array lengthcurrent 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 argumentthe 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 | |
private static int | |
private static int | Returns: the calculated hash valuefor 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) andlength (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.the first array to be tested for mismatch, or a, long null for
direct memory accessthe relative offset, in bytes, from the base address of
the first array to test from, otherwise if the first array is
aOffset, Object null , an absolute address pointing to the first element to test.the second array to be tested for mismatch, or b, long null for
direct memory accessthe relative offset, in bytes, from the base address of
the second array to test from, otherwise if the second array is
bOffset, int null , an absolute address pointing to the first element to test.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. |
BIG_ENDIAN | back to summary |
---|---|
private static final boolean BIG_ENDIAN |
JLA | back to summary |
---|---|
private static final JavaLangAccess JLA |
LOG2_ARRAY_BOOLEAN_INDEX_SCALE | back to summary |
---|---|
public static final int LOG2_ARRAY_BOOLEAN_INDEX_SCALE |
LOG2_ARRAY_BYTE_INDEX_SCALE | back to summary |
---|---|
public static final int LOG2_ARRAY_BYTE_INDEX_SCALE |
LOG2_ARRAY_CHAR_INDEX_SCALE | back to summary |
---|---|
public static final int LOG2_ARRAY_CHAR_INDEX_SCALE |
LOG2_ARRAY_DOUBLE_INDEX_SCALE | back to summary |
---|---|
public static final int LOG2_ARRAY_DOUBLE_INDEX_SCALE |
LOG2_ARRAY_FLOAT_INDEX_SCALE | back to summary |
---|---|
public static final int LOG2_ARRAY_FLOAT_INDEX_SCALE |
LOG2_ARRAY_INT_INDEX_SCALE | back to summary |
---|---|
public static final int LOG2_ARRAY_INT_INDEX_SCALE |
LOG2_ARRAY_LONG_INDEX_SCALE | back to summary |
---|---|
public static final int LOG2_ARRAY_LONG_INDEX_SCALE |
LOG2_ARRAY_SHORT_INDEX_SCALE | back to summary |
---|---|
public static final int LOG2_ARRAY_SHORT_INDEX_SCALE |
LOG2_BYTE_BIT_SIZE | back to summary |
---|---|
private static final int LOG2_BYTE_BIT_SIZE |
SOFT_MAX_ARRAY_LENGTH | back 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_BOOLEAN | back to summary |
---|---|
private static final int T_BOOLEAN |
T_BYTE | back to summary |
---|---|
private static final int T_BYTE |
T_CHAR | back to summary |
---|---|
private static final int T_CHAR |
T_DOUBLE | back to summary |
---|---|
private static final int T_DOUBLE |
T_FLOAT | back to summary |
---|---|
private static final int T_FLOAT |
T_INT | back to summary |
---|---|
private static final int T_INT |
T_LONG | back to summary |
---|---|
private static final int T_LONG |
T_SHORT | back to summary |
---|---|
private static final int T_SHORT |
U | back to summary |
---|---|
pack-priv static final Unsafe U |
ArraysSupport | back to summary |
---|---|
private ArraysSupport() |
exactLog2 | back to summary |
---|---|
private static int exactLog2(int scale) |
hashCode | back 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.
|
hashCode | back 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.
|
hashCode | back 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.
|
hashCode | back 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.
|
hashCode | back 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.
|
hashCode | back to summary |
---|---|
private static int hashCode(int result, byte[] a, int fromIndex, int length) |
hashCode | back to summary |
---|---|
private static int hashCode(int result, char[] a, int fromIndex, int length) |
hashCode | back to summary |
---|---|
private static int hashCode(int result, short[] a, int fromIndex, int length) |
hashCode | back to summary |
---|---|
private static int hashCode(int result, int[] a, int fromIndex, int length) |
hashCodeOfUnsigned | back 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.
|
hashCodeOfUTF16 | back 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.
|
hugeLength | back to summary |
---|---|
private static int hugeLength(int oldLength, int minGrowth) |
mismatch | back to summary |
---|---|
public static int mismatch(boolean[] a, boolean[] b, int length) |
mismatch | back to summary |
---|---|
public static int mismatch(boolean[] a, int aFromIndex, boolean[] b, int bFromIndex, int length) |
mismatch | back 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.
|
mismatch | back 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.
|
mismatch | back to summary |
---|---|
public static int mismatch(char[] a, char[] b, int length) |
mismatch | back to summary |
---|---|
public static int mismatch(char[] a, int aFromIndex, char[] b, int bFromIndex, int length) |
mismatch | back to summary |
---|---|
public static int mismatch(short[] a, short[] b, int length) |
mismatch | back to summary |
---|---|
public static int mismatch(short[] a, int aFromIndex, short[] b, int bFromIndex, int length) |
mismatch | back to summary |
---|---|
public static int mismatch(int[] a, int[] b, int length) |
mismatch | back to summary |
---|---|
public static int mismatch(int[] a, int aFromIndex, int[] b, int bFromIndex, int length) |
mismatch | back to summary |
---|---|
public static int mismatch(float[] a, float[] b, int length) |
mismatch | back to summary |
---|---|
public static int mismatch(float[] a, int aFromIndex, float[] b, int bFromIndex, int length) |
mismatch | back to summary |
---|---|
public static int mismatch(long[] a, long[] b, int length) |
mismatch | back to summary |
---|---|
public static int mismatch(long[] a, int aFromIndex, long[] b, int bFromIndex, int length) |
mismatch | back to summary |
---|---|
public static int mismatch(double[] a, double[] b, int length) |
mismatch | back to summary |
---|---|
public static int mismatch(double[] a, int aFromIndex, double[] b, int bFromIndex, int length) |
newLength | back 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.
|
reverse | back to summary |
---|---|
public static <T> T[] reverse(T[] a) Reverses the elements of an array in-place.
|
toArrayReversed | back 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.
|
unsignedHashCode | back to summary |
---|---|
private static int unsignedHashCode(int result, byte[] a, int fromIndex, int length) |
utf16hashCode | back to summary |
---|---|
private static int utf16hashCode(int result, byte[] value, int fromIndex, int length) |
vectorizedHashCode | back 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.
|
vectorizedMismatch | back 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.
|