RFR 8149469: ByteBuffer API and implementation enhancements for VarHandles

Chris Hegarty chris.hegarty at oracle.com
Thu Feb 18 11:08:41 UTC 2016

On 9 Feb 2016, at 15:05, Paul Sandoz <paul.sandoz at oracle.com> wrote:

> Hi,
> This patch contains API and internal enhancements to ByteBuffer:
> http://cr.openjdk.java.net/~psandoz/jdk9/JDK-8149469-byte-buffer-align-and-unifying-enhancements/webrev/index.html

This is certainly for advanced/power users feature. I expect it may not be
used all that widely, but I do see your requirement with VarHandles.

A few specific comments:

 - I find the comment in Buffer.java a little confusing. Specifically where is
   says that byte[] allocations might no align to a word boundary on 32-bit
   systems. I think you mean that the alignment will not be 8-byte aligned.
   I get that slices can be arbitrary.

 - The changes to set isReadOnly do not seem related, but you explained
   off-list that these are in preparation for VarHandlers. They seem benign.

 - The computation in misalignment is based on assumptions about layout,
   that may, or may not, be valid ( but it is had to see otherwise ). You had
   suggested that UOE could be thrown here? 

 - Minor spec issue in alignedSlice: "If rounding results in out-of-bound values
   then this buffers capacity and limit will be zero.”. You mean "the new byte
   buffer", right?


> I am CC’ing hotspot-compiler because i want to push to hs-comp to reduce time delays for reviewing/pushing dependent VarHandle patches (which also depends on the Unsafe enhancements currently under review that are also to be pushed to hs-comp).
> The API enhancements provide support for querying misalignment of an index and unit size, and also slicing a buffer to align for a unit size. Such functionality enables one to set up for aligned access for unit sizes bytes, if supported by the platform.
> The internal enhancements enable consistent use of fields readOnly and address. For the moment i have only enabled consistent use of address for ByteBuffers.
> A primary motivation for these enhancements is too support VarHandles access to ByteBuffer contents. The API enhancement enables access to values at known aligned indexes and therefore provides stronger atomicity guarantees and enhanced atomic accesses that follow from that. The internal enhancement allows VarHandle implementations to uniformly access buffers using the Unsafe double addressing mode [*].
> I believe these enhancements are also valuable independently of the VarHandles work. Furthermore, the internal enhancements are a stepping stone to potentially consolidating many aspects of heap and direct buffers.
> Thanks,
> Paul.
> [*]
> For heap buffers
>  Buffer.address == Unsafe.ARRAY_BYTE_BASE_OFFSET + offset;
>  ByteBuffer.hb != null,
> For direct buffers
>  Buffer.address == <base address of region>;
>  ByteBuffer.hb == null,

More information about the hotspot-compiler-dev mailing list