Array equality, comparison and mismatch
Tagir F. Valeev
amaembo at gmail.com
Wed Sep 23 06:55:20 UTC 2015
Quite interesting feature. Isn't it a typo here?
419 if (!Float.isNaN(a[aFromIndex + i]) || !Float.isNaN(b[aFromIndex + i]))
487 if (!Double.isNaN(a[aFromIndex + i]) || !Double.isNaN(b[aFromIndex + i]))
Seems that it should be b[bFromIndex + i], not b[aFromIndex + i] in
As for methods like compare(byte a, int aFromIndex, int aToIndex,
byte b, int bFromIndex, int bToIndex)
Is it better to introduce two to-indices instead of single length
argument (like in System.arraycopy)? Six method arguments looks
a little bit crowded.
As for method count, it would be really nice and user-friendly were it
be possible to add methods to the array types themselves, so users can
int mismatchPos = b1.mismatch(b2);
Comparator<byte> cmp = byte::compareTo;
This somehow works with clone() method (I guess, there is some very
special code in javac for this). There could be some special abstract
classes like java.lang.ByteArray with private constructor (using
this[idx] to access elements) and all byte arrays could extend them,
so JDK developers could add new methods to arrays without modifying
the compiler. Well, that's just my fantasies about the better
With best regards,
PS> Please review the following which adds methods to Arrays for
PS> performing equality, comparison and mismatch:
PS> The motivation behind this is the use of Unsafe in popular
PS> libraries and frameworks to speed up the lexicographical comparison of byte arrays.
PS> This issue focuses on the API and functional implementations. A
PS> follow up issue  tracks updating the implementations to use a
PS> common method that leverages Unsafe to improve performance. A
PS> further issue  tracks the intrinsification of that common
PS> method to support operating on > 64 bits in width and further
PS> improve performance. A further issue, yet to be created, will
PS> follow up on updating existing JDK code to use the public and/or
PS> internal methods where appropriate. Example candidates include
PS> String (compareTo, perhaps add a mismatch method and possibly
PS> reviewing existing intrinsics, including those for compact
PS> Strings), and managed and direct Buffers.
PS> So far i have only documented the new methods operating on
PS> byte, as that will act as the template for the other methods.
PS> Some points:
PS> - Methods operating on Object will compare Object elements
PS> using Object.equals or associated comparators (as is the case for
PS> the existing equals method operating on Object).
PS> - Methods operating on float or double will check such array
PS> elements for equality using the IEEE bit layout (as is the case
PS> for the existing equals method operating on float or double).
PS> - Primitive array element comparison will be performed as if by
PS> the boxed primitive type’s compare or compareUnsigned method.
PS> - Range-accepting methods do not support null array values.
PS> - Non-range and range-accepting mismatch methods do not support
PS> null array values. (What value should be returned when a mismatch
PS> is performed on a null array and an empty array)?
PS> - Speculation: it may be possible that Project Valhalla will
PS> enable us to “compress” down methods of certain groups to one
PS> “template” method. Even if that is not possible i am not overly
PS> concerned about the number of methods added, which represents the
PS> maximum set. We could reduce them without a fundamental loss of
PS> functionality, but that might have a “semantic” loss requiring
PS> developers to roll their own wrappers.
PS>  https://bugs.openjdk.java.net/browse/JDK-8136924
PS>  https://bugs.openjdk.java.net/browse/JDK-8044082
More information about the core-libs-dev