Unsafe for array access
Paul Sandoz
paul.sandoz at oracle.com
Mon May 5 15:20:36 UTC 2014
Hi,
As part of some investigatory work for JEP 193: Enhanced Volatiles i have been looking at how enhanced (and safe) access to array elements could be implemented.
The obvious and simple choice is to reach for unsafe with a few safety checks, for example:
@ForceInline
static void setVolatile(ArrayRefHandle handle, Object[] array, int index,
Object value) {
if (index < 0 || index >= array.length) // bounds and null check
throw new ArrayIndexOutOfBoundsException();
UNSAFE.putObjectVolatile(array,
(((long) index) << handle.ashift) + handle.abase,
castReference(handle.componentType, value));
}
The problem is the compiler does fully recognize an array access is going on and so certain optimizations tend not to kick in, such as removing or strength reducing bounds checks, or treating "index" as a signed value rather than unsigned.
Any clues/pointers on how i can hack things so that hotspot knows that an array access is going on in the above code, and views it as almost equivalent to following in terms of optimizations?
@ForceInline
static void setVolatile(ArrayRefHandle handle, Object[] array, int index,
Object value) {
if (index < 0 || index >= array.length) // bounds and null check
throw new ArrayIndexOutOfBoundsException();
array[i] = castReference(handle.componentType, value);
}
Some context: ForkJoinPool is peppered with code like the following:
int i = (((a.length - 1) & b) << ASHIFT) + ABASE;
ForkJoinTask<?> t =
(ForkJoinTask<?>)U.getObjectVolatile(a, i);
Any viable replacement for direct Unsafe usages in that code will want those strength reducing optimizations to kick in.
Paul.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 841 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20140505/22512f76/signature-0001.asc>
More information about the hotspot-compiler-dev
mailing list