Summary
The alignment of array elements with a size smaller than or equals 4 bytes on 64-bit platforms is relaxed such that array elements can start at 4-byte aligned address, rather than 8-byte aligned address. This allows to utilize the alignment gap between the array-length-field and the start of the array elements, when running with -UseCompressedClassPointers and in the future with +UseCompactObjectHeaders.
Problem
Currently, arrays are laid out in memory like this (each line representing 8 bytes/ 1 word, each block representing 4 bytes):
+-----------------------------------------+
| Mark-word |
+--------------------------+--------------+
| Compressed Class Pointer | Array length |
+--------------------------+--------------+
| Array elements... |
However, when running without compressed class pointers (-UseCompressedClassPointers), it looks like this:
+-----------------------------------------+
| Mark-word |
+-----------------------------------------+
| Class Pointer |
+----------------------+------------------+
| Array length | Alignment gap |
+----------------------+------------------+
| Array elements... |
Similarily, with the upcoming JEP 450: Compact Object Headers, it would look like this:
+-----------------------------------------+
| Mark-word / CompressedClassPointer |
+----------------------+------------------+
| Array length | Alignment gap |
+----------------------+------------------+
| Array elements... |
It would be better to use the 4-bytes alignment gap for the array elements, instead:
+-----------------------------------------+
| Mark-word / CompressedClassPointer |
+----------------------+------------------+
| Array length | Array elements |
+----------------------+------------------+
| Array elements... |
Solution
The proposed change relaxes the alignment requirements for array types with element sizes of 4 bytes or less - byte[], boolean[], char[], short[], int[], float[] and Object[] (when running with -XX:+UseCompressedOops, which is the default for many heap configurations) such that the array elements can start at 4-byte aligned addresses. long[], double[] and Object[] (with -COOPS) remain unaffected.
Specification
Not sure how to specify this. The PR has all the relevant changes, but is quite large and not very useful, I suppose. The above problem and solution description captures the change better, IMO.
https://patch-diff.githubusercontent.com/raw/openjdk/jdk/pull/11044.diff
- csr of
-
JDK-8139457 Relax alignment of array elements
- Resolved
- relates to
-
JDK-8318967 (bf) ByteBuffer::alignedSlice and alignmentOffset should not promise heap alignment
- Closed
-
JDK-8318966 Some methods make promises about Java array element alignment that are too strong
- Resolved