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 — Eliminate redundant bounds checks in CompositeByteBuf accessors · netty/netty@ff008bb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src
    • main/java/io/netty/buffer
      • CompositeByteBuf.java
    • test/java/io/netty/buffer
      • AbstractCompositeByteBufTest.java
  • microbench/src/main/java/io/netty/buffer
    • CompositeByteBufSequentialBenchmark.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java
+64-20Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -952,20 +952,20 @@ public int toByteIndex(int cIndex) {
952952
@Override
953953
public byte getByte(int index) {
954954
Component c = findComponent(index);
955-
return c.buf.getByte(c.idx(index));
955+
return c.abuf != null ? c.abuf._getByte(c.idx(index)) : c.buf.getByte(c.idx(index));
956956
}
957957

958958
@Override
959959
protected byte _getByte(int index) {
960960
Component c = findComponent0(index);
961-
return c.buf.getByte(c.idx(index));
961+
return c.abuf != null ? c.abuf._getByte(c.idx(index)) : c.buf.getByte(c.idx(index));
962962
}
963963

964964
@Override
965965
protected short _getShort(int index) {
966966
Component c = findComponent0(index);
967967
if (index + 2 <= c.endOffset) {
968-
return c.buf.getShort(c.idx(index));
968+
return c.abuf != null ? c.abuf._getShort(c.idx(index)) : c.buf.getShort(c.idx(index));
969969
} else if (order() == ByteOrder.BIG_ENDIAN) {
970970
return (short) ((_getByte(index) & 0xff) << 8 | _getByte(index + 1) & 0xff);
971971
} else {
@@ -977,7 +977,7 @@ protected short _getShort(int index) {
977977
protected short _getShortLE(int index) {
978978
Component c = findComponent0(index);
979979
if (index + 2 <= c.endOffset) {
980-
return c.buf.getShortLE(c.idx(index));
980+
return c.abuf != null ? c.abuf._getShortLE(c.idx(index)) : c.buf.getShortLE(c.idx(index));
981981
} else if (order() == ByteOrder.BIG_ENDIAN) {
982982
return (short) (_getByte(index) & 0xff | (_getByte(index + 1) & 0xff) << 8);
983983
} else {
@@ -989,7 +989,8 @@ protected short _getShortLE(int index) {
989989
protected int _getUnsignedMedium(int index) {
990990
Component c = findComponent0(index);
991991
if (index + 3 <= c.endOffset) {
992-
return c.buf.getUnsignedMedium(c.idx(index));
992+
return c.abuf != null ? c.abuf._getUnsignedMedium(c.idx(index))
993+
: c.buf.getUnsignedMedium(c.idx(index));
993994
} else if (order() == ByteOrder.BIG_ENDIAN) {
994995
return (_getShort(index) & 0xffff) << 8 | _getByte(index + 2) & 0xff;
995996
} else {
@@ -1001,7 +1002,8 @@ protected int _getUnsignedMedium(int index) {
10011002
protected int _getUnsignedMediumLE(int index) {
10021003
Component c = findComponent0(index);
10031004
if (index + 3 <= c.endOffset) {
1004-
return c.buf.getUnsignedMediumLE(c.idx(index));
1005+
return c.abuf != null ? c.abuf._getUnsignedMediumLE(c.idx(index))
1006+
: c.buf.getUnsignedMediumLE(c.idx(index));
10051007
} else if (order() == ByteOrder.BIG_ENDIAN) {
10061008
return _getShortLE(index) & 0xffff | (_getByte(index + 2) & 0xff) << 16;
10071009
} else {
@@ -1013,7 +1015,7 @@ protected int _getUnsignedMediumLE(int index) {
10131015
protected int _getInt(int index) {
10141016
Component c = findComponent0(index);
10151017
if (index + 4 <= c.endOffset) {
1016-
return c.buf.getInt(c.idx(index));
1018+
return c.abuf != null ? c.abuf._getInt(c.idx(index)) : c.buf.getInt(c.idx(index));
10171019
} else if (order() == ByteOrder.BIG_ENDIAN) {
10181020
return (_getShort(index) & 0xffff) << 16 | _getShort(index + 2) & 0xffff;
10191021
} else {
@@ -1025,7 +1027,7 @@ protected int _getInt(int index) {
10251027
protected int _getIntLE(int index) {
10261028
Component c = findComponent0(index);
10271029
if (index + 4 <= c.endOffset) {
1028-
return c.buf.getIntLE(c.idx(index));
1030+
return c.abuf != null ? c.abuf._getIntLE(c.idx(index)) : c.buf.getIntLE(c.idx(index));
10291031
} else if (order() == ByteOrder.BIG_ENDIAN) {
10301032
return _getShortLE(index) & 0xffff | (_getShortLE(index + 2) & 0xffff) << 16;
10311033
} else {
@@ -1037,7 +1039,7 @@ protected int _getIntLE(int index) {
10371039
protected long _getLong(int index) {
10381040
Component c = findComponent0(index);
10391041
if (index + 8 <= c.endOffset) {
1040-
return c.buf.getLong(c.idx(index));
1042+
return c.abuf != null ? c.abuf._getLong(c.idx(index)) : c.buf.getLong(c.idx(index));
10411043
} else if (order() == ByteOrder.BIG_ENDIAN) {
10421044
return (_getInt(index) & 0xffffffffL) << 32 | _getInt(index + 4) & 0xffffffffL;
10431045
} else {
@@ -1049,7 +1051,7 @@ protected long _getLong(int index) {
10491051
protected long _getLongLE(int index) {
10501052
Component c = findComponent0(index);
10511053
if (index + 8 <= c.endOffset) {
1052-
return c.buf.getLongLE(c.idx(index));
1054+
return c.abuf != null ? c.abuf._getLongLE(c.idx(index)) : c.buf.getLongLE(c.idx(index));
10531055
} else if (order() == ByteOrder.BIG_ENDIAN) {
10541056
return _getIntLE(index) & 0xffffffffL | (_getIntLE(index + 4) & 0xffffffffL) << 32;
10551057
} else {
@@ -1180,14 +1182,22 @@ public CompositeByteBuf getBytes(int index, OutputStream out, int length) throws
11801182
@Override
11811183
public CompositeByteBuf setByte(int index, int value) {
11821184
Component c = findComponent(index);
1183-
c.buf.setByte(c.idx(index), value);
1185+
if (c.abuf != null) {
1186+
c.abuf._setByte(c.idx(index), value);
1187+
} else {
1188+
c.buf.setByte(c.idx(index), value);
1189+
}
11841190
return this;
11851191
}
11861192

11871193
@Override
11881194
protected void _setByte(int index, int value) {
11891195
Component c = findComponent0(index);
1190-
c.buf.setByte(c.idx(index), value);
1196+
if (c.abuf != null) {
1197+
c.abuf._setByte(c.idx(index), value);
1198+
} else {
1199+
c.buf.setByte(c.idx(index), value);
1200+
}
11911201
}
11921202

11931203
@Override
@@ -1201,7 +1211,11 @@ public CompositeByteBuf setShort(int index, int value) {
12011211
protected void _setShort(int index, int value) {
12021212
Component c = findComponent0(index);
12031213
if (index + 2 <= c.endOffset) {
1204-
c.buf.setShort(c.idx(index), value);
1214+
if (c.abuf != null) {
1215+
c.abuf._setShort(c.idx(index), value);
1216+
} else {
1217+
c.buf.setShort(c.idx(index), value);
1218+
}
12051219
} else if (order() == ByteOrder.BIG_ENDIAN) {
12061220
_setByte(index, (byte) (value >>> 8));
12071221
_setByte(index + 1, (byte) value);
@@ -1215,7 +1229,11 @@ protected void _setShort(int index, int value) {
12151229
protected void _setShortLE(int index, int value) {
12161230
Component c = findComponent0(index);
12171231
if (index + 2 <= c.endOffset) {
1218-
c.buf.setShortLE(c.idx(index), value);
1232+
if (c.abuf != null) {
1233+
c.abuf._setShortLE(c.idx(index), value);
1234+
} else {
1235+
c.buf.setShortLE(c.idx(index), value);
1236+
}
12191237
} else if (order() == ByteOrder.BIG_ENDIAN) {
12201238
_setByte(index, (byte) value);
12211239
_setByte(index + 1, (byte) (value >>> 8));
@@ -1236,7 +1254,11 @@ public CompositeByteBuf setMedium(int index, int value) {
12361254
protected void _setMedium(int index, int value) {
12371255
Component c = findComponent0(index);
12381256
if (index + 3 <= c.endOffset) {
1239-
c.buf.setMedium(c.idx(index), value);
1257+
if (c.abuf != null) {
1258+
c.abuf._setMedium(c.idx(index), value);
1259+
} else {
1260+
c.buf.setMedium(c.idx(index), value);
1261+
}
12401262
} else if (order() == ByteOrder.BIG_ENDIAN) {
12411263
_setShort(index, (short) (value >> 8));
12421264
_setByte(index + 2, (byte) value);
@@ -1250,7 +1272,11 @@ protected void _setMedium(int index, int value) {
12501272
protected void _setMediumLE(int index, int value) {
12511273
Component c = findComponent0(index);
12521274
if (index + 3 <= c.endOffset) {
1253-
c.buf.setMediumLE(c.idx(index), value);
1275+
if (c.abuf != null) {
1276+
c.abuf._setMediumLE(c.idx(index), value);
1277+
} else {
1278+
c.buf.setMediumLE(c.idx(index), value);
1279+
}
12541280
} else if (order() == ByteOrder.BIG_ENDIAN) {
12551281
_setShortLE(index, (short) value);
12561282
_setByte(index + 2, (byte) (value >>> 16));
@@ -1271,7 +1297,11 @@ public CompositeByteBuf setInt(int index, int value) {
12711297
protected void _setInt(int index, int value) {
12721298
Component c = findComponent0(index);
12731299
if (index + 4 <= c.endOffset) {
1274-
c.buf.setInt(c.idx(index), value);
1300+
if (c.abuf != null) {
1301+
c.abuf._setInt(c.idx(index), value);
1302+
} else {
1303+
c.buf.setInt(c.idx(index), value);
1304+
}
12751305
} else if (order() == ByteOrder.BIG_ENDIAN) {
12761306
_setShort(index, (short) (value >>> 16));
12771307
_setShort(index + 2, (short) value);
@@ -1285,7 +1315,11 @@ protected void _setInt(int index, int value) {
12851315
protected void _setIntLE(int index, int value) {
12861316
Component c = findComponent0(index);
12871317
if (index + 4 <= c.endOffset) {
1288-
c.buf.setIntLE(c.idx(index), value);
1318+
if (c.abuf != null) {
1319+
c.abuf._setIntLE(c.idx(index), value);
1320+
} else {
1321+
c.buf.setIntLE(c.idx(index), value);
1322+
}
12891323
} else if (order() == ByteOrder.BIG_ENDIAN) {
12901324
_setShortLE(index, (short) value);
12911325
_setShortLE(index + 2, (short) (value >>> 16));
@@ -1306,7 +1340,11 @@ public CompositeByteBuf setLong(int index, long value) {
13061340
protected void _setLong(int index, long value) {
13071341
Component c = findComponent0(index);
13081342
if (index + 8 <= c.endOffset) {
1309-
c.buf.setLong(c.idx(index), value);
1343+
if (c.abuf != null) {
1344+
c.abuf._setLong(c.idx(index), value);
1345+
} else {
1346+
c.buf.setLong(c.idx(index), value);
1347+
}
13101348
} else if (order() == ByteOrder.BIG_ENDIAN) {
13111349
_setInt(index, (int) (value >>> 32));
13121350
_setInt(index + 4, (int) value);
@@ -1320,7 +1358,11 @@ protected void _setLong(int index, long value) {
13201358
protected void _setLongLE(int index, long value) {
13211359
Component c = findComponent0(index);
13221360
if (index + 8 <= c.endOffset) {
1323-
c.buf.setLongLE(c.idx(index), value);
1361+
if (c.abuf != null) {
1362+
c.abuf._setLongLE(c.idx(index), value);
1363+
} else {
1364+
c.buf.setLongLE(c.idx(index), value);
1365+
}
13241366
} else if (order() == ByteOrder.BIG_ENDIAN) {
13251367
_setIntLE(index, (int) value);
13261368
_setIntLE(index + 4, (int) (value >>> 32));
@@ -1913,6 +1955,7 @@ public String toString() {
19131955
private static final class Component {
19141956
final ByteBuf srcBuf; // the originally added buffer
19151957
final ByteBuf buf; // srcBuf unwrapped zero or more times
1958+
final AbstractByteBuf abuf; // buf cast to AbstractByteBuf, or null if not an instance
19161959

19171960
int srcAdjustment; // index of the start of this CompositeByteBuf relative to srcBuf
19181961
int adjustment; // index of the start of this CompositeByteBuf relative to buf
@@ -1927,6 +1970,7 @@ private static final class Component {
19271970
this.srcBuf = srcBuf;
19281971
this.srcAdjustment = srcOffset - offset;
19291972
this.buf = buf;
1973+
this.abuf = buf instanceof AbstractByteBuf ? (AbstractByteBuf) buf : null;
19301974
this.adjustment = bufOffset - offset;
19311975
this.offset = offset;
19321976
this.endOffset = offset + len;
Collapse file

‎buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.nio.ByteBuffer;
2626
import java.nio.ByteOrder;
27+
import java.util.Arrays;
2728
import java.util.ArrayList;
2829
import java.util.Collections;
2930
import java.util.ConcurrentModificationException;
@@ -1855,4 +1856,55 @@ public void componentSliceFromFlattenedCompositeShouldHaveCorrectView() {
18551856
}
18561857
}
18571858

1859+
@Test
1860+
public void testDiscardReadComponentsDoesNotCorruptNextRead() {
1861+
int bufferSize = 32768;
1862+
byte[] sourceData = new byte[2 * bufferSize];
1863+
CompositeByteBuf compositeBuffer = newCompositeBuffer();
1864+
1865+
// First buffer component will be full of 1's
1866+
Arrays.fill(sourceData, (byte) 0x01);
1867+
ByteBuf buffer1 = newBuffer(2 * bufferSize);
1868+
buffer1.writeBytes(sourceData, 0, bufferSize);
1869+
assertEquals(bufferSize, buffer1.readableBytes());
1870+
compositeBuffer.addFlattenedComponents(true, buffer1);
1871+
1872+
// Second buffer component will be full of 2's, with a non-zero internal offset
1873+
Arrays.fill(sourceData, (byte) 0x02);
1874+
ByteBuf buffer2 = newBuffer(2 * bufferSize);
1875+
buffer2.writeBytes(sourceData, 0, 32781);
1876+
buffer2.skipBytes(32781);
1877+
buffer2.writeBytes(sourceData, 0, bufferSize);
1878+
compositeBuffer.addFlattenedComponents(true, buffer2);
1879+
1880+
// Read into the second buffer to establish cached component state
1881+
compositeBuffer.skipBytes(bufferSize + 9);
1882+
byte readByte = compositeBuffer.readByte();
1883+
assertEquals((byte) 0x02, readByte);
1884+
1885+
// Advance further but stay in the second buffer
1886+
compositeBuffer.skipBytes(22519);
1887+
1888+
// Discard the first buffer
1889+
compositeBuffer.discardReadComponents();
1890+
1891+
// Add more buffers
1892+
Arrays.fill(sourceData, (byte) 0x03);
1893+
ByteBuf buffer3 = newBuffer(bufferSize);
1894+
buffer3.writeBytes(sourceData, 0, bufferSize);
1895+
compositeBuffer.addFlattenedComponents(true, buffer3);
1896+
1897+
Arrays.fill(sourceData, (byte) 0x04);
1898+
ByteBuf buffer4 = newBuffer(bufferSize);
1899+
buffer4.writeBytes(sourceData, 0, bufferSize);
1900+
compositeBuffer.addFlattenedComponents(true, buffer4);
1901+
1902+
// Skip past the remaining second buffer data into the third buffer
1903+
compositeBuffer.skipBytes(bufferSize);
1904+
readByte = compositeBuffer.readByte();
1905+
assertEquals((byte) 0x03, readByte);
1906+
1907+
compositeBuffer.release();
1908+
}
1909+
18581910
}

0 commit comments

Comments
0 (0)
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.