Skip to content

Commit 7074624

Browse files
authored
Revert "Eliminate redundant bounds checks in CompositeByteBuf accessors" (#16550)
Reverts #16525
1 parent c3b0a43 commit 7074624

2 files changed

Lines changed: 21 additions & 179 deletions

File tree

buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java

Lines changed: 20 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,6 @@ public CompositeByteBuf removeComponent(int cIndex) {
615615
Component comp = components[cIndex];
616616
if (lastAccessed == comp) {
617617
lastAccessed = null;
618-
lastAccessedIndex = 0;
619618
}
620619
comp.free();
621620
removeComp(cIndex);
@@ -647,7 +646,6 @@ public CompositeByteBuf removeComponents(int cIndex, int numComponents) {
647646
}
648647
if (lastAccessed == c) {
649648
lastAccessed = null;
650-
lastAccessedIndex = 0;
651649
}
652650
c.free();
653651
}
@@ -858,7 +856,6 @@ public CompositeByteBuf capacity(int newCapacity) {
858856
}
859857
} else if (newCapacity < oldCapacity) {
860858
lastAccessed = null;
861-
lastAccessedIndex = 0;
862859
int i = size - 1;
863860
for (int bytesToTrim = oldCapacity - newCapacity; i >= 0; i--) {
864861
Component c = components[i];
@@ -955,36 +952,20 @@ public int toByteIndex(int cIndex) {
955952
@Override
956953
public byte getByte(int index) {
957954
Component c = findComponent(index);
958-
return c.abuf != null ? c.abuf._getByte(c.idx(index)) : c.buf.getByte(c.idx(index));
959-
}
960-
961-
@Override
962-
public byte readByte() {
963-
checkReadableBytes(1);
964-
int rIdx = readerIndex;
965-
Component c = lastAccessed;
966-
if (c == null) {
967-
c = findIt(rIdx);
968-
} else if (rIdx >= c.endOffset) {
969-
c = findComponentForRead(rIdx);
970-
} else if (rIdx < c.offset) {
971-
c = findIt(rIdx);
972-
}
973-
readerIndex = rIdx + 1;
974-
return c.abuf != null ? c.abuf._getByte(rIdx + c.adjustment) : c.buf.getByte(rIdx + c.adjustment);
955+
return c.buf.getByte(c.idx(index));
975956
}
976957

977958
@Override
978959
protected byte _getByte(int index) {
979960
Component c = findComponent0(index);
980-
return c.abuf != null ? c.abuf._getByte(c.idx(index)) : c.buf.getByte(c.idx(index));
961+
return c.buf.getByte(c.idx(index));
981962
}
982963

983964
@Override
984965
protected short _getShort(int index) {
985966
Component c = findComponent0(index);
986967
if (index + 2 <= c.endOffset) {
987-
return c.abuf != null ? c.abuf._getShort(c.idx(index)) : c.buf.getShort(c.idx(index));
968+
return c.buf.getShort(c.idx(index));
988969
} else if (order() == ByteOrder.BIG_ENDIAN) {
989970
return (short) ((_getByte(index) & 0xff) << 8 | _getByte(index + 1) & 0xff);
990971
} else {
@@ -996,7 +977,7 @@ protected short _getShort(int index) {
996977
protected short _getShortLE(int index) {
997978
Component c = findComponent0(index);
998979
if (index + 2 <= c.endOffset) {
999-
return c.abuf != null ? c.abuf._getShortLE(c.idx(index)) : c.buf.getShortLE(c.idx(index));
980+
return c.buf.getShortLE(c.idx(index));
1000981
} else if (order() == ByteOrder.BIG_ENDIAN) {
1001982
return (short) (_getByte(index) & 0xff | (_getByte(index + 1) & 0xff) << 8);
1002983
} else {
@@ -1008,8 +989,7 @@ protected short _getShortLE(int index) {
1008989
protected int _getUnsignedMedium(int index) {
1009990
Component c = findComponent0(index);
1010991
if (index + 3 <= c.endOffset) {
1011-
return c.abuf != null ? c.abuf._getUnsignedMedium(c.idx(index))
1012-
: c.buf.getUnsignedMedium(c.idx(index));
992+
return c.buf.getUnsignedMedium(c.idx(index));
1013993
} else if (order() == ByteOrder.BIG_ENDIAN) {
1014994
return (_getShort(index) & 0xffff) << 8 | _getByte(index + 2) & 0xff;
1015995
} else {
@@ -1021,8 +1001,7 @@ protected int _getUnsignedMedium(int index) {
10211001
protected int _getUnsignedMediumLE(int index) {
10221002
Component c = findComponent0(index);
10231003
if (index + 3 <= c.endOffset) {
1024-
return c.abuf != null ? c.abuf._getUnsignedMediumLE(c.idx(index))
1025-
: c.buf.getUnsignedMediumLE(c.idx(index));
1004+
return c.buf.getUnsignedMediumLE(c.idx(index));
10261005
} else if (order() == ByteOrder.BIG_ENDIAN) {
10271006
return _getShortLE(index) & 0xffff | (_getByte(index + 2) & 0xff) << 16;
10281007
} else {
@@ -1034,7 +1013,7 @@ protected int _getUnsignedMediumLE(int index) {
10341013
protected int _getInt(int index) {
10351014
Component c = findComponent0(index);
10361015
if (index + 4 <= c.endOffset) {
1037-
return c.abuf != null ? c.abuf._getInt(c.idx(index)) : c.buf.getInt(c.idx(index));
1016+
return c.buf.getInt(c.idx(index));
10381017
} else if (order() == ByteOrder.BIG_ENDIAN) {
10391018
return (_getShort(index) & 0xffff) << 16 | _getShort(index + 2) & 0xffff;
10401019
} else {
@@ -1046,7 +1025,7 @@ protected int _getInt(int index) {
10461025
protected int _getIntLE(int index) {
10471026
Component c = findComponent0(index);
10481027
if (index + 4 <= c.endOffset) {
1049-
return c.abuf != null ? c.abuf._getIntLE(c.idx(index)) : c.buf.getIntLE(c.idx(index));
1028+
return c.buf.getIntLE(c.idx(index));
10501029
} else if (order() == ByteOrder.BIG_ENDIAN) {
10511030
return _getShortLE(index) & 0xffff | (_getShortLE(index + 2) & 0xffff) << 16;
10521031
} else {
@@ -1058,7 +1037,7 @@ protected int _getIntLE(int index) {
10581037
protected long _getLong(int index) {
10591038
Component c = findComponent0(index);
10601039
if (index + 8 <= c.endOffset) {
1061-
return c.abuf != null ? c.abuf._getLong(c.idx(index)) : c.buf.getLong(c.idx(index));
1040+
return c.buf.getLong(c.idx(index));
10621041
} else if (order() == ByteOrder.BIG_ENDIAN) {
10631042
return (_getInt(index) & 0xffffffffL) << 32 | _getInt(index + 4) & 0xffffffffL;
10641043
} else {
@@ -1070,7 +1049,7 @@ protected long _getLong(int index) {
10701049
protected long _getLongLE(int index) {
10711050
Component c = findComponent0(index);
10721051
if (index + 8 <= c.endOffset) {
1073-
return c.abuf != null ? c.abuf._getLongLE(c.idx(index)) : c.buf.getLongLE(c.idx(index));
1052+
return c.buf.getLongLE(c.idx(index));
10741053
} else if (order() == ByteOrder.BIG_ENDIAN) {
10751054
return _getIntLE(index) & 0xffffffffL | (_getIntLE(index + 4) & 0xffffffffL) << 32;
10761055
} else {
@@ -1201,22 +1180,14 @@ public CompositeByteBuf getBytes(int index, OutputStream out, int length) throws
12011180
@Override
12021181
public CompositeByteBuf setByte(int index, int value) {
12031182
Component c = findComponent(index);
1204-
if (c.abuf != null) {
1205-
c.abuf._setByte(c.idx(index), value);
1206-
} else {
1207-
c.buf.setByte(c.idx(index), value);
1208-
}
1183+
c.buf.setByte(c.idx(index), value);
12091184
return this;
12101185
}
12111186

12121187
@Override
12131188
protected void _setByte(int index, int value) {
12141189
Component c = findComponent0(index);
1215-
if (c.abuf != null) {
1216-
c.abuf._setByte(c.idx(index), value);
1217-
} else {
1218-
c.buf.setByte(c.idx(index), value);
1219-
}
1190+
c.buf.setByte(c.idx(index), value);
12201191
}
12211192

12221193
@Override
@@ -1230,11 +1201,7 @@ public CompositeByteBuf setShort(int index, int value) {
12301201
protected void _setShort(int index, int value) {
12311202
Component c = findComponent0(index);
12321203
if (index + 2 <= c.endOffset) {
1233-
if (c.abuf != null) {
1234-
c.abuf._setShort(c.idx(index), value);
1235-
} else {
1236-
c.buf.setShort(c.idx(index), value);
1237-
}
1204+
c.buf.setShort(c.idx(index), value);
12381205
} else if (order() == ByteOrder.BIG_ENDIAN) {
12391206
_setByte(index, (byte) (value >>> 8));
12401207
_setByte(index + 1, (byte) value);
@@ -1248,11 +1215,7 @@ protected void _setShort(int index, int value) {
12481215
protected void _setShortLE(int index, int value) {
12491216
Component c = findComponent0(index);
12501217
if (index + 2 <= c.endOffset) {
1251-
if (c.abuf != null) {
1252-
c.abuf._setShortLE(c.idx(index), value);
1253-
} else {
1254-
c.buf.setShortLE(c.idx(index), value);
1255-
}
1218+
c.buf.setShortLE(c.idx(index), value);
12561219
} else if (order() == ByteOrder.BIG_ENDIAN) {
12571220
_setByte(index, (byte) value);
12581221
_setByte(index + 1, (byte) (value >>> 8));
@@ -1273,11 +1236,7 @@ public CompositeByteBuf setMedium(int index, int value) {
12731236
protected void _setMedium(int index, int value) {
12741237
Component c = findComponent0(index);
12751238
if (index + 3 <= c.endOffset) {
1276-
if (c.abuf != null) {
1277-
c.abuf._setMedium(c.idx(index), value);
1278-
} else {
1279-
c.buf.setMedium(c.idx(index), value);
1280-
}
1239+
c.buf.setMedium(c.idx(index), value);
12811240
} else if (order() == ByteOrder.BIG_ENDIAN) {
12821241
_setShort(index, (short) (value >> 8));
12831242
_setByte(index + 2, (byte) value);
@@ -1291,11 +1250,7 @@ protected void _setMedium(int index, int value) {
12911250
protected void _setMediumLE(int index, int value) {
12921251
Component c = findComponent0(index);
12931252
if (index + 3 <= c.endOffset) {
1294-
if (c.abuf != null) {
1295-
c.abuf._setMediumLE(c.idx(index), value);
1296-
} else {
1297-
c.buf.setMediumLE(c.idx(index), value);
1298-
}
1253+
c.buf.setMediumLE(c.idx(index), value);
12991254
} else if (order() == ByteOrder.BIG_ENDIAN) {
13001255
_setShortLE(index, (short) value);
13011256
_setByte(index + 2, (byte) (value >>> 16));
@@ -1316,11 +1271,7 @@ public CompositeByteBuf setInt(int index, int value) {
13161271
protected void _setInt(int index, int value) {
13171272
Component c = findComponent0(index);
13181273
if (index + 4 <= c.endOffset) {
1319-
if (c.abuf != null) {
1320-
c.abuf._setInt(c.idx(index), value);
1321-
} else {
1322-
c.buf.setInt(c.idx(index), value);
1323-
}
1274+
c.buf.setInt(c.idx(index), value);
13241275
} else if (order() == ByteOrder.BIG_ENDIAN) {
13251276
_setShort(index, (short) (value >>> 16));
13261277
_setShort(index + 2, (short) value);
@@ -1334,11 +1285,7 @@ protected void _setInt(int index, int value) {
13341285
protected void _setIntLE(int index, int value) {
13351286
Component c = findComponent0(index);
13361287
if (index + 4 <= c.endOffset) {
1337-
if (c.abuf != null) {
1338-
c.abuf._setIntLE(c.idx(index), value);
1339-
} else {
1340-
c.buf.setIntLE(c.idx(index), value);
1341-
}
1288+
c.buf.setIntLE(c.idx(index), value);
13421289
} else if (order() == ByteOrder.BIG_ENDIAN) {
13431290
_setShortLE(index, (short) value);
13441291
_setShortLE(index + 2, (short) (value >>> 16));
@@ -1359,11 +1306,7 @@ public CompositeByteBuf setLong(int index, long value) {
13591306
protected void _setLong(int index, long value) {
13601307
Component c = findComponent0(index);
13611308
if (index + 8 <= c.endOffset) {
1362-
if (c.abuf != null) {
1363-
c.abuf._setLong(c.idx(index), value);
1364-
} else {
1365-
c.buf.setLong(c.idx(index), value);
1366-
}
1309+
c.buf.setLong(c.idx(index), value);
13671310
} else if (order() == ByteOrder.BIG_ENDIAN) {
13681311
_setInt(index, (int) (value >>> 32));
13691312
_setInt(index + 4, (int) value);
@@ -1377,11 +1320,7 @@ protected void _setLong(int index, long value) {
13771320
protected void _setLongLE(int index, long value) {
13781321
Component c = findComponent0(index);
13791322
if (index + 8 <= c.endOffset) {
1380-
if (c.abuf != null) {
1381-
c.abuf._setLongLE(c.idx(index), value);
1382-
} else {
1383-
c.buf.setLongLE(c.idx(index), value);
1384-
}
1323+
c.buf.setLongLE(c.idx(index), value);
13851324
} else if (order() == ByteOrder.BIG_ENDIAN) {
13861325
_setIntLE(index, (int) value);
13871326
_setIntLE(index + 4, (int) (value >>> 32));
@@ -1674,7 +1613,6 @@ public ByteBuf internalComponentAtOffset(int offset) {
16741613

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

16791617
private Component findComponent(int offset) {
16801618
Component la = lastAccessed;
@@ -1694,22 +1632,6 @@ private Component findComponent0(int offset) {
16941632
return findIt(offset);
16951633
}
16961634

1697-
/**
1698-
* Sequential-read fast path: try the next component(s) before falling back to binary search.
1699-
*/
1700-
private Component findComponentForRead(int offset) {
1701-
int cc = componentCount;
1702-
for (int next = lastAccessedIndex + 1; next < cc; next++) {
1703-
Component c = components[next];
1704-
if (c.endOffset > c.offset) {
1705-
lastAccessed = c;
1706-
lastAccessedIndex = next;
1707-
return c;
1708-
}
1709-
}
1710-
return findIt(offset);
1711-
}
1712-
17131635
private Component findIt(int offset) {
17141636
for (int low = 0, high = componentCount; low <= high;) {
17151637
int mid = low + high >>> 1;
@@ -1724,7 +1646,6 @@ private Component findIt(int offset) {
17241646
high = mid - 1;
17251647
} else {
17261648
lastAccessed = c;
1727-
lastAccessedIndex = mid;
17281649
return c;
17291650
}
17301651
}
@@ -1864,7 +1785,6 @@ private void consolidate0(int cIndex, int numComponents) {
18641785
components[i].transferTo(consolidated);
18651786
}
18661787
lastAccessed = null;
1867-
lastAccessedIndex = 0;
18681788
removeCompRange(cIndex + 1, endCIndex);
18691789
components[cIndex] = newComponent(consolidated, 0);
18701790
if (cIndex != 0 || numComponents != componentCount) {
@@ -1889,7 +1809,6 @@ public CompositeByteBuf discardReadComponents() {
18891809
components[i].free();
18901810
}
18911811
lastAccessed = null;
1892-
lastAccessedIndex = 0;
18931812
clearComps();
18941813
setIndex(0, 0);
18951814
adjustMarkers(readerIndex);
@@ -1912,7 +1831,6 @@ public CompositeByteBuf discardReadComponents() {
19121831
Component la = lastAccessed;
19131832
if (la != null && la.endOffset <= readerIndex) {
19141833
lastAccessed = null;
1915-
lastAccessedIndex = 0;
19161834
}
19171835
removeCompRange(0, firstComponentId);
19181836

@@ -1939,7 +1857,6 @@ public CompositeByteBuf discardReadBytes() {
19391857
components[i].free();
19401858
}
19411859
lastAccessed = null;
1942-
lastAccessedIndex = 0;
19431860
clearComps();
19441861
setIndex(0, 0);
19451862
adjustMarkers(readerIndex);
@@ -1971,7 +1888,6 @@ public CompositeByteBuf discardReadBytes() {
19711888
Component la = lastAccessed;
19721889
if (la != null && la.endOffset <= readerIndex) {
19731890
lastAccessed = null;
1974-
lastAccessedIndex = 0;
19751891
}
19761892

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

20021917
int srcAdjustment; // index of the start of this CompositeByteBuf relative to srcBuf
20031918
int adjustment; // index of the start of this CompositeByteBuf relative to buf
@@ -2012,7 +1927,6 @@ private static final class Component {
20121927
this.srcBuf = srcBuf;
20131928
this.srcAdjustment = srcOffset - offset;
20141929
this.buf = buf;
2015-
this.abuf = buf instanceof AbstractByteBuf ? (AbstractByteBuf) buf : null;
20161930
this.adjustment = bufOffset - offset;
20171931
this.offset = offset;
20181932
this.endOffset = offset + len;

0 commit comments

Comments
 (0)