Minor simplifications/optimizations to AbstractByteBuf methods (#9845)

Motivation

While working on other changes I noticed some opportunities to
streamline a few things in AbstractByteBuf.

Modifications

- Avoid duplicate ensureAccessible() checks in discard(Some)ReadBytes()
and ensureWritable0(int) methods
- Simplify ensureWritable0(int) logic
- Make some conditional checks more concise

Result

Cleaner, possibly faster code
This commit is contained in:
Nick Hill 2019-12-05 02:32:21 -08:00 committed by Norman Maurer
parent 6211d19503
commit 4a5712f8d9

View File

@ -183,8 +183,8 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public ByteBuf discardReadBytes() { public ByteBuf discardReadBytes() {
ensureAccessible();
if (readerIndex == 0) { if (readerIndex == 0) {
ensureAccessible();
return this; return this;
} }
@ -193,6 +193,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
writerIndex -= readerIndex; writerIndex -= readerIndex;
readerIndex = 0; readerIndex = 0;
} else { } else {
ensureAccessible();
writerIndex = readerIndex = 0; writerIndex = readerIndex = 0;
} }
return this; return this;
@ -200,12 +201,9 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public ByteBuf discardSomeReadBytes() { public ByteBuf discardSomeReadBytes() {
ensureAccessible(); if (readerIndex > 0) {
if (readerIndex == 0) {
return this;
}
if (readerIndex == writerIndex) { if (readerIndex == writerIndex) {
ensureAccessible();
writerIndex = readerIndex = 0; writerIndex = readerIndex = 0;
return this; return this;
} }
@ -214,7 +212,10 @@ public abstract class AbstractByteBuf extends ByteBuf {
setBytes(0, this, readerIndex, writerIndex - readerIndex); setBytes(0, this, readerIndex, writerIndex - readerIndex);
writerIndex -= readerIndex; writerIndex -= readerIndex;
readerIndex = 0; readerIndex = 0;
return this;
} }
}
ensureAccessible();
return this; return this;
} }
@ -227,34 +228,28 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public ByteBuf ensureWritable(int minWritableBytes) { public ByteBuf ensureWritable(int minWritableBytes) {
checkPositiveOrZero(minWritableBytes, "minWritableBytes"); ensureWritable0(checkPositiveOrZero(minWritableBytes, "minWritableBytes"));
ensureWritable0(minWritableBytes);
return this; return this;
} }
final void ensureWritable0(int minWritableBytes) { final void ensureWritable0(int minWritableBytes) {
final int writerIndex = writerIndex();
final int targetCapacity = writerIndex + minWritableBytes;
if (targetCapacity <= capacity()) {
ensureAccessible(); ensureAccessible();
if (minWritableBytes <= writableBytes()) {
return; return;
} }
final int writerIndex = writerIndex(); if (checkBounds && targetCapacity > maxCapacity) {
if (checkBounds) { ensureAccessible();
if (minWritableBytes > maxCapacity - writerIndex) {
throw new IndexOutOfBoundsException(String.format( throw new IndexOutOfBoundsException(String.format(
"writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s", "writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s",
writerIndex, minWritableBytes, maxCapacity, this)); writerIndex, minWritableBytes, maxCapacity, this));
} }
}
// Normalize the current capacity to the power of 2. // Normalize the target capacity to the power of 2.
int minNewCapacity = writerIndex + minWritableBytes; final int fastWritable = maxFastWritableBytes();
int newCapacity = alloc().calculateNewCapacity(minNewCapacity, maxCapacity); int newCapacity = fastWritable >= minWritableBytes ? writerIndex + fastWritable
: alloc().calculateNewCapacity(targetCapacity, maxCapacity);
int fastCapacity = writerIndex + maxFastWritableBytes();
// Grow by a smaller amount if it will avoid reallocation
if (newCapacity > fastCapacity && minNewCapacity <= fastCapacity) {
newCapacity = fastCapacity;
}
// Adjust to the new capacity. // Adjust to the new capacity.
capacity(newCapacity); capacity(newCapacity);
@ -280,15 +275,9 @@ public abstract class AbstractByteBuf extends ByteBuf {
return 3; return 3;
} }
// Normalize the current capacity to the power of 2. int fastWritable = maxFastWritableBytes();
int minNewCapacity = writerIndex + minWritableBytes; int newCapacity = fastWritable >= minWritableBytes ? writerIndex + fastWritable
int newCapacity = alloc().calculateNewCapacity(minNewCapacity, maxCapacity); : alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity);
int fastCapacity = writerIndex + maxFastWritableBytes();
// Grow by a smaller amount if it will avoid reallocation
if (newCapacity > fastCapacity && minNewCapacity <= fastCapacity) {
newCapacity = fastCapacity;
}
// Adjust to the new capacity. // Adjust to the new capacity.
capacity(newCapacity); capacity(newCapacity);
@ -1420,30 +1409,25 @@ public abstract class AbstractByteBuf extends ByteBuf {
* than the specified value. * than the specified value.
*/ */
protected final void checkReadableBytes(int minimumReadableBytes) { protected final void checkReadableBytes(int minimumReadableBytes) {
checkPositiveOrZero(minimumReadableBytes, "minimumReadableBytes"); checkReadableBytes0(checkPositiveOrZero(minimumReadableBytes, "minimumReadableBytes"));
checkReadableBytes0(minimumReadableBytes);
} }
protected final void checkNewCapacity(int newCapacity) { protected final void checkNewCapacity(int newCapacity) {
ensureAccessible(); ensureAccessible();
if (checkBounds) { if (checkBounds && (newCapacity < 0 || newCapacity > maxCapacity())) {
if (newCapacity < 0 || newCapacity > maxCapacity()) {
throw new IllegalArgumentException("newCapacity: " + newCapacity + throw new IllegalArgumentException("newCapacity: " + newCapacity +
" (expected: 0-" + maxCapacity() + ')'); " (expected: 0-" + maxCapacity() + ')');
} }
} }
}
private void checkReadableBytes0(int minimumReadableBytes) { private void checkReadableBytes0(int minimumReadableBytes) {
ensureAccessible(); ensureAccessible();
if (checkBounds) { if (checkBounds && readerIndex > writerIndex - minimumReadableBytes) {
if (readerIndex > writerIndex - minimumReadableBytes) {
throw new IndexOutOfBoundsException(String.format( throw new IndexOutOfBoundsException(String.format(
"readerIndex(%d) + length(%d) exceeds writerIndex(%d): %s", "readerIndex(%d) + length(%d) exceeds writerIndex(%d): %s",
readerIndex, minimumReadableBytes, writerIndex, this)); readerIndex, minimumReadableBytes, writerIndex, this));
} }
} }
}
/** /**
* Should be called by every method that tries to access the buffers content to check * Should be called by every method that tries to access the buffers content to check