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

Peter Levart peter.levart at gmail.com
Mon Mar 9 11:32:29 UTC 2015


On 03/09/2015 10:11 AM, Andrew Haley wrote:
> On 08/03/15 23:21, Remi Forax wrote:
>> BE doesn't need to be an instance, method of Unsafe are instance methods
>> just to enforce security,
>> you can not call them if you don"t have an instance of Unsafe (and
>> getUnsafe() does the security check).
>>
>> BE is private so it doesn't have to be an instance field it can be
>> static field.
>>
>> And I agree with you that the native isBigEndian() should be called once
>> in the lifetime of the VM.
>>
>> so the idea is to write:
>>
>> public boolean isBigEndian() {
>>     return BE;
>> }
>> private static final boolean BE = isBigEndian0();
>>
>> private static native boolean isBigEndian0();
> I understand.  However, the callers of isBigEndian() do their own
> caching, so it seems to me that this will be a case of double-caching.
> It's not important from a performance point of view, but it is
> additional complexity.
>
> Andrew.

If we take a look at status quo, then the pattern for safe constant 
state in Unsafe is as follows:

     public native int arrayBaseOffset(Class<?> arrayClass);

     /** The value of {@code arrayBaseOffset(boolean[].class)} */
     public static final int ARRAY_BOOLEAN_BASE_OFFSET
             = theUnsafe.arrayBaseOffset(boolean[].class);


...this has the benefit that it doesn't require caching at use site. But 
the above pattern could be tweaked a bit in case of isBigEndian(). The 
method could be declared as private since it does not add a utility like 
above method which is a function of 1 argument.

The argument that we don't want to expose public API is unfounded as 
Unsafe won't be visible to normal code in JDK9, right?

Regards, Peter





More information about the core-libs-dev mailing list