GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |
GeezerVison is showing github.com — Auto-port 4.1: Optimize redundant bounds checking in ByteBuf.setBytes by netty-project-bot · Pull Request #17827 · netty/netty Conversation
Contributor
Auto-port of #17023 to 4.1 MotivationWhen ModificationThree
The Note: No behavior change — this is purely an internal optimization that removes redundant bounds checking from the hottest code path in buffer-to-buffer transfers. Result
Closes #14824
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions## Motivation When `ByteBuf.setBytes(int index, ByteBuf src, int srcIndex, int length)` is called with an array-backed source (`src.hasArray() == true`), the old code delegated to `setBytes(int index, byte[] src, int srcIndex, int length)`, which performs a second `checkSrcIndex` against `src.array().length`. This check is redundant because the outer `checkSrcIndex` already validated the operation against `src.capacity()`, and for array-backed buffers `capacity = array.length - arrayOffset`, so the outer check strictly dominates the inner one. ## Modification Three `ByteBuf` implementations are affected by this pattern — `UnpooledHeapByteBuf`, `PooledHeapByteBuf`, and `PooledDirectByteBuf`. All three had their `setBytes(int index, ByteBuf src, int srcIndex, int length)` method modified to copy directly from the source array instead of routing through the `setBytes(byte[], ...)` overload: - **`UnpooledHeapByteBuf`**: `setBytes(index, src.array(), ...)` → `System.arraycopy(src.array(), src.arrayOffset() + srcIndex, array, index, length)` - **`PooledHeapByteBuf`**: `setBytes(index, src.array(), ...)` → `System.arraycopy(src.array(), src.arrayOffset() + srcIndex, memory, idx(index), length)` - **`PooledDirectByteBuf`**: `setBytes(index, src.array(), ...)` → `_internalNioBuffer(index, length, false).put(src.array(), src.arrayOffset() + srcIndex, length)` The `PooledUnsafeDirectByteBuf` and `UnpooledUnsafeDirectByteBuf` classes already used a direct copy path in their `hasArray()` branch and did not need changes. Note: `PooledDirectByteBuf` extends `PooledByteBuf<ByteBuffer>` — its internal `memory` field is a *direct* (off-heap) `ByteBuffer`, not a `byte[]`. `System.arraycopy()` only supports copying between Java heap arrays, so the direct buffer path requires `_internalNioBuffer().put()` instead. The heap-buffer variants (`UnpooledHeapByteBuf`, `PooledHeapByteBuf`) use `System.arraycopy()` directly since their memory is a `byte[]`. No behavior change — this is purely an internal optimization that removes redundant bounds checking from the hottest code path in buffer-to-buffer transfers. ## Result - **No more redundant `checkSrcIndex`**: the first check against `src.capacity()` is the only one needed - **Faster transfers** from array-backed sources to any buffer type Closes #14824 (cherry picked from commit d972cae)
|
There was an error while loading. Please reload this page.
✅ All tests passed ✅🏷️ Commit: eed5a5d Learn more about TestLens at testlens.app/docs. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…netty#17827) Auto-port of netty#17023 to 4.1 Cherry-picked commit: d972cae --- ## Motivation When `ByteBuf.setBytes(int index, ByteBuf src, int srcIndex, int length)` is called with an array-backed source (`src.hasArray() == true`), the old code delegated to `setBytes(int index, byte[] src, int srcIndex, int length)`, which performs a second `checkSrcIndex` against `src.array().length`. This check is redundant because the outer `checkSrcIndex` already validated the operation against `src.capacity()`, and for array-backed buffers `capacity = array.length - arrayOffset`, so the outer check strictly dominates the inner one. ## Modification Three `ByteBuf` implementations are affected by this pattern — `UnpooledHeapByteBuf`, `PooledHeapByteBuf`, and `PooledDirectByteBuf`. All three had their `setBytes(int index, ByteBuf src, int srcIndex, int length)` method modified to copy directly from the source array instead of routing through the `setBytes(byte[], ...)` overload: - **`UnpooledHeapByteBuf`**: `setBytes(index, src.array(), ...)` → `System.arraycopy(src.array(), src.arrayOffset() + srcIndex, array, index, length)` - **`PooledHeapByteBuf`**: `setBytes(index, src.array(), ...)` → `System.arraycopy(src.array(), src.arrayOffset() + srcIndex, memory, idx(index), length)` - **`PooledDirectByteBuf`**: `setBytes(index, src.array(), ...)` → `_internalNioBuffer(index, length, false).put(src.array(), src.arrayOffset() + srcIndex, length)` The `PooledUnsafeDirectByteBuf` and `UnpooledUnsafeDirectByteBuf` classes already used a direct copy path in their `hasArray()` branch and did not need changes. Note: `PooledDirectByteBuf` extends `PooledByteBuf<ByteBuffer>` — its internal `memory` field is a *direct* (off-heap) `ByteBuffer`, not a `byte[]`. `System.arraycopy()` only supports copying between Java heap arrays, so the direct buffer path requires `_internalNioBuffer().put()` instead. The heap-buffer variants (`UnpooledHeapByteBuf`, `PooledHeapByteBuf`) use `System.arraycopy()` directly since their memory is a `byte[]`. No behavior change — this is purely an internal optimization that removes redundant bounds checking from the hottest code path in buffer-to-buffer transfers. ## Result - **No more redundant `checkSrcIndex`**: the first check against `src.capacity()` is the only one needed - **Faster transfers** from array-backed sources to any buffer type Closes netty#14824 Co-authored-by: Vasily Pelikh <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.