--- old/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template 2015-03-06 18:36:49.077282077 +0000 +++ new/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template 2015-03-06 18:36:48.727317015 +0000 @@ -27,6 +27,7 @@ package java.nio; +import sun.misc.Unsafe; /** #if[rw] @@ -52,6 +53,16 @@ #end[rw] */ +#if[byte] + + // Cached unsafe-access object + private static final Unsafe unsafe = Bits.unsafe(); + + // Cached array base offset + private static final long arrayBaseOffset = unsafe.arrayBaseOffset($type$[].class); + +#end[byte] + Heap$Type$Buffer$RW$(int cap, int lim) { // package-private #if[rw] super(-1, 0, lim, cap, new $type$[cap], 0); @@ -131,6 +142,12 @@ return i + offset; } +#if[byte] + private long byteOffset(long i) { + return arrayBaseOffset + i + offset; + } +#end[byte] + public $type$ get() { return hb[ix(nextGetIndex())]; } @@ -256,18 +273,18 @@ #if[rw] public char getChar() { - return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian); + return unsafe.getCharUnaligned(hb, byteOffset(nextGetIndex(2)), bigEndian); } public char getChar(int i) { - return Bits.getChar(this, ix(checkIndex(i, 2)), bigEndian); + return unsafe.getCharUnaligned(hb, byteOffset(checkIndex(i, 2)), bigEndian); } #end[rw] public $Type$Buffer putChar(char x) { #if[rw] - Bits.putChar(this, ix(nextPutIndex(2)), x, bigEndian); + unsafe.putCharUnaligned(hb, byteOffset(nextPutIndex(2)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -276,7 +293,7 @@ public $Type$Buffer putChar(int i, char x) { #if[rw] - Bits.putChar(this, ix(checkIndex(i, 2)), x, bigEndian); + unsafe.putCharUnaligned(hb, byteOffset(checkIndex(i, 2)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -307,18 +324,18 @@ #if[rw] public short getShort() { - return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian); + return unsafe.getShortUnaligned(hb, byteOffset(nextGetIndex(2)), bigEndian); } public short getShort(int i) { - return Bits.getShort(this, ix(checkIndex(i, 2)), bigEndian); + return unsafe.getShortUnaligned(hb, byteOffset(checkIndex(i, 2)), bigEndian); } #end[rw] public $Type$Buffer putShort(short x) { #if[rw] - Bits.putShort(this, ix(nextPutIndex(2)), x, bigEndian); + unsafe.putShortUnaligned(hb, byteOffset(nextPutIndex(2)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -327,7 +344,7 @@ public $Type$Buffer putShort(int i, short x) { #if[rw] - Bits.putShort(this, ix(checkIndex(i, 2)), x, bigEndian); + unsafe.putShortUnaligned(hb, byteOffset(checkIndex(i, 2)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -358,18 +375,18 @@ #if[rw] public int getInt() { - return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian); + return unsafe.getIntUnaligned(hb, byteOffset(nextGetIndex(4)), bigEndian); } public int getInt(int i) { - return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian); + return unsafe.getIntUnaligned(hb, byteOffset(checkIndex(i, 4)), bigEndian); } #end[rw] public $Type$Buffer putInt(int x) { #if[rw] - Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian); + unsafe.putIntUnaligned(hb, byteOffset(nextPutIndex(4)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -378,7 +395,7 @@ public $Type$Buffer putInt(int i, int x) { #if[rw] - Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian); + unsafe.putIntUnaligned(hb, byteOffset(checkIndex(i, 4)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -409,18 +426,18 @@ #if[rw] public long getLong() { - return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian); + return unsafe.getLongUnaligned(hb, byteOffset(nextGetIndex(8)), bigEndian); } public long getLong(int i) { - return Bits.getLong(this, ix(checkIndex(i, 8)), bigEndian); + return unsafe.getLongUnaligned(hb, byteOffset(checkIndex(i, 8)), bigEndian); } #end[rw] public $Type$Buffer putLong(long x) { #if[rw] - Bits.putLong(this, ix(nextPutIndex(8)), x, bigEndian); + unsafe.putLongUnaligned(hb, byteOffset(nextPutIndex(8)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -429,7 +446,7 @@ public $Type$Buffer putLong(int i, long x) { #if[rw] - Bits.putLong(this, ix(checkIndex(i, 8)), x, bigEndian); + unsafe.putLongUnaligned(hb, byteOffset(checkIndex(i, 8)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -460,18 +477,19 @@ #if[rw] public float getFloat() { - return Bits.getFloat(this, ix(nextGetIndex(4)), bigEndian); + return getFloat(nextGetIndex(4)); } public float getFloat(int i) { - return Bits.getFloat(this, ix(checkIndex(i, 4)), bigEndian); + int x = unsafe.getIntUnaligned(hb, byteOffset(checkIndex(i, 4)), bigEndian); + return Float.intBitsToFloat(x); } #end[rw] public $Type$Buffer putFloat(float x) { #if[rw] - Bits.putFloat(this, ix(nextPutIndex(4)), x, bigEndian); + putFloat(nextPutIndex(4), x); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -480,7 +498,8 @@ public $Type$Buffer putFloat(int i, float x) { #if[rw] - Bits.putFloat(this, ix(checkIndex(i, 4)), x, bigEndian); + int y = Float.floatToRawIntBits(x); + unsafe.putIntUnaligned(hb, byteOffset(checkIndex(i, 4)), y, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -511,18 +530,19 @@ #if[rw] public double getDouble() { - return Bits.getDouble(this, ix(nextGetIndex(8)), bigEndian); + return getDouble(nextGetIndex(8)); } public double getDouble(int i) { - return Bits.getDouble(this, ix(checkIndex(i, 8)), bigEndian); + long x = unsafe.getLongUnaligned(hb, byteOffset(checkIndex(i, 8)), bigEndian); + return Double.longBitsToDouble(x); } #end[rw] public $Type$Buffer putDouble(double x) { #if[rw] - Bits.putDouble(this, ix(nextPutIndex(8)), x, bigEndian); + putDouble(nextPutIndex(8), x); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -531,7 +551,8 @@ public $Type$Buffer putDouble(int i, double x) { #if[rw] - Bits.putDouble(this, ix(checkIndex(i, 8)), x, bigEndian); + long y = Double.doubleToRawLongBits(x); + unsafe.putLongUnaligned(hb, byteOffset(checkIndex(i, 4)), y, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException();