RFR: 8139457: Array bases are aligned at HeapWord granularity [v55]

Jorn Vernee jvernee at openjdk.org
Tue Oct 24 16:03:01 UTC 2023


On Tue, 24 Oct 2023 15:27:38 GMT, Paul Sandoz <psandoz at openjdk.org> wrote:

>     2. `ByteBuffer::alignedSlice` and `ByteBuffer::alignmentOffset`
>        -Deprecate, not for removal, explaining limitations and referring to use of direct buffers, native memory segments, or heap memory segments covering `long[]` arrays.
>        -Update implementations to replace hard coded unit size of 8 to a runtime query for `byte[]`. Can we use `Unsafe.arrayBaseOffset`? (This is the only recommended implementation update.)
>        -Update documentation for implementation notes and for `throws UnsupportedOperationException`.

The restrictions on `alignedSlice` are inherited from `alignmentOffset`. The documentation of the latter has this:


     * @throws UnsupportedOperationException
     *         If the native platform does not guarantee stable alignment offset
     *         values for the given unit size when managing the memory regions
     *         of buffers of the same kind as this buffer (direct or
     *         non-direct).  For example, if garbage collection would result
     *         in the moving of a memory region covered by a non-direct buffer
     *         from one location to another and both locations have different
     *         alignment characteristics.


Since "guarantee" is used here, I think the current acceptance of `unitSize > 1` for heap buffers can be framed as a bug in the implementation. Since, there is no guarantee on the alignment beyond the natural alignment of the array elements (which for `byte` is 1).

I think uniformly rejecting `unitSize > 1` for heap buffers in all configurations would be preferable, though, rather than relying on the alignment the VM chooses, which can change based on the used VM flags (?).
> 
>     3. `MethodHandles::byteArrayViewVarHandle` and `MethodHandles::byteBufferViewVarHandle`
>        -Deprecate, not for removal, explaining limitations and referring to use of direct buffers, native memory segments, or heap memory segments covering `long[]` arrays.

For both 2. and 3., I think deprecation is perhaps a bit too strong... `alignedSlice` and `alignmentOffset` are well-formed for off-heap buffers, and `byteArrayViewVarHandle` and `byteBufferViewVarHandle` work well with plain access modes. I think we don't want to discourage using these APIs for those well-formed use cases? Also, since we are already changing the implementation, and changing the observable behavior, maybe we should just change it to do the right thing once and for all (and update the spec accordingly)?

-------------

PR Comment: https://git.openjdk.org/jdk/pull/11044#issuecomment-1777543126


More information about the hotspot-dev mailing list