JDK-8151163 : All Buffer implementations should leverage Unsafe unaligned accessors
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 9
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2016-03-03
  • Updated: 2022-03-04
  • Resolved: 2016-07-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b131Fixed
Related Reports
Blocks :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
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.
Comments
URL: http://hg.openjdk.java.net/jdk9/jdk9/hotspot/rev/f9dcefa42da3 User: lana Date: 2016-08-10 20:19:27 +0000
10-08-2016

URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/8b4f9c8902e3 User: lana Date: 2016-08-10 20:19:27 +0000
10-08-2016

I don't know what this does but it seems to cause the linked bug.
22-07-2016

URL: http://hg.openjdk.java.net/jdk9/hs/jdk/rev/8b4f9c8902e3 User: psandoz Date: 2016-07-15 14:08:39 +0000
15-07-2016

URL: http://hg.openjdk.java.net/jdk9/hs/hotspot/rev/f9dcefa42da3 User: psandoz Date: 2016-07-15 14:08:37 +0000
15-07-2016

In progress webrev: http://cr.openjdk.java.net/~psandoz/jdk9/JDK-8151163-buffer-unsafe-unaligned-access/webrev/
04-03-2016

test/compiler/intrinsics/unsafe/HeapByteBufferTest.java should be modified/cloned to support the testing of direct buffers and views of both heap and direct.
04-03-2016

The buffer views over ByteBuffer cannot utilize unsafe unaligned access until JDK-8149469 is fixed. Then the accessors can leverage the double addressing mode to support heap and direct buffers.
04-03-2016