Skip to content

Commit 11446f9

Browse files
committed
Minimize reference count checks in SlicedByteBuf
Motivation: SlicedByteBuf did double reference count checking for various bulk operations, which affects performance. Modifications: - Add package private method to AbstractByteBuf that can be used to check indexes without check the reference count - Use this new method in the bulk operation os SlicedByteBuf as the reference count checks take place on the wrapped buffer anyway - Fix test-case to not try to read data that is out of the bounds of the buffer. Result: Better performance on bulk operations when using SlicedByteBuf (and sub-classes)
1 parent 4c8bc81 commit 11446f9

3 files changed

Lines changed: 21 additions & 16 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,10 @@ protected final void checkIndex(int index) {
11061106

11071107
protected final void checkIndex(int index, int fieldLength) {
11081108
ensureAccessible();
1109+
checkIndex0(index, fieldLength);
1110+
}
1111+
1112+
final void checkIndex0(int index, int fieldLength) {
11091113
if (isInvalid(index, fieldLength, capacity())) {
11101114
throw new IndexOutOfBoundsException(String.format(
11111115
"index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity()));

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,33 +148,33 @@ public ByteBuf duplicate() {
148148

149149
@Override
150150
public ByteBuf copy(int index, int length) {
151-
checkIndex(index, length);
151+
checkIndex0(index, length);
152152
return buffer.copy(idx(index), length);
153153
}
154154

155155
@Override
156156
public ByteBuf slice(int index, int length) {
157-
checkIndex(index, length);
157+
checkIndex0(index, length);
158158
return buffer.slice(idx(index), length);
159159
}
160160

161161
@Override
162162
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
163-
checkIndex(index, length);
163+
checkIndex0(index, length);
164164
buffer.getBytes(idx(index), dst, dstIndex, length);
165165
return this;
166166
}
167167

168168
@Override
169169
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
170-
checkIndex(index, length);
170+
checkIndex0(index, length);
171171
buffer.getBytes(idx(index), dst, dstIndex, length);
172172
return this;
173173
}
174174

175175
@Override
176176
public ByteBuf getBytes(int index, ByteBuffer dst) {
177-
checkIndex(index, dst.remaining());
177+
checkIndex0(index, dst.remaining());
178178
buffer.getBytes(idx(index), dst);
179179
return this;
180180
}
@@ -206,47 +206,47 @@ protected void _setLong(int index, long value) {
206206

207207
@Override
208208
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
209-
checkIndex(index, length);
209+
checkIndex0(index, length);
210210
buffer.setBytes(idx(index), src, srcIndex, length);
211211
return this;
212212
}
213213

214214
@Override
215215
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
216-
checkIndex(index, length);
216+
checkIndex0(index, length);
217217
buffer.setBytes(idx(index), src, srcIndex, length);
218218
return this;
219219
}
220220

221221
@Override
222222
public ByteBuf setBytes(int index, ByteBuffer src) {
223-
checkIndex(index, src.remaining());
223+
checkIndex0(index, src.remaining());
224224
buffer.setBytes(idx(index), src);
225225
return this;
226226
}
227227

228228
@Override
229229
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
230-
checkIndex(index, length);
230+
checkIndex0(index, length);
231231
buffer.getBytes(idx(index), out, length);
232232
return this;
233233
}
234234

235235
@Override
236236
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
237-
checkIndex(index, length);
237+
checkIndex0(index, length);
238238
return buffer.getBytes(idx(index), out, length);
239239
}
240240

241241
@Override
242242
public int setBytes(int index, InputStream in, int length) throws IOException {
243-
checkIndex(index, length);
243+
checkIndex0(index, length);
244244
return buffer.setBytes(idx(index), in, length);
245245
}
246246

247247
@Override
248248
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
249-
checkIndex(index, length);
249+
checkIndex0(index, length);
250250
return buffer.setBytes(idx(index), in, length);
251251
}
252252

@@ -257,24 +257,24 @@ public int nioBufferCount() {
257257

258258
@Override
259259
public ByteBuffer nioBuffer(int index, int length) {
260-
checkIndex(index, length);
260+
checkIndex0(index, length);
261261
return buffer.nioBuffer(idx(index), length);
262262
}
263263

264264
@Override
265265
public ByteBuffer[] nioBuffers(int index, int length) {
266-
checkIndex(index, length);
266+
checkIndex0(index, length);
267267
return buffer.nioBuffers(idx(index), length);
268268
}
269269

270270
@Override
271271
public ByteBuffer internalNioBuffer(int index, int length) {
272-
checkIndex(index, length);
273272
return nioBuffer(index, length);
274273
}
275274

276275
@Override
277276
public int forEachByte(int index, int length, ByteProcessor processor) {
277+
checkIndex0(index, length);
278278
int ret = buffer.forEachByte(idx(index), length, processor);
279279
if (ret >= adjustment) {
280280
return ret - adjustment;
@@ -285,6 +285,7 @@ public int forEachByte(int index, int length, ByteProcessor processor) {
285285

286286
@Override
287287
public int forEachByteDesc(int index, int length, ByteProcessor processor) {
288+
checkIndex0(index, length);
288289
int ret = buffer.forEachByteDesc(idx(index), length, processor);
289290
if (ret >= adjustment) {
290291
return ret - adjustment;

buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2043,7 +2043,7 @@ public void testGetDoubleAfterRelease() {
20432043

20442044
@Test(expected = IllegalReferenceCountException.class)
20452045
public void testGetBytesAfterRelease() {
2046-
releasedBuffer().getBytes(0, releaseLater(buffer()));
2046+
releasedBuffer().getBytes(0, releaseLater(buffer(8)));
20472047
}
20482048

20492049
@Test(expected = IllegalReferenceCountException.class)

0 commit comments

Comments
 (0)