Unsafe: efficiently comparing two byte arrays

David M. Lloyd david.lloyd at redhat.com
Thu Mar 13 14:26:03 UTC 2014

On 03/13/2014 07:19 AM, Andrew Haley wrote:
> On 03/13/2014 11:57 AM, Paul Sandoz wrote:
>> Now i am in two minds to whether to add ByteBuffer.compareUnsigned or add Arrays.compareUnsigned.
>> An explosion of methods on Arrays for all types (plus to/from versions) would be annoying.
> Surely it's a no brainer?  All we have to do is add the intrinsics for
> ByteBuffers, then we get fast ByteBuffers and fast array comparisons too.
> I don't see the problem.

Why such a hangup on ByteBuffers and Arrays?  I think it'd be super 
useful to simply have methods on Long, Integer, etc:

public static void getBytesBE(long orig, byte[] dst, int off)
public static void getBytesLE(long orig, byte[] dst, int off)

public static int compareBE(long orig, byte[] other, int off)
public static int compareLE(long orig, byte[] other, int off)

public static long getLongFromBytesBE(byte[] src, int off)
public static long getLongFromBytesLE(byte[] src, int off)


It makes sense as there are already some byte- and bit-ish methods on 
there (reversing byte order for example, or finding population count, 
etc.).  If these were intrinsic, ByteBuffer could use these methods (as 
could Arrays.fill(...) and friends, if one was so inclined).  Then 
Data*Stream could use them as well perhaps.


More information about the core-libs-dev mailing list