Unsafe: efficiently comparing two byte arrays
Paul Sandoz
paul.sandoz at oracle.com
Wed Feb 26 15:42:48 UTC 2014
Hi,
A common reason why Unsafe is used is to more efficiently compare two unsigned byte arrays, viewing those byte arrays as long arrays. See Guava [1] and a number of apache frameworks for similar code.
One solution is to provide such functionality in Arrays for all primitives and probably refs [2]:
int Arrays.compare(byte[], byte[]);
Then it is easy to create a comparator using a method reference:
Comparator<byte[]> c = Arrays::compare;
There could, initially, be Java implementations for those methods, including using Unsafe for byte[]. I gather those methods could be intrinsified to implementations using SIMD instructions on supported platforms. I don't know if that is possible today with Hotspot, but regardless i think a good start would be to have Java-based implementations in place.
Thoughts?
Paul.
[1] http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/primitives/UnsignedBytes.html#lexicographicalComparator()
[2] https://bugs.openjdk.java.net/browse/JDK-8033148
More information about the core-libs-dev
mailing list