Unsafe: efficiently comparing two byte arrays
paul.sandoz at oracle.com
Wed Feb 26 15:42:48 UTC 2014
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  and a number of apache frameworks for similar code.
One solution is to provide such functionality in Arrays for all primitives and probably refs :
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.
More information about the core-libs-dev