JEP draft: Efficient array comparison intrinsics

Created2014/05/27 23:53
Updated2016/03/04 12:40

(rough draft; needs formatting; see

There are efficient operations (both safe and unsafe) for copying arrays, but none for comparing them. Let's fix this so users don't have to roll their own.

At a minimum, the following intrinsic would allow suitable higher-level comparison operations to be implemented:

/** Find the index of a mismatch between src and dest, or -1 if none. */
System.arrayMismatch((Object src, int srcPos,
    Object dest, int destPos, int length, boolean fromEnd);

This mechanism is easy to code and easy to vectorize. Most higher-level array comparison operations can readily be built on top of it.

Basic comparison operations (at least lexicographic compareTo) using it should be added to java.util.Arrays.

String.compareTo can be re-implemented on top of this intrinsic. Methods of the form String.mismatchTo should be added.

As a possible follow-up, System.arrayIndexOf could be added to perform a vectorized non-anchored search of one array inside another. This loop is less interesting, however, since it would be too narrow for some use cases involving NaNs, structural object comparisons, or case-insensitive searches.