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

All Buffer implementations should leverage Unsafe unaligned accessors

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 9
    • core-libs
    • None
    • b131

      JDK-8026049 updated HeapByteBuffer such that the put/get methods leveraged the Unsafe.get/put*Unaligned methods.

      The unsafe methods are intrinsic on x86, they are essentially aliases to the equivalent Unsafe.get/put*. For platforms that do not support misaligned access the methods are more efficient than the functionally equivalent methods in java.nio.Bits, since the widest atomic access is utlized depending on the result of the alignment, specifically address to the first byte modulo the unit size.

      Direct ByteBuffers should be updated to use these methods, rather than doing this:

          private int getInt(long a) {
              if (unaligned) {
                  int x = unsafe.getInt(a);
                  return (nativeByteOrder ? x : Bits.swap(x));
              }
              return Bits.getInt(a, bigEndian);
          }

          private ByteBuffer putInt(long a, int x) {

              if (unaligned) {
                  int y = (x);
                  unsafe.putInt(a, (nativeByteOrder ? y : Bits.swap(y)));
              } else {
                  Bits.putInt(a, x, bigEndian);
              }
              return this;



          }

      This then will ensure that any architecture that can intrinsify the unaligned accessor will further benefit without requiring modification to buffer code.

      Furthermore, the wider views of ByteBuffer can also be consolidated using such access. The int view for a heap ByteBuffer performs per-byte access leveraging code in java.nio.Bits. The int view for a direct ByteBuffer has four separate implementations (a cross product of alignment and endianess), and at least one axis can be removed.

            psandoz Paul Sandoz
            psandoz Paul Sandoz
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: