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 — Avoid allocating NIO buffer views in AdaptiveByteBuf.setBytes(int, ByteBuf, int, int) by lhotari · Pull Request #17840 · netty/netty Conversation
Contributor
Motivation:
The pooled direct buffers ( This showed up in an Apache Pulsar broker profile. The broker copies slices of cached entries into adaptive buffers when it batches small messages to a consumer. The Modification:
Result: With Unsafe, copies from slices and composites into an With Unsafe:
With Unsafe, 64-byte copies:
Tests: the JMH setup:
Results are throughput in Mops/s and allocations in B/op. With Unsafe (
|
|
Found it: I proposed this same fix in #15723 (comment), using UnsafeByteBufUtil.setBytes as PooledUnsafeDirectByteBuf does. #15736 then took the absolute put route for AdaptiveByteBuf sources only, and the issue was closed on d9a8b00, which only removed duplicate() in the channel variants. Slices, composites and other allocators kept the nioBuffer() path, so this PR is the missing half. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
# Conflicts: # buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
There was an error while loading. Please reload this page.
🚨 All tests passed but jobs failed 🚨Failed Jobs without Test Failures❌ Auto-port to 4.1 / Auto-porting to 4.1 🏷️ Commit: 1a5441a Rerun ControlsClick the checkbox to trigger a rerun:
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.
|
Could not create auto-port PR. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@lhotari thanks! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Auto-port PR for 5.0: #17890 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…etBytes(int, ByteBuf, int, int) (#17890) Auto-port of #17840 to 5.0 Cherry-picked commit: ee7ab44 --- Motivation: `AdaptiveByteBuf.setBytes(int, ByteBuf, int, int)`, which `writeBytes(ByteBuf)`, `writeBytes(ByteBuf, int)` and `writeBytes(ByteBuf, int, int)` use, copies directly only from another `AdaptiveByteBuf`. From any other source, it copies through `src.nioBuffer(srcIndex, length)`, which allocates a `ByteBuffer` view for each copy. Such sources include: - a slice, even a slice of an `AdaptiveByteBuf`; - a `CompositeByteBuf`, which merges its components into a new buffer; - a buffer of another allocator. The pooled direct buffers (`PooledUnsafeDirectByteBuf` via `UnsafeByteBufUtil.setBytes`) copy such sources between memory addresses without allocating. This showed up in an Apache Pulsar broker profile. The broker copies slices of cached entries into adaptive buffers when it batches small messages to a consumer. The `ByteBuffer` views were 13 % of the broker's allocations. Modification: - When Unsafe is available and the buffer has a memory address, copy with `UnsafeByteBufUtil.setBytes`, as `PooledUnsafeDirectByteBuf` does. That copies between memory addresses, from the source's array, or lets the source copy itself into this buffer. - Check `PlatformDependent.hasUnsafe()` as well, as `PooledHeapByteBuf` does. Without Unsafe, a direct buffer can still have a memory address on Java 25+, and `UnsafeByteBufUtil` would fail with an NPE. The new test catches this on Java 25 without the check. `hasUnsafe()` comes first: it's a constant, so without Unsafe the JIT drops the branch and the copies run as before. - Add `AbstractAdaptiveByteBufTest.testSetBytesFromBuffersOfOtherKinds`, which runs for all four adaptive buffer types. It copies from: - slices of pooled direct, unpooled heap and adaptive buffers; - a composite; - a read-only buffer. It also checks the contents, the source indexes and the bounds checks. - Add `AdaptiveByteBufSetBytesBenchmark`. It copies into an adaptive direct buffer from six kinds of sources, at 64, 1024 and 16384 bytes. Run it with `-prof gc`. It enables Unsafe with `-Dio.netty.noUnsafe=false` in its `@Fork`, since Netty turns Unsafe off by default on Java 25+. Result: With Unsafe, copies from slices and composites into an `AdaptiveByteBuf` no longer allocate, and copies from direct buffers, slices and composites are faster. Without Unsafe, the copies take the same path as before. With Unsafe: - Slices of adaptive and pooled direct buffers: 64-byte copies are 2–3.4× faster, larger ones 13–70 % faster, and they no longer allocate (64 B per copy before). - Composites: 4–5× faster, and they no longer allocate 464 B to 16.8 KB per copy. - Adaptive and pooled direct sources: faster too, by 13 % to 2×. - Unpooled heap: 64-byte copies are 9 % faster (in interleaved runs); larger sizes are within noise. With Unsafe, 64-byte copies: | Source | Before | After | |---|---|---| | Slice of a pooled direct buffer | 25.5 Mops/s, 64 B per copy | 88.3 Mops/s, 0 B | | Slice of an adaptive buffer | 25.3 Mops/s, 64 B per copy | 56.8 Mops/s, 0 B | | Composite | 5.4 Mops/s, 464 B per copy | 21.9 Mops/s, 0 B | Tests: the `buffer` module passes on JDK 21, with and without `-PnoUnsafe`. The `Adaptive*Test` classes pass on JDK 25, with and without `-PnoUnsafe`. JMH setup: - JDK 25.0.4; - `-wi 5 -i 5 -f 2 -prof gc`; - turbo boost and thermald off; - baseline: 4.2 at 8ee32af plus the benchmark; - candidate: this commit. Results are throughput in Mops/s and allocations in B/op. <details open><summary>With Unsafe (<code>-Dio.netty.noUnsafe=false</code>)</summary> ``` == unsafe (Mops/s; B/op) source size base Mops/s cand Mops/s change base B/op cand B/op ADAPTIVE 64 73.1± 0.8 85.1± 0.3 +16% 0.0 0.0 ADAPTIVE 1024 31.9± 0.3 39.9± 0.2 +25% 0.0 0.0 ADAPTIVE 16384 1.9± 0.1 2.6± 0.2 +36% 0.0 0.0 ADAPTIVE_SLICE 64 25.3± 0.8 56.8± 0.7 +125% 64.0 0.0 ADAPTIVE_SLICE 1024 17.0± 0.7 23.4± 7.7 +37% 64.0 0.0 ADAPTIVE_SLICE 16384 1.7± 0.5 1.9± 0.3 +13% 64.0 0.0 POOLED_DIRECT 64 46.6± 0.3 91.8± 5.2 +97% 0.0 0.0 POOLED_DIRECT 1024 23.2± 6.3 29.3± 3.1 +26% 0.0 0.0 POOLED_DIRECT 16384 1.6± 0.3 2.0± 0.0 +23% 0.0 0.0 POOLED_DIRECT_SLICE 64 25.5± 0.1 88.3± 0.4 +247% 64.0 0.0 POOLED_DIRECT_SLICE 1024 17.1± 0.2 29.0± 2.5 +70% 64.0 0.0 POOLED_DIRECT_SLICE 16384 1.7± 0.2 2.2± 0.2 +28% 64.0 0.0 UNPOOLED_HEAP 64 87.9± 1.2 95.2± 0.4 +8% 0.0 0.0 UNPOOLED_HEAP 1024 36.1± 3.3 33.7± 6.0 -7% 0.0 0.0 UNPOOLED_HEAP 16384 2.0± 0.0 2.2± 0.5 +12% 0.0 0.0 COMPOSITE 64 5.4± 0.1 21.9± 0.3 +304% 464.0 0.0 COMPOSITE 1024 3.6± 0.0 15.5± 0.1 +326% 1424.0 0.0 COMPOSITE 16384 0.4± 0.0 1.8± 0.1 +325% 16784.0 0.0 ``` </details> <details><summary>Without Unsafe (<code>-Dio.netty.noUnsafe=true</code>): same path as before</summary> ``` == nounsafe (Mops/s; B/op) source size base Mops/s cand Mops/s change base B/op cand B/op ADAPTIVE 64 55.3± 1.2 55.7± 2.2 +1% 0.0 0.0 ADAPTIVE 1024 29.3± 0.1 29.3± 0.2 -0% 0.0 0.0 ADAPTIVE 16384 2.2± 0.1 2.0± 0.3 -9% 0.0 0.0 ADAPTIVE_SLICE 64 39.2± 2.0 40.4± 0.5 +3% 0.0 0.0 ADAPTIVE_SLICE 1024 16.3± 0.4 16.8± 1.5 +3% 0.0 0.0 ADAPTIVE_SLICE 16384 1.8± 0.2 1.4± 0.0 -23% 0.0 0.0 POOLED_DIRECT 64 41.3± 0.3 41.2± 0.4 -0% 0.0 0.0 POOLED_DIRECT 1024 26.6± 0.8 26.8± 0.8 +1% 0.0 0.0 POOLED_DIRECT 16384 1.9± 0.2 2.1± 0.1 +11% 0.0 0.0 POOLED_DIRECT_SLICE 64 21.1± 0.2 21.3± 0.2 +1% 64.0 64.0 POOLED_DIRECT_SLICE 1024 15.3± 0.9 16.5± 0.7 +8% 64.0 64.0 POOLED_DIRECT_SLICE 16384 1.9± 0.1 1.8± 0.2 -3% 64.0 64.0 UNPOOLED_HEAP 64 74.6± 7.3 79.3± 0.2 +6% 0.0 0.0 UNPOOLED_HEAP 1024 35.5± 3.2 34.7± 2.5 -2% 0.0 0.0 UNPOOLED_HEAP 16384 2.0± 0.3 1.8± 0.1 -10% 0.0 0.0 COMPOSITE 64 5.1± 0.1 5.1± 0.1 -0% 464.0 464.0 COMPOSITE 1024 3.6± 0.0 3.6± 0.0 -0% 1424.0 1424.0 COMPOSITE 16384 0.4± 0.0 0.4± 0.0 -5% 16784.0 16784.0 ``` The 16 KB results vary by 10–20 % between runs of the same code on this host, so differences at that size are noise. </details> Co-authored-by: Lari Hotari <[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.