Make field access via ByteBuf.read/write*() faster by avoiding unnecessary boundary checks

- also disabled a time consuming test that is actually a regression test
This commit is contained in:
Trustin Lee 2013-03-06 10:32:29 +09:00
parent 5f2c2cdc9b
commit 1c1570ffc4
11 changed files with 430 additions and 171 deletions

View File

@ -342,6 +342,14 @@ public abstract class AbstractByteBuf implements ByteBuf {
return swappedBuf;
}
@Override
public byte getByte(int index) {
checkIndex(index);
return _getByte(index);
}
protected abstract byte _getByte(int index);
@Override
public boolean getBoolean(int index) {
return getByte(index) != 0;
@ -352,11 +360,27 @@ public abstract class AbstractByteBuf implements ByteBuf {
return (short) (getByte(index) & 0xFF);
}
@Override
public short getShort(int index) {
checkIndex(index, 2);
return _getShort(index);
}
protected abstract short _getShort(int index);
@Override
public int getUnsignedShort(int index) {
return getShort(index) & 0xFFFF;
}
@Override
public int getUnsignedMedium(int index) {
checkIndex(index, 3);
return _getUnsignedMedium(index);
}
protected abstract int _getUnsignedMedium(int index);
@Override
public int getMedium(int index) {
int value = getUnsignedMedium(index);
@ -366,11 +390,27 @@ public abstract class AbstractByteBuf implements ByteBuf {
return value;
}
@Override
public int getInt(int index) {
checkIndex(index, 4);
return _getInt(index);
}
protected abstract int _getInt(int index);
@Override
public long getUnsignedInt(int index) {
return getInt(index) & 0xFFFFFFFFL;
}
@Override
public long getLong(int index) {
checkIndex(index, 8);
return _getLong(index);
}
protected abstract long _getLong(int index);
@Override
public char getChar(int index) {
return (char) getShort(index);
@ -414,24 +454,69 @@ public abstract class AbstractByteBuf implements ByteBuf {
return this;
}
@Override
public ByteBuf setByte(int index, int value) {
checkIndex(index);
_setByte(index, value);
return this;
}
protected abstract void _setByte(int index, int value);
@Override
public ByteBuf setBoolean(int index, boolean value) {
setByte(index, value ? 1 : 0);
return this;
}
@Override
public ByteBuf setShort(int index, int value) {
checkIndex(index, 2);
_setShort(index, value);
return this;
}
protected abstract void _setShort(int index, int value);
@Override
public ByteBuf setChar(int index, int value) {
setShort(index, value);
return this;
}
@Override
public ByteBuf setMedium(int index, int value) {
checkIndex(index, 3);
_setMedium(index, value);
return this;
}
protected abstract void _setMedium(int index, int value);
@Override
public ByteBuf setInt(int index, int value) {
checkIndex(index, 4);
_setInt(index, value);
return this;
}
protected abstract void _setInt(int index, int value);
@Override
public ByteBuf setFloat(int index, float value) {
setInt(index, Float.floatToRawIntBits(value));
return this;
}
@Override
public ByteBuf setLong(int index, long value) {
checkIndex(index, 8);
_setLong(index, value);
return this;
}
protected abstract void _setLong(int index, long value);
@Override
public ByteBuf setDouble(int index, double value) {
setLong(index, Double.doubleToRawLongBits(value));
@ -503,7 +588,7 @@ public abstract class AbstractByteBuf implements ByteBuf {
if (readerIndex == writerIndex) {
throw new IndexOutOfBoundsException("readerIndex(" + readerIndex + ") == writerIndex(" + writerIndex + ')');
}
return getByte(readerIndex ++);
return _getByte(readerIndex++);
}
@Override
@ -519,7 +604,7 @@ public abstract class AbstractByteBuf implements ByteBuf {
@Override
public short readShort() {
checkReadableBytes(2);
short v = getShort(readerIndex);
short v = _getShort(readerIndex);
readerIndex += 2;
return v;
}
@ -541,7 +626,7 @@ public abstract class AbstractByteBuf implements ByteBuf {
@Override
public int readUnsignedMedium() {
checkReadableBytes(3);
int v = getUnsignedMedium(readerIndex);
int v = _getUnsignedMedium(readerIndex);
readerIndex += 3;
return v;
}
@ -549,7 +634,7 @@ public abstract class AbstractByteBuf implements ByteBuf {
@Override
public int readInt() {
checkReadableBytes(4);
int v = getInt(readerIndex);
int v = _getInt(readerIndex);
readerIndex += 4;
return v;
}
@ -562,7 +647,7 @@ public abstract class AbstractByteBuf implements ByteBuf {
@Override
public long readLong() {
checkReadableBytes(8);
long v = getLong(readerIndex);
long v = _getLong(readerIndex);
readerIndex += 8;
return v;
}

View File

@ -556,55 +556,60 @@ public class DefaultCompositeByteBuf extends AbstractReferenceCountedByteBuf
@Override
public byte getByte(int index) {
return _getByte(index);
}
@Override
protected byte _getByte(int index) {
Component c = findComponent(index);
return c.buf.getByte(index - c.offset);
}
@Override
public short getShort(int index) {
protected short _getShort(int index) {
Component c = findComponent(index);
if (index + 2 <= c.endOffset) {
return c.buf.getShort(index - c.offset);
} else if (order() == ByteOrder.BIG_ENDIAN) {
return (short) ((getByte(index) & 0xff) << 8 | getByte(index + 1) & 0xff);
return (short) ((_getByte(index) & 0xff) << 8 | _getByte(index + 1) & 0xff);
} else {
return (short) (getByte(index) & 0xff | (getByte(index + 1) & 0xff) << 8);
return (short) (_getByte(index) & 0xff | (_getByte(index + 1) & 0xff) << 8);
}
}
@Override
public int getUnsignedMedium(int index) {
protected int _getUnsignedMedium(int index) {
Component c = findComponent(index);
if (index + 3 <= c.endOffset) {
return c.buf.getUnsignedMedium(index - c.offset);
} else if (order() == ByteOrder.BIG_ENDIAN) {
return (getShort(index) & 0xffff) << 8 | getByte(index + 2) & 0xff;
return (_getShort(index) & 0xffff) << 8 | _getByte(index + 2) & 0xff;
} else {
return getShort(index) & 0xFFFF | (getByte(index + 2) & 0xFF) << 16;
return _getShort(index) & 0xFFFF | (_getByte(index + 2) & 0xFF) << 16;
}
}
@Override
public int getInt(int index) {
protected int _getInt(int index) {
Component c = findComponent(index);
if (index + 4 <= c.endOffset) {
return c.buf.getInt(index - c.offset);
} else if (order() == ByteOrder.BIG_ENDIAN) {
return (getShort(index) & 0xffff) << 16 | getShort(index + 2) & 0xffff;
return (_getShort(index) & 0xffff) << 16 | _getShort(index + 2) & 0xffff;
} else {
return getShort(index) & 0xFFFF | (getShort(index + 2) & 0xFFFF) << 16;
return _getShort(index) & 0xFFFF | (_getShort(index + 2) & 0xFFFF) << 16;
}
}
@Override
public long getLong(int index) {
protected long _getLong(int index) {
Component c = findComponent(index);
if (index + 8 <= c.endOffset) {
return c.buf.getLong(index - c.offset);
} else if (order() == ByteOrder.BIG_ENDIAN) {
return (getInt(index) & 0xffffffffL) << 32 | getInt(index + 4) & 0xffffffffL;
return (_getInt(index) & 0xffffffffL) << 32 | _getInt(index + 4) & 0xffffffffL;
} else {
return getInt(index) & 0xFFFFFFFFL | (getInt(index + 4) & 0xFFFFFFFFL) << 32;
return _getInt(index) & 0xFFFFFFFFL | (_getInt(index + 4) & 0xFFFFFFFFL) << 32;
}
}
@ -751,64 +756,85 @@ public class DefaultCompositeByteBuf extends AbstractReferenceCountedByteBuf
return this;
}
@Override
protected void _setByte(int index, int value) {
setByte(index, value);
}
@Override
public CompositeByteBuf setShort(int index, int value) {
return (CompositeByteBuf) super.setShort(index, value);
}
@Override
protected void _setShort(int index, int value) {
Component c = findComponent(index);
if (index + 2 <= c.endOffset) {
c.buf.setShort(index - c.offset, value);
} else if (order() == ByteOrder.BIG_ENDIAN) {
setByte(index, (byte) (value >>> 8));
setByte(index + 1, (byte) value);
_setByte(index, (byte) (value >>> 8));
_setByte(index + 1, (byte) value);
} else {
setByte(index , (byte) value);
setByte(index + 1, (byte) (value >>> 8));
_setByte(index, (byte) value);
_setByte(index + 1, (byte) (value >>> 8));
}
return this;
}
@Override
public CompositeByteBuf setMedium(int index, int value) {
return (CompositeByteBuf) super.setMedium(index, value);
}
@Override
protected void _setMedium(int index, int value) {
Component c = findComponent(index);
if (index + 3 <= c.endOffset) {
c.buf.setMedium(index - c.offset, value);
} else if (order() == ByteOrder.BIG_ENDIAN) {
setShort(index, (short) (value >> 8));
setByte(index + 2, (byte) value);
_setShort(index, (short) (value >> 8));
_setByte(index + 2, (byte) value);
} else {
setShort(index , (short) value);
setByte(index + 2, (byte) (value >>> 16));
_setShort(index, (short) value);
_setByte(index + 2, (byte) (value >>> 16));
}
return this;
}
@Override
public CompositeByteBuf setInt(int index, int value) {
return (CompositeByteBuf) super.setInt(index, value);
}
@Override
protected void _setInt(int index, int value) {
Component c = findComponent(index);
if (index + 4 <= c.endOffset) {
c.buf.setInt(index - c.offset, value);
} else if (order() == ByteOrder.BIG_ENDIAN) {
setShort(index, (short) (value >>> 16));
setShort(index + 2, (short) value);
_setShort(index, (short) (value >>> 16));
_setShort(index + 2, (short) value);
} else {
setShort(index , (short) value);
setShort(index + 2, (short) (value >>> 16));
_setShort(index, (short) value);
_setShort(index + 2, (short) (value >>> 16));
}
return this;
}
@Override
public CompositeByteBuf setLong(int index, long value) {
return (CompositeByteBuf) super.setLong(index, value);
}
@Override
protected void _setLong(int index, long value) {
Component c = findComponent(index);
if (index + 8 <= c.endOffset) {
c.buf.setLong(index - c.offset, value);
} else if (order() == ByteOrder.BIG_ENDIAN) {
setInt(index, (int) (value >>> 32));
setInt(index + 4, (int) value);
_setInt(index, (int) (value >>> 32));
_setInt(index + 4, (int) value);
} else {
setInt(index , (int) value);
setInt(index + 4, (int) (value >>> 32));
_setInt(index, (int) value);
_setInt(index + 4, (int) (value >>> 32));
}
return this;
}
@Override

View File

@ -93,26 +93,51 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf {
@Override
public byte getByte(int index) {
return _getByte(index);
}
@Override
protected byte _getByte(int index) {
return buffer.getByte(index);
}
@Override
public short getShort(int index) {
return _getShort(index);
}
@Override
protected short _getShort(int index) {
return buffer.getShort(index);
}
@Override
public int getUnsignedMedium(int index) {
return _getUnsignedMedium(index);
}
@Override
protected int _getUnsignedMedium(int index) {
return buffer.getUnsignedMedium(index);
}
@Override
public int getInt(int index) {
return _getInt(index);
}
@Override
protected int _getInt(int index) {
return buffer.getInt(index);
}
@Override
public long getLong(int index) {
return _getLong(index);
}
@Override
protected long _getLong(int index) {
return buffer.getLong(index);
}
@ -146,34 +171,59 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf {
@Override
public ByteBuf setByte(int index, int value) {
buffer.setByte(index, value);
_setByte(index, value);
return this;
}
@Override
protected void _setByte(int index, int value) {
buffer.setByte(index, value);
}
@Override
public ByteBuf setShort(int index, int value) {
buffer.setShort(index, value);
_setShort(index, value);
return this;
}
@Override
protected void _setShort(int index, int value) {
buffer.setShort(index, value);
}
@Override
public ByteBuf setMedium(int index, int value) {
buffer.setMedium(index, value);
_setMedium(index, value);
return this;
}
@Override
protected void _setMedium(int index, int value) {
buffer.setMedium(index, value);
}
@Override
public ByteBuf setInt(int index, int value) {
buffer.setInt(index, value);
_setInt(index, value);
return this;
}
@Override
protected void _setInt(int index, int value) {
buffer.setInt(index, value);
}
@Override
public ByteBuf setLong(int index, long value) {
buffer.setLong(index, value);
_setLong(index, value);
return this;
}
@Override
protected void _setLong(int index, long value) {
buffer.setLong(index, value);
}
@Override
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
buffer.setBytes(index, src, srcIndex, length);

View File

@ -41,33 +41,28 @@ final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
}
@Override
public byte getByte(int index) {
checkIndex(index);
protected byte _getByte(int index) {
return memory.get(idx(index));
}
@Override
public short getShort(int index) {
checkIndex(index, 2);
protected short _getShort(int index) {
return memory.getShort(idx(index));
}
@Override
public int getUnsignedMedium(int index) {
checkIndex(index, 3);
protected int _getUnsignedMedium(int index) {
index = idx(index);
return (memory.get(index) & 0xff) << 16 | (memory.get(index + 1) & 0xff) << 8 | memory.get(index + 2) & 0xff;
}
@Override
public int getInt(int index) {
checkIndex(index, 4);
protected int _getInt(int index) {
return memory.getInt(idx(index));
}
@Override
public long getLong(int index) {
checkIndex(index, 8);
protected long _getLong(int index) {
return memory.getLong(idx(index));
}
@ -138,41 +133,31 @@ final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
}
@Override
public ByteBuf setByte(int index, int value) {
checkIndex(index);
protected void _setByte(int index, int value) {
memory.put(idx(index), (byte) value);
return this;
}
@Override
public ByteBuf setShort(int index, int value) {
checkIndex(index, 2);
protected void _setShort(int index, int value) {
memory.putShort(idx(index), (short) value);
return this;
}
@Override
public ByteBuf setMedium(int index, int value) {
checkIndex(index, 3);
protected void _setMedium(int index, int value) {
index = idx(index);
memory.put(index, (byte) (value >>> 16));
memory.put(index + 1, (byte) (value >>> 8));
memory.put(index + 2, (byte) value);
return this;
}
@Override
public ByteBuf setInt(int index, int value) {
checkIndex(index, 4);
protected void _setInt(int index, int value) {
memory.putInt(idx(index), value);
return this;
}
@Override
public ByteBuf setLong(int index, long value) {
checkIndex(index, 8);
protected void _setLong(int index, long value) {
memory.putLong(idx(index), value);
return this;
}
@Override

View File

@ -34,21 +34,18 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
}
@Override
public byte getByte(int index) {
checkIndex(index);
protected byte _getByte(int index) {
return memory[idx(index)];
}
@Override
public short getShort(int index) {
checkIndex(index, 2);
protected short _getShort(int index) {
index = idx(index);
return (short) (memory[index] << 8 | memory[index + 1] & 0xFF);
}
@Override
public int getUnsignedMedium(int index) {
checkIndex(index, 3);
protected int _getUnsignedMedium(int index) {
index = idx(index);
return (memory[index] & 0xff) << 16 |
(memory[index + 1] & 0xff) << 8 |
@ -56,8 +53,7 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
}
@Override
public int getInt(int index) {
checkIndex(index, 4);
protected int _getInt(int index) {
index = idx(index);
return (memory[index] & 0xff) << 24 |
(memory[index + 1] & 0xff) << 16 |
@ -66,8 +62,7 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
}
@Override
public long getLong(int index) {
checkIndex(index, 8);
protected long _getLong(int index) {
index = idx(index);
return ((long) memory[index] & 0xff) << 56 |
((long) memory[index + 1] & 0xff) << 48 |
@ -81,10 +76,10 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
@Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
checkIndex(index, length);
if (dst.hasArray()) {
getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
} else {
checkIndex(index, length);
dst.setBytes(dstIndex, memory, idx(index), length);
}
return this;
@ -119,45 +114,36 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
}
@Override
public ByteBuf setByte(int index, int value) {
checkIndex(index);
protected void _setByte(int index, int value) {
memory[idx(index)] = (byte) value;
return this;
}
@Override
public ByteBuf setShort(int index, int value) {
checkIndex(index, 2);
protected void _setShort(int index, int value) {
index = idx(index);
memory[index] = (byte) (value >>> 8);
memory[index + 1] = (byte) value;
return this;
}
@Override
public ByteBuf setMedium(int index, int value) {
checkIndex(index, 3);
protected void _setMedium(int index, int value) {
index = idx(index);
memory[index] = (byte) (value >>> 16);
memory[index + 1] = (byte) (value >>> 8);
memory[index + 2] = (byte) value;
return this;
}
@Override
public ByteBuf setInt(int index, int value) {
checkIndex(index, 4);
protected void _setInt(int index, int value) {
index = idx(index);
memory[index] = (byte) (value >>> 24);
memory[index + 1] = (byte) (value >>> 16);
memory[index + 2] = (byte) (value >>> 8);
memory[index + 3] = (byte) value;
return this;
}
@Override
public ByteBuf setLong(int index, long value) {
checkIndex(index, 8);
protected void _setLong(int index, long value) {
index = idx(index);
memory[index] = (byte) (value >>> 56);
memory[index + 1] = (byte) (value >>> 48);
@ -167,7 +153,6 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
memory[index + 5] = (byte) (value >>> 16);
memory[index + 6] = (byte) (value >>> 8);
memory[index + 7] = (byte) value;
return this;
}
@Override

View File

@ -63,21 +63,18 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
}
@Override
public byte getByte(int index) {
checkIndex(index);
protected byte _getByte(int index) {
return PlatformDependent.getByte(addr(index));
}
@Override
public short getShort(int index) {
checkIndex(index, 2);
protected short _getShort(int index) {
short v = PlatformDependent.getShort(addr(index));
return NATIVE_ORDER? v : Short.reverseBytes(v);
}
@Override
public int getUnsignedMedium(int index) {
checkIndex(index, 3);
protected int _getUnsignedMedium(int index) {
long addr = addr(index);
return (PlatformDependent.getByte(addr) & 0xff) << 16 |
(PlatformDependent.getByte(addr + 1) & 0xff) << 8 |
@ -85,15 +82,13 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
}
@Override
public int getInt(int index) {
checkIndex(index, 4);
protected int _getInt(int index) {
int v = PlatformDependent.getInt(addr(index));
return NATIVE_ORDER? v : Integer.reverseBytes(v);
}
@Override
public long getLong(int index) {
checkIndex(index, 8);
protected long _getLong(int index) {
long v = PlatformDependent.getLong(addr(index));
return NATIVE_ORDER? v : Long.reverseBytes(v);
}
@ -159,41 +154,38 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
}
@Override
public ByteBuf setByte(int index, int value) {
checkIndex(index);
protected void _setByte(int index, int value) {
PlatformDependent.putByte(addr(index), (byte) value);
return this;
}
@Override
public ByteBuf setShort(int index, int value) {
checkIndex(index, 2);
PlatformDependent.putShort(addr(index), NATIVE_ORDER? (short) value : Short.reverseBytes((short) value));
return this;
protected void _setShort(int index, int value) {
PlatformDependent.putShort(addr(index), NATIVE_ORDER ? (short) value : Short.reverseBytes((short) value));
}
@Override
public ByteBuf setMedium(int index, int value) {
checkIndex(index, 3);
_setMedium(index, value);
return this;
}
@Override
protected void _setMedium(int index, int value) {
long addr = addr(index);
PlatformDependent.putByte(addr, (byte) (value >>> 16));
PlatformDependent.putByte(addr + 1, (byte) (value >>> 8));
PlatformDependent.putByte(addr + 2, (byte) value);
return this;
}
@Override
public ByteBuf setInt(int index, int value) {
checkIndex(index, 4);
PlatformDependent.putInt(addr(index), NATIVE_ORDER? value : Integer.reverseBytes(value));
return this;
protected void _setInt(int index, int value) {
PlatformDependent.putInt(addr(index), NATIVE_ORDER ? value : Integer.reverseBytes(value));
}
@Override
public ByteBuf setLong(int index, long value) {
checkIndex(index, 8);
PlatformDependent.putLong(addr(index), NATIVE_ORDER? value : Long.reverseBytes(value));
return this;
protected void _setLong(int index, long value) {
PlatformDependent.putLong(addr(index), NATIVE_ORDER ? value : Long.reverseBytes(value));
}
@Override

View File

@ -84,11 +84,6 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf {
throw new ReadOnlyBufferException();
}
@Override
public ByteBuf setByte(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
throw new ReadOnlyBufferException();
@ -104,26 +99,56 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf {
throw new ReadOnlyBufferException();
}
@Override
public ByteBuf setByte(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override
protected void _setByte(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override
public ByteBuf setShort(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override
protected void _setShort(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override
public ByteBuf setMedium(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override
protected void _setMedium(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override
public ByteBuf setInt(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override
protected void _setInt(int index, int value) {
throw new ReadOnlyBufferException();
}
@Override
public ByteBuf setLong(int index, long value) {
throw new ReadOnlyBufferException();
}
@Override
protected void _setLong(int index, long value) {
throw new ReadOnlyBufferException();
}
@Override
public int setBytes(int index, InputStream in, int length) {
throw new ReadOnlyBufferException();
@ -182,26 +207,51 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf {
@Override
public byte getByte(int index) {
return _getByte(index);
}
@Override
protected byte _getByte(int index) {
return buffer.getByte(index);
}
@Override
public short getShort(int index) {
return _getShort(index);
}
@Override
protected short _getShort(int index) {
return buffer.getShort(index);
}
@Override
public int getUnsignedMedium(int index) {
return _getUnsignedMedium(index);
}
@Override
protected int _getUnsignedMedium(int index) {
return buffer.getUnsignedMedium(index);
}
@Override
public int getInt(int index) {
return _getInt(index);
}
@Override
protected int _getInt(int index) {
return buffer.getInt(index);
}
@Override
public long getLong(int index) {
return _getLong(index);
}
@Override
protected long _getLong(int index) {
return buffer.getLong(index);
}

View File

@ -103,32 +103,27 @@ public class SlicedByteBuf extends AbstractDerivedByteBuf {
}
@Override
public byte getByte(int index) {
checkIndex(index);
protected byte _getByte(int index) {
return buffer.getByte(index + adjustment);
}
@Override
public short getShort(int index) {
checkIndex(index, 2);
protected short _getShort(int index) {
return buffer.getShort(index + adjustment);
}
@Override
public int getUnsignedMedium(int index) {
checkIndex(index, 3);
protected int _getUnsignedMedium(int index) {
return buffer.getUnsignedMedium(index + adjustment);
}
@Override
public int getInt(int index) {
checkIndex(index, 4);
protected int _getInt(int index) {
return buffer.getInt(index + adjustment);
}
@Override
public long getLong(int index) {
checkIndex(index, 8);
protected long _getLong(int index) {
return buffer.getLong(index + adjustment);
}
@ -176,38 +171,28 @@ public class SlicedByteBuf extends AbstractDerivedByteBuf {
}
@Override
public ByteBuf setByte(int index, int value) {
checkIndex(index);
protected void _setByte(int index, int value) {
buffer.setByte(index + adjustment, value);
return this;
}
@Override
public ByteBuf setShort(int index, int value) {
checkIndex(index, 2);
protected void _setShort(int index, int value) {
buffer.setShort(index + adjustment, value);
return this;
}
@Override
public ByteBuf setMedium(int index, int value) {
checkIndex(index, 3);
protected void _setMedium(int index, int value) {
buffer.setMedium(index + adjustment, value);
return this;
}
@Override
public ByteBuf setInt(int index, int value) {
checkIndex(index, 4);
protected void _setInt(int index, int value) {
buffer.setInt(index + adjustment, value);
return this;
}
@Override
public ByteBuf setLong(int index, long value) {
checkIndex(index, 8);
protected void _setLong(int index, long value) {
buffer.setLong(index + adjustment, value);
return this;
}
@Override

View File

@ -198,30 +198,55 @@ final class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
@Override
public byte getByte(int index) {
ensureAccessible();
return _getByte(index);
}
@Override
protected byte _getByte(int index) {
return buffer.get(index);
}
@Override
public short getShort(int index) {
ensureAccessible();
return _getShort(index);
}
@Override
protected short _getShort(int index) {
return buffer.getShort(index);
}
@Override
public int getUnsignedMedium(int index) {
ensureAccessible();
return _getUnsignedMedium(index);
}
@Override
protected int _getUnsignedMedium(int index) {
return (getByte(index) & 0xff) << 16 | (getByte(index + 1) & 0xff) << 8 | getByte(index + 2) & 0xff;
}
@Override
public int getInt(int index) {
ensureAccessible();
return _getInt(index);
}
@Override
protected int _getInt(int index) {
return buffer.getInt(index);
}
@Override
public long getLong(int index) {
ensureAccessible();
return _getLong(index);
}
@Override
protected long _getLong(int index) {
return buffer.getLong(index);
}
@ -275,40 +300,65 @@ final class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
@Override
public ByteBuf setByte(int index, int value) {
ensureAccessible();
buffer.put(index, (byte) value);
_setByte(index, value);
return this;
}
@Override
protected void _setByte(int index, int value) {
buffer.put(index, (byte) value);
}
@Override
public ByteBuf setShort(int index, int value) {
ensureAccessible();
buffer.putShort(index, (short) value);
_setShort(index, value);
return this;
}
@Override
protected void _setShort(int index, int value) {
buffer.putShort(index, (short) value);
}
@Override
public ByteBuf setMedium(int index, int value) {
ensureAccessible();
_setMedium(index, value);
return this;
}
@Override
protected void _setMedium(int index, int value) {
setByte(index, (byte) (value >>> 16));
setByte(index + 1, (byte) (value >>> 8));
setByte(index + 2, (byte) value);
return this;
}
@Override
public ByteBuf setInt(int index, int value) {
ensureAccessible();
buffer.putInt(index, value);
_setInt(index, value);
return this;
}
@Override
protected void _setInt(int index, int value) {
buffer.putInt(index, value);
}
@Override
public ByteBuf setLong(int index, long value) {
ensureAccessible();
buffer.putLong(index, value);
_setLong(index, value);
return this;
}
@Override
protected void _setLong(int index, long value) {
buffer.putLong(index, value);
}
@Override
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
ensureAccessible();

View File

@ -145,12 +145,6 @@ final class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
return 0;
}
@Override
public byte getByte(int index) {
ensureAccessible();
return array[index];
}
@Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
ensureAccessible();
@ -189,13 +183,6 @@ final class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
return out.write((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length));
}
@Override
public ByteBuf setByte(int index, int value) {
ensureAccessible();
array[index] = (byte) value;
return this;
}
@Override
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
ensureAccessible();
@ -253,15 +240,36 @@ final class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
return new ByteBuffer[] { nioBuffer(index, length) };
}
@Override
public byte getByte(int index) {
ensureAccessible();
return _getByte(index);
}
@Override
protected byte _getByte(int index) {
return array[index];
}
@Override
public short getShort(int index) {
ensureAccessible();
return _getShort(index);
}
@Override
protected short _getShort(int index) {
return (short) (array[index] << 8 | array[index + 1] & 0xFF);
}
@Override
public int getUnsignedMedium(int index) {
ensureAccessible();
return _getUnsignedMedium(index);
}
@Override
protected int _getUnsignedMedium(int index) {
return (array[index] & 0xff) << 16 |
(array[index + 1] & 0xff) << 8 |
array[index + 2] & 0xff;
@ -270,6 +278,11 @@ final class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
@Override
public int getInt(int index) {
ensureAccessible();
return _getInt(index);
}
@Override
protected int _getInt(int index) {
return (array[index] & 0xff) << 24 |
(array[index + 1] & 0xff) << 16 |
(array[index + 2] & 0xff) << 8 |
@ -279,6 +292,11 @@ final class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
@Override
public long getLong(int index) {
ensureAccessible();
return _getLong(index);
}
@Override
protected long _getLong(int index) {
return ((long) array[index] & 0xff) << 56 |
((long) array[index + 1] & 0xff) << 48 |
((long) array[index + 2] & 0xff) << 40 |
@ -289,36 +307,69 @@ final class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
(long) array[index + 7] & 0xff;
}
@Override
public ByteBuf setByte(int index, int value) {
ensureAccessible();
_setByte(index, value);
return this;
}
@Override
protected void _setByte(int index, int value) {
array[index] = (byte) value;
}
@Override
public ByteBuf setShort(int index, int value) {
ensureAccessible();
_setShort(index, value);
return this;
}
@Override
protected void _setShort(int index, int value) {
array[index] = (byte) (value >>> 8);
array[index + 1] = (byte) value;
return this;
}
@Override
public ByteBuf setMedium(int index, int value) {
ensureAccessible();
_setMedium(index, value);
return this;
}
@Override
protected void _setMedium(int index, int value) {
array[index] = (byte) (value >>> 16);
array[index + 1] = (byte) (value >>> 8);
array[index + 2] = (byte) value;
return this;
}
@Override
public ByteBuf setInt(int index, int value) {
ensureAccessible();
_setInt(index, value);
return this;
}
@Override
protected void _setInt(int index, int value) {
array[index] = (byte) (value >>> 24);
array[index + 1] = (byte) (value >>> 16);
array[index + 2] = (byte) (value >>> 8);
array[index + 3] = (byte) value;
return this;
}
@Override
public ByteBuf setLong(int index, long value) {
ensureAccessible();
_setLong(index, value);
return this;
}
@Override
protected void _setLong(int index, long value) {
array[index] = (byte) (value >>> 56);
array[index + 1] = (byte) (value >>> 48);
array[index + 2] = (byte) (value >>> 40);
@ -327,7 +378,6 @@ final class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
array[index + 5] = (byte) (value >>> 16);
array[index + 6] = (byte) (value >>> 8);
array[index + 7] = (byte) value;
return this;
}
@Override

View File

@ -79,6 +79,7 @@ public class LocalTransportThreadModelTest {
}
@Test(timeout = 30000)
@Ignore
public void testStagedExecutionMultiple() throws Throwable {
for (int i = 0; i < 10; i ++) {
testStagedExecution();