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:
parent
a322addb17
commit
63d33b390a
@ -213,8 +213,8 @@ public abstract class AbstractByteBuf extends ByteBuf {
|
||||
|
||||
@Override
|
||||
public ByteBuf discardReadBytes() {
|
||||
ensureAccessible();
|
||||
if (readerIndex == 0) {
|
||||
ensureAccessible();
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -224,6 +224,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
|
||||
adjustMarkers(readerIndex);
|
||||
readerIndex = 0;
|
||||
} else {
|
||||
ensureAccessible();
|
||||
adjustMarkers(readerIndex);
|
||||
writerIndex = readerIndex = 0;
|
||||
}
|
||||
@ -232,23 +233,23 @@ public abstract class AbstractByteBuf extends ByteBuf {
|
||||
|
||||
@Override
|
||||
public ByteBuf discardSomeReadBytes() {
|
||||
if (readerIndex > 0) {
|
||||
if (readerIndex == writerIndex) {
|
||||
ensureAccessible();
|
||||
adjustMarkers(readerIndex);
|
||||
writerIndex = readerIndex = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
if (readerIndex >= capacity() >>> 1) {
|
||||
setBytes(0, this, readerIndex, writerIndex - readerIndex);
|
||||
writerIndex -= readerIndex;
|
||||
adjustMarkers(readerIndex);
|
||||
readerIndex = 0;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
ensureAccessible();
|
||||
if (readerIndex == 0) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (readerIndex == writerIndex) {
|
||||
adjustMarkers(readerIndex);
|
||||
writerIndex = readerIndex = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
if (readerIndex >= capacity() >>> 1) {
|
||||
setBytes(0, this, readerIndex, writerIndex - readerIndex);
|
||||
writerIndex -= readerIndex;
|
||||
adjustMarkers(readerIndex);
|
||||
readerIndex = 0;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -277,34 +278,28 @@ public abstract class AbstractByteBuf extends ByteBuf {
|
||||
|
||||
@Override
|
||||
public ByteBuf ensureWritable(int minWritableBytes) {
|
||||
checkPositiveOrZero(minWritableBytes, "minWritableBytes");
|
||||
ensureWritable0(minWritableBytes);
|
||||
ensureWritable0(checkPositiveOrZero(minWritableBytes, "minWritableBytes"));
|
||||
return this;
|
||||
}
|
||||
|
||||
final void ensureWritable0(int minWritableBytes) {
|
||||
ensureAccessible();
|
||||
if (minWritableBytes <= writableBytes()) {
|
||||
final int writerIndex = writerIndex();
|
||||
final int targetCapacity = writerIndex + minWritableBytes;
|
||||
if (targetCapacity <= capacity()) {
|
||||
ensureAccessible();
|
||||
return;
|
||||
}
|
||||
final int writerIndex = writerIndex();
|
||||
if (checkBounds) {
|
||||
if (minWritableBytes > maxCapacity - writerIndex) {
|
||||
throw new IndexOutOfBoundsException(String.format(
|
||||
"writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s",
|
||||
writerIndex, minWritableBytes, maxCapacity, this));
|
||||
}
|
||||
if (checkBounds && targetCapacity > maxCapacity) {
|
||||
ensureAccessible();
|
||||
throw new IndexOutOfBoundsException(String.format(
|
||||
"writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s",
|
||||
writerIndex, minWritableBytes, maxCapacity, this));
|
||||
}
|
||||
|
||||
// Normalize the current capacity to the power of 2.
|
||||
int minNewCapacity = writerIndex + minWritableBytes;
|
||||
int newCapacity = alloc().calculateNewCapacity(minNewCapacity, maxCapacity);
|
||||
|
||||
int fastCapacity = writerIndex + maxFastWritableBytes();
|
||||
// Grow by a smaller amount if it will avoid reallocation
|
||||
if (newCapacity > fastCapacity && minNewCapacity <= fastCapacity) {
|
||||
newCapacity = fastCapacity;
|
||||
}
|
||||
// Normalize the target capacity to the power of 2.
|
||||
final int fastWritable = maxFastWritableBytes();
|
||||
int newCapacity = fastWritable >= minWritableBytes ? writerIndex + fastWritable
|
||||
: alloc().calculateNewCapacity(targetCapacity, maxCapacity);
|
||||
|
||||
// Adjust to the new capacity.
|
||||
capacity(newCapacity);
|
||||
@ -330,15 +325,9 @@ public abstract class AbstractByteBuf extends ByteBuf {
|
||||
return 3;
|
||||
}
|
||||
|
||||
// Normalize the current capacity to the power of 2.
|
||||
int minNewCapacity = writerIndex + minWritableBytes;
|
||||
int newCapacity = alloc().calculateNewCapacity(minNewCapacity, maxCapacity);
|
||||
|
||||
int fastCapacity = writerIndex + maxFastWritableBytes();
|
||||
// Grow by a smaller amount if it will avoid reallocation
|
||||
if (newCapacity > fastCapacity && minNewCapacity <= fastCapacity) {
|
||||
newCapacity = fastCapacity;
|
||||
}
|
||||
int fastWritable = maxFastWritableBytes();
|
||||
int newCapacity = fastWritable >= minWritableBytes ? writerIndex + fastWritable
|
||||
: alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity);
|
||||
|
||||
// Adjust to the new capacity.
|
||||
capacity(newCapacity);
|
||||
@ -1474,28 +1463,23 @@ public abstract class AbstractByteBuf extends ByteBuf {
|
||||
* than the specified value.
|
||||
*/
|
||||
protected final void checkReadableBytes(int minimumReadableBytes) {
|
||||
checkPositiveOrZero(minimumReadableBytes, "minimumReadableBytes");
|
||||
checkReadableBytes0(minimumReadableBytes);
|
||||
checkReadableBytes0(checkPositiveOrZero(minimumReadableBytes, "minimumReadableBytes"));
|
||||
}
|
||||
|
||||
protected final void checkNewCapacity(int newCapacity) {
|
||||
ensureAccessible();
|
||||
if (checkBounds) {
|
||||
if (newCapacity < 0 || newCapacity > maxCapacity()) {
|
||||
throw new IllegalArgumentException("newCapacity: " + newCapacity +
|
||||
" (expected: 0-" + maxCapacity() + ')');
|
||||
}
|
||||
if (checkBounds && (newCapacity < 0 || newCapacity > maxCapacity())) {
|
||||
throw new IllegalArgumentException("newCapacity: " + newCapacity +
|
||||
" (expected: 0-" + maxCapacity() + ')');
|
||||
}
|
||||
}
|
||||
|
||||
private void checkReadableBytes0(int minimumReadableBytes) {
|
||||
ensureAccessible();
|
||||
if (checkBounds) {
|
||||
if (readerIndex > writerIndex - minimumReadableBytes) {
|
||||
throw new IndexOutOfBoundsException(String.format(
|
||||
"readerIndex(%d) + length(%d) exceeds writerIndex(%d): %s",
|
||||
readerIndex, minimumReadableBytes, writerIndex, this));
|
||||
}
|
||||
if (checkBounds && readerIndex > writerIndex - minimumReadableBytes) {
|
||||
throw new IndexOutOfBoundsException(String.format(
|
||||
"readerIndex(%d) + length(%d) exceeds writerIndex(%d): %s",
|
||||
readerIndex, minimumReadableBytes, writerIndex, this));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user