Unsafe.{get,put}-X-Unaligned; Efficient array comparison intrinsics

Andrew Haley aph at redhat.com
Mon Feb 23 18:13:37 UTC 2015


I've been kicking around a few ideas for Unsafe access methods for
unaligned access to byte arrays and buffers in order to provide
"whatever second-best mechanism the platform offers".  These would
provide the base for fast lexicographic array comparisons, etc.

https://bugs.openjdk.java.net/browse/JDK-8044082

If the platform supports unaligned memory accesses, the implementation
of {get,put}-X-Unaligned is obvious and trivial for both C1 and C2.
It gets interesting when we want to provide efficient unaligned
methods on machines with no hardware support.

We could provide compiler intrinsics which do when we need on such
machines.  However, I think this wouldn't deliver the best results.
>From the experiments I've done, the best implementation is to write
the access methods in Java and allow HotSpot to optimize them.  While
this seemed a bit counter-intuitive to me, it's best because C2 has
profile data that it can work on.  In many cases I suspect that data
read and written from a byte array will be aligned for their type and
C2 will take advantage of this, relegating the misaligned access to an
out-of-line code path as appropriate.  Also, these methods have the
additional benefit that they are always atomic as long as the data are
naturally aligned.

This does result in rather a lot of code for the methods for all sizes
and endiannesses, but none of it will be used on machines with
unaligned hardware support except in the interpreter.  (Perhaps the
interpreter too could have intrinsics?)

I have changed HeapByteBuffer to use these methods, with a major
performance improvement.  I've also provided Unsafe methods to query
endianness and alignment support.

Webrevs at http://cr.openjdk.java.net/~aph/unaligned.hotspot.1/
http://cr.openjdk.java.net/~aph/unaligned.jdk.1/

Andrew.



More information about the core-libs-dev mailing list