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

Peter Levart peter.levart at gmail.com
Tue Feb 24 15:40:59 UTC 2015


On 02/24/2015 02:48 PM, Andrew Haley wrote:
>> >private static final boolean IS_UNALIGNED = theUnsafe.unalignedAccess();
>> >
>> >public void putIntUnaligned(Object o, long offset, int x) { if (IS_UNALIGNED || (offset & 3) == 0) { putInt(o, offset, x); } else if (byteOrder == BIG_ENDIAN) { putIntB(o, offset, x); } else { putIntL(o, offset, x); } }
> Yes.  It certainly could be done like this but I think C1 doesn't do
> the optimization to remove the IS_UNALIGNED test, so we'd still want
> the C1 builtins.  Perhaps we could do without the C2 builtins but they
> cost very little, they save C2 a fair amount of work, and they remove
> the vagaries of inlining.  I take your point about the interpreter,
> though.
>

What about if you make unalignedAccess() and getByteOrder() static 
methods in Unsafe (they are safe aren't they?) and then do the following:

public abstract class Unsafe {
...
private static final Unsafe theUnsafe =
   unalignedAccess()
     ? (getByteOrder() ? new UnsafeUB() : new UnsafeUL())
     : (getByteOrder() ? new UnsafeAB() : new UnsafeAL());
...
     public abstract int getIntUnaligned(Object o, long offset);
...
   private static final class UnsafeUB extends Unsafe { ... }
   private static final class UnsafeUL extends Unsafe { ... }
   private static final class UnsafeAB extends Unsafe { ... }
   private static final class UnsafeAL extends Unsafe { ... }


There will be only one runtime Unsafe sub-type ever observed in a 
particular VM.

Peter




More information about the core-libs-dev mailing list