Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8314882

Relax alignment of array elements

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 23
    • hotspot
    • None
    • behavioral
    • low
    • Hide
      The VarHandle API allows long[]-views of byte[]. Many operations throw an IllegalStateException when misaligned access is attempted. For example, a compareAndSet() on index 0 of such a long[]-wrapped byte[] would previously have worked, while throwing an IllegalStateException with the proposed change.
      Similar to that, arrays that are fetched via GetPrimitiveArrayCritical, and operated upon under the assumption of particular alignment (e.g. using SIMD instructions) might break.
      Show
      The VarHandle API allows long[]-views of byte[]. Many operations throw an IllegalStateException when misaligned access is attempted. For example, a compareAndSet() on index 0 of such a long[]-wrapped byte[] would previously have worked, while throwing an IllegalStateException with the proposed change. Similar to that, arrays that are fetched via GetPrimitiveArrayCritical, and operated upon under the assumption of particular alignment (e.g. using SIMD instructions) might break.
    • Implementation

      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

            rkennke Roman Kennke
            shade Aleksey Shipilev
            Coleen Phillimore, Jorn Vernee
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: