GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix component search fast path by yawkat · Pull Request #16548 · netty/netty

Fix component search fast path #16548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
chrisvest merged 4 commits into 4.2 from wrong-component
Apr 4, 2026
+202 −21
Merged

Fix component search fast path #16548

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
2b7aff1
Reapply "Eliminate redundant bounds checks in CompositeByteBuf access…
chrisvest Mar 25, 2026
2bbf85e
Fix component search fast path
yawkat Mar 25, 2026
e1fcbbc
CR
yawkat Mar 25, 2026
838caca
Merge branch '4.2' into wrong-component
normanmaurer Mar 31, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

126 changes: 106 additions & 20 deletions 126 buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ public CompositeByteBuf removeComponent(int cIndex) {
Component comp = components[cIndex];
if (lastAccessed == comp) {
lastAccessed = null;
lastAccessedIndex = 0;
}
comp.free();
removeComp(cIndex);
Expand Down Expand Up @@ -646,6 +647,7 @@ public CompositeByteBuf removeComponents(int cIndex, int numComponents) {
}
if (lastAccessed == c) {
lastAccessed = null;
lastAccessedIndex = 0;
}
c.free();
}
Expand Down Expand Up @@ -856,6 +858,7 @@ public CompositeByteBuf capacity(int newCapacity) {
}
} else if (newCapacity < oldCapacity) {
lastAccessed = null;
lastAccessedIndex = 0;
int i = size - 1;
for (int bytesToTrim = oldCapacity - newCapacity; i >= 0; i--) {
Component c = components[i];
Expand Down Expand Up @@ -952,20 +955,36 @@ public int toByteIndex(int cIndex) {
@Override
public byte getByte(int index) {
Component c = findComponent(index);
return c.buf.getByte(c.idx(index));
return c.abuf != null ? c.abuf._getByte(c.idx(index)) : c.buf.getByte(c.idx(index));
}

@Override
public byte readByte() {
checkReadableBytes(1);
int rIdx = readerIndex;
Component c = lastAccessed;
if (c == null) {
c = findIt(rIdx);
} else if (rIdx >= c.endOffset) {
c = findComponentForRead(rIdx);
} else if (rIdx < c.offset) {
c = findIt(rIdx);
}
readerIndex = rIdx + 1;
return c.abuf != null ? c.abuf._getByte(rIdx + c.adjustment) : c.buf.getByte(rIdx + c.adjustment);
}

@Override
protected byte _getByte(int index) {
Component c = findComponent0(index);
return c.buf.getByte(c.idx(index));
return c.abuf != null ? c.abuf._getByte(c.idx(index)) : c.buf.getByte(c.idx(index));
}

@Override
protected short _getShort(int index) {
Component c = findComponent0(index);
if (index + 2 <= c.endOffset) {
return c.buf.getShort(c.idx(index));
return c.abuf != null ? c.abuf._getShort(c.idx(index)) : c.buf.getShort(c.idx(index));
} else if (order() == ByteOrder.BIG_ENDIAN) {
return (short) ((_getByte(index) & 0xff) << 8 | _getByte(index + 1) & 0xff);
} else {
Expand All @@ -977,7 +996,7 @@ protected short _getShort(int index) {
protected short _getShortLE(int index) {
Component c = findComponent0(index);
if (index + 2 <= c.endOffset) {
return c.buf.getShortLE(c.idx(index));
return c.abuf != null ? c.abuf._getShortLE(c.idx(index)) : c.buf.getShortLE(c.idx(index));
} else if (order() == ByteOrder.BIG_ENDIAN) {
return (short) (_getByte(index) & 0xff | (_getByte(index + 1) & 0xff) << 8);
} else {
Expand All @@ -989,7 +1008,8 @@ protected short _getShortLE(int index) {
protected int _getUnsignedMedium(int index) {
Component c = findComponent0(index);
if (index + 3 <= c.endOffset) {
return c.buf.getUnsignedMedium(c.idx(index));
return c.abuf != null ? c.abuf._getUnsignedMedium(c.idx(index))
: c.buf.getUnsignedMedium(c.idx(index));
} else if (order() == ByteOrder.BIG_ENDIAN) {
return (_getShort(index) & 0xffff) << 8 | _getByte(index + 2) & 0xff;
} else {
Expand All @@ -1001,7 +1021,8 @@ protected int _getUnsignedMedium(int index) {
protected int _getUnsignedMediumLE(int index) {
Component c = findComponent0(index);
if (index + 3 <= c.endOffset) {
return c.buf.getUnsignedMediumLE(c.idx(index));
return c.abuf != null ? c.abuf._getUnsignedMediumLE(c.idx(index))
: c.buf.getUnsignedMediumLE(c.idx(index));
} else if (order() == ByteOrder.BIG_ENDIAN) {
return _getShortLE(index) & 0xffff | (_getByte(index + 2) & 0xff) << 16;
} else {
Expand All @@ -1013,7 +1034,7 @@ protected int _getUnsignedMediumLE(int index) {
protected int _getInt(int index) {
Component c = findComponent0(index);
if (index + 4 <= c.endOffset) {
return c.buf.getInt(c.idx(index));
return c.abuf != null ? c.abuf._getInt(c.idx(index)) : c.buf.getInt(c.idx(index));
} else if (order() == ByteOrder.BIG_ENDIAN) {
return (_getShort(index) & 0xffff) << 16 | _getShort(index + 2) & 0xffff;
} else {
Expand All @@ -1025,7 +1046,7 @@ protected int _getInt(int index) {
protected int _getIntLE(int index) {
Component c = findComponent0(index);
if (index + 4 <= c.endOffset) {
return c.buf.getIntLE(c.idx(index));
return c.abuf != null ? c.abuf._getIntLE(c.idx(index)) : c.buf.getIntLE(c.idx(index));
} else if (order() == ByteOrder.BIG_ENDIAN) {
return _getShortLE(index) & 0xffff | (_getShortLE(index + 2) & 0xffff) << 16;
} else {
Expand All @@ -1037,7 +1058,7 @@ protected int _getIntLE(int index) {
protected long _getLong(int index) {
Component c = findComponent0(index);
if (index + 8 <= c.endOffset) {
return c.buf.getLong(c.idx(index));
return c.abuf != null ? c.abuf._getLong(c.idx(index)) : c.buf.getLong(c.idx(index));
} else if (order() == ByteOrder.BIG_ENDIAN) {
return (_getInt(index) & 0xffffffffL) << 32 | _getInt(index + 4) & 0xffffffffL;
} else {
Expand All @@ -1049,7 +1070,7 @@ protected long _getLong(int index) {
protected long _getLongLE(int index) {
Component c = findComponent0(index);
if (index + 8 <= c.endOffset) {
return c.buf.getLongLE(c.idx(index));
return c.abuf != null ? c.abuf._getLongLE(c.idx(index)) : c.buf.getLongLE(c.idx(index));
} else if (order() == ByteOrder.BIG_ENDIAN) {
return _getIntLE(index) & 0xffffffffL | (_getIntLE(index + 4) & 0xffffffffL) << 32;
} else {
Expand Down Expand Up @@ -1180,14 +1201,22 @@ public CompositeByteBuf getBytes(int index, OutputStream out, int length) throws
@Override
public CompositeByteBuf setByte(int index, int value) {
Component c = findComponent(index);
c.buf.setByte(c.idx(index), value);
if (c.abuf != null) {
c.abuf._setByte(c.idx(index), value);
} else {
c.buf.setByte(c.idx(index), value);
}
return this;
}

@Override
protected void _setByte(int index, int value) {
Component c = findComponent0(index);
c.buf.setByte(c.idx(index), value);
if (c.abuf != null) {
c.abuf._setByte(c.idx(index), value);
} else {
c.buf.setByte(c.idx(index), value);
}
}

@Override
Expand All @@ -1201,7 +1230,11 @@ public CompositeByteBuf setShort(int index, int value) {
protected void _setShort(int index, int value) {
Component c = findComponent0(index);
if (index + 2 <= c.endOffset) {
c.buf.setShort(c.idx(index), value);
if (c.abuf != null) {
c.abuf._setShort(c.idx(index), value);
} else {
c.buf.setShort(c.idx(index), value);
}
} else if (order() == ByteOrder.BIG_ENDIAN) {
_setByte(index, (byte) (value >>> 8));
_setByte(index + 1, (byte) value);
Expand All @@ -1215,7 +1248,11 @@ protected void _setShort(int index, int value) {
protected void _setShortLE(int index, int value) {
Component c = findComponent0(index);
if (index + 2 <= c.endOffset) {
c.buf.setShortLE(c.idx(index), value);
if (c.abuf != null) {
c.abuf._setShortLE(c.idx(index), value);
} else {
c.buf.setShortLE(c.idx(index), value);
}
} else if (order() == ByteOrder.BIG_ENDIAN) {
_setByte(index, (byte) value);
_setByte(index + 1, (byte) (value >>> 8));
Expand All @@ -1236,7 +1273,11 @@ public CompositeByteBuf setMedium(int index, int value) {
protected void _setMedium(int index, int value) {
Component c = findComponent0(index);
if (index + 3 <= c.endOffset) {
c.buf.setMedium(c.idx(index), value);
if (c.abuf != null) {
c.abuf._setMedium(c.idx(index), value);
} else {
c.buf.setMedium(c.idx(index), value);
}
} else if (order() == ByteOrder.BIG_ENDIAN) {
_setShort(index, (short) (value >> 8));
_setByte(index + 2, (byte) value);
Expand All @@ -1250,7 +1291,11 @@ protected void _setMedium(int index, int value) {
protected void _setMediumLE(int index, int value) {
Component c = findComponent0(index);
if (index + 3 <= c.endOffset) {
c.buf.setMediumLE(c.idx(index), value);
if (c.abuf != null) {
c.abuf._setMediumLE(c.idx(index), value);
} else {
c.buf.setMediumLE(c.idx(index), value);
}
} else if (order() == ByteOrder.BIG_ENDIAN) {
_setShortLE(index, (short) value);
_setByte(index + 2, (byte) (value >>> 16));
Expand All @@ -1271,7 +1316,11 @@ public CompositeByteBuf setInt(int index, int value) {
protected void _setInt(int index, int value) {
Component c = findComponent0(index);
if (index + 4 <= c.endOffset) {
c.buf.setInt(c.idx(index), value);
if (c.abuf != null) {
c.abuf._setInt(c.idx(index), value);
} else {
c.buf.setInt(c.idx(index), value);
}
} else if (order() == ByteOrder.BIG_ENDIAN) {
_setShort(index, (short) (value >>> 16));
_setShort(index + 2, (short) value);
Expand All @@ -1285,7 +1334,11 @@ protected void _setInt(int index, int value) {
protected void _setIntLE(int index, int value) {
Component c = findComponent0(index);
if (index + 4 <= c.endOffset) {
c.buf.setIntLE(c.idx(index), value);
if (c.abuf != null) {
c.abuf._setIntLE(c.idx(index), value);
} else {
c.buf.setIntLE(c.idx(index), value);
}
} else if (order() == ByteOrder.BIG_ENDIAN) {
_setShortLE(index, (short) value);
_setShortLE(index + 2, (short) (value >>> 16));
Expand All @@ -1306,7 +1359,11 @@ public CompositeByteBuf setLong(int index, long value) {
protected void _setLong(int index, long value) {
Component c = findComponent0(index);
if (index + 8 <= c.endOffset) {
c.buf.setLong(c.idx(index), value);
if (c.abuf != null) {
c.abuf._setLong(c.idx(index), value);
} else {
c.buf.setLong(c.idx(index), value);
}
} else if (order() == ByteOrder.BIG_ENDIAN) {
_setInt(index, (int) (value >>> 32));
_setInt(index + 4, (int) value);
Expand All @@ -1320,7 +1377,11 @@ protected void _setLong(int index, long value) {
protected void _setLongLE(int index, long value) {
Component c = findComponent0(index);
if (index + 8 <= c.endOffset) {
c.buf.setLongLE(c.idx(index), value);
if (c.abuf != null) {
c.abuf._setLongLE(c.idx(index), value);
} else {
c.buf.setLongLE(c.idx(index), value);
}
} else if (order() == ByteOrder.BIG_ENDIAN) {
_setIntLE(index, (int) value);
_setIntLE(index + 4, (int) (value >>> 32));
Expand Down Expand Up @@ -1613,6 +1674,7 @@ public ByteBuf internalComponentAtOffset(int offset) {

// weak cache - check it first when looking for component
private Component lastAccessed;
private int lastAccessedIndex;

private Component findComponent(int offset) {
Component la = lastAccessed;
Expand All @@ -1632,6 +1694,22 @@ private Component findComponent0(int offset) {
return findIt(offset);
}

/**
* Sequential-read fast path: try the next component(s) before falling back to binary search.
*/
private Component findComponentForRead(int offset) {
int cc = componentCount;
for (int next = lastAccessedIndex + 1; next < cc; next++) {
Component c = components[next];
if (offset < c.endOffset) {
lastAccessed = c;
lastAccessedIndex = next;
return c;
}
}
return findIt(offset);
}

private Component findIt(int offset) {
for (int low = 0, high = componentCount; low <= high;) {
int mid = low + high >>> 1;
Expand All @@ -1646,6 +1724,7 @@ private Component findIt(int offset) {
high = mid - 1;
} else {
lastAccessed = c;
lastAccessedIndex = mid;
return c;
}
}
Expand Down Expand Up @@ -1785,6 +1864,7 @@ private void consolidate0(int cIndex, int numComponents) {
components[i].transferTo(consolidated);
}
lastAccessed = null;
lastAccessedIndex = 0;
removeCompRange(cIndex + 1, endCIndex);
components[cIndex] = newComponent(consolidated, 0);
if (cIndex != 0 || numComponents != componentCount) {
Expand All @@ -1809,6 +1889,7 @@ public CompositeByteBuf discardReadComponents() {
components[i].free();
}
lastAccessed = null;
lastAccessedIndex = 0;
clearComps();
setIndex(0, 0);
adjustMarkers(readerIndex);
Expand All @@ -1831,6 +1912,7 @@ public CompositeByteBuf discardReadComponents() {
Component la = lastAccessed;
if (la != null && la.endOffset <= readerIndex) {
lastAccessed = null;
lastAccessedIndex = 0;
}
removeCompRange(0, firstComponentId);

Expand All @@ -1857,6 +1939,7 @@ public CompositeByteBuf discardReadBytes() {
components[i].free();
}
lastAccessed = null;
lastAccessedIndex = 0;
clearComps();
setIndex(0, 0);
adjustMarkers(readerIndex);
Expand Down Expand Up @@ -1888,6 +1971,7 @@ public CompositeByteBuf discardReadBytes() {
Component la = lastAccessed;
if (la != null && la.endOffset <= readerIndex) {
lastAccessed = null;
lastAccessedIndex = 0;
}

removeCompRange(0, firstComponentId);
Expand All @@ -1913,6 +1997,7 @@ public String toString() {
private static final class Component {
final ByteBuf srcBuf; // the originally added buffer
final ByteBuf buf; // srcBuf unwrapped zero or more times
final AbstractByteBuf abuf; // buf cast to AbstractByteBuf, or null if not an instance

int srcAdjustment; // index of the start of this CompositeByteBuf relative to srcBuf
int adjustment; // index of the start of this CompositeByteBuf relative to buf
Expand All @@ -1927,6 +2012,7 @@ private static final class Component {
this.srcBuf = srcBuf;
this.srcAdjustment = srcOffset - offset;
this.buf = buf;
this.abuf = buf instanceof AbstractByteBuf ? (AbstractByteBuf) buf : null;
this.adjustment = bufOffset - offset;
this.offset = offset;
this.endOffset = offset + len;
Expand Down
23 changes: 23 additions & 0 deletions 23 buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1855,4 +1855,27 @@ public void componentSliceFromFlattenedCompositeShouldHaveCorrectView() {
}
}

@Test
public void testFindComponent() {
CompositeByteBuf composite = newCompositeBuffer();

ByteBuf b1 = newBuffer(10);
b1.writeByte('a');
composite.addComponent(true, b1);

ByteBuf b2 = newBuffer(10);
b2.writeByte('b');
composite.addComponent(true, b2);

ByteBuf b3 = newBuffer(10);
b3.writeByte('c');
composite.addComponent(true, b3);

assertEquals('a', composite.readByte());
composite.skipBytes(1);
assertEquals('c', composite.readByte());
Comment thread
yawkat marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


composite.release();
}

}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.