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 — Make AdaptiveByteBuf.memoryAddress() faster by lhotari · Pull Request #17767 · netty/netty Conversation
|
| Before | After | |
|---|---|---|
| JDK 21, run 1, ops/s | 18,497,697 ± 74,560 | 30,904,677 ± 69,876 |
| JDK 21, run 2, ops/s | 18,514,434 ± 31,468 | 30,915,307 ± 74,873 |
| JDK 25, run 1, ops/s | 18,532,696 ± 50,826 | 30,851,633 ± 296,462 |
| JDK 25, run 2, ops/s | 18,558,233 ± 23,243 | 30,877,691 ± 173,624 |
That's 3.38 ns per buffer before and 2.02 ns after on both JDKs: 67 % more throughput, with warm caches and a monomorphic call site.
Benchmark (buffers) (size) Mode Cnt Score Error Units
JDK 21 before:
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 18497696.574 ± 74560.210 ops/s
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 18514434.341 ± 31467.599 ops/s
JDK 21 after:
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 30904677.380 ± 69875.648 ops/s
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 30915306.886 ± 74873.233 ops/s
JDK 25 before:
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 18532696.343 ± 50826.312 ops/s
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 18558232.849 ± 23243.203 ops/s
JDK 25 after:
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 30851632.966 ± 296461.862 ops/s
AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 30877691.180 ± 173623.904 ops/s
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Motivation: An AdaptiveByteBuf is a segment of a chunk's root buffer. AdaptiveByteBuf._memoryAddress() computed its address by calling the root buffer's _memoryAddress() and adding the segment's offset. On every call, the root buffer's _memoryAddress() checks isAccessible(), calls hasMemoryAddress(), and calls memoryAddress(), which checks the reference count again in ensureAccessible(). These checks on the root buffer are redundant: the root buffer stays accessible while the AdaptiveByteBuf holds a segment of its chunk, and the root buffer's address doesn't change. A gathering write in the native transports gets the memory address of each buffer that it writes (IovArray.add()). In a profile of an Apache Pulsar broker writing 8 KB messages, the checks on the root buffer were 3.3 % of the I/O threads' CPU. Also, when the root buffer has no memory address, such as a heap chunk, memoryAddress() and _memoryAddress() returned the segment's offset in the chunk, which isn't an address. Modification: - Chunk computes the root buffer's memory address once, when the chunk is created. - AdaptiveByteBuf.init() stores the buffer's memory address, the root buffer's address plus startIndex, in a new field. When the root buffer has no memory address, it stores 0, as AbstractByteBuf._memoryAddress() returns for buffers without one. A reallocation calls init() again. - AdaptiveByteBuf._memoryAddress() returns the field, instead of calling the root buffer's _memoryAddress(). The public memoryAddress() still checks that the AdaptiveByteBuf itself is accessible. - deallocate() resets the field, so _memoryAddress() still returns 0 after the buffer is deallocated. - AdaptiveByteBufAllocatorTest checks a direct buffer's memoryAddress() against the address of its NIO buffer, also after the buffer grows into another segment, and that a heap buffer's memoryAddress() is 0. - AdaptiveByteBufMemoryAddressBenchmark gets the memory addresses of 16 direct buffers, as a gathering write does. Result: Each memoryAddress() call checks a reference count once, the AdaptiveByteBuf's own, instead of three times, and an AdaptiveByteBuf without a memory address returns 0 instead of its offset. With 16 buffers of 8 KB, AdaptiveByteBufMemoryAddressBenchmark gets 75 % more throughput: 3.06 ns per buffer before and 1.75 ns after.
|
Re
Let me challenge/stress this. So, very important, did you make sure Unsafe or VarHandle are properly used? That said, I'm happy if we can save work to happen 🚀 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
@franz1981 Good question. I went back to the JFR recording (combined recording of async-profiler (cpu 10ms interval) + JFR + jonoffcpu for off-cpu profiling with eBPF) of that profile to check. The broker ran on JDK 25 (25.0.4.1) with Relevant JVM options from the recordingIn that profile (8 KB messages, 8 I/O threads), The most common stack (2.1 % of the I/O threads' CPU samples)For context, this was tested on a rather old CPU: a 2019 Dell XPS 15 7590 laptop with an Intel Core i9-9980HK (8 cores, 16 threads). The broker profile comes from the Pulsar Performance Testing Framework, which runs the Pulsar cluster and its clients in Docker, here on Linux (Pop!_OS 24.04, kernel 7.1.5). To reduce variance between test runs, the framework can control the power-saving settings and fix the CPUs at 2.4 GHz, with no turbo boost. The JMH benchmark in this PR ran with the same settings. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
So @lhotari you are still thinking this is worthwhile correct ? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
@normanmaurer yes, I think that this change is a useful performance improvement. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Lgtm
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…ress # Conflicts: # buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
Motivation: AbstractByteBufTest.testMemoryAddressUnsupportedWhenNoMemoryAddress (netty#17820) checks that memoryAddress() throws for a buffer without a memory address, which covers the heap buffers of the adaptive allocator. testHeapBufferMemoryAddressIsZero expected 0 from it instead, and fails since. Modification: Remove testHeapBufferMemoryAddressIsZero. Result: The adaptive allocator's tests pass with netty#17820.
|
@normanmaurer I have resolved the merge conflicts after #17820 was merged to 4.2. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
🚨 TestLens detected 2 failed tests 🚨Failed Jobs without Test Failures❌ Auto-port to 4.1 / Auto-porting to 4.1 Test SummaryTip Inspect all details with the TestLens CLI: Build PR / macos-aarch64-java11-boringssl build > Netty/Transport/Native/KQueue
🏷️ Commit: bc01fbc Test FailuresKQueueDatagramUnicastIPv6MappedTest > testSimpleSendCompositeDirectByteBuf(TestInfo) (Netty/Transport/Native/KQueue in Build PR / macos-aarch64-java11-boringssl build)KQueueDatagramUnicastIPv6MappedTest > testSimpleSendWithoutBind(TestInfo) (Netty/Transport/Native/KQueue in Build PR / macos-aarch64-java11-boringssl build)Rerun ControlsSelect tests to mute in this pull request:
Reuse successful test results:
Click 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.
|
Could not create auto-port PR. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Motivation: An `AdaptiveByteBuf` is a segment of a chunk's root buffer. `AdaptiveByteBuf._memoryAddress()` computes its address by calling the root buffer's `_memoryAddress()` and adding the segment's offset. On every call, the root buffer's `_memoryAddress()`: 1. checks `isAccessible()`, 2. calls `hasMemoryAddress()`, 3. calls `memoryAddress()`, which checks the reference count again in `ensureAccessible()`. These checks on the root buffer are redundant. The root buffer stays accessible while the `AdaptiveByteBuf` holds a segment of its chunk, and the root buffer's address doesn't change. A gathering write in the native transports gets the memory address of each buffer that it writes (`IovArray.add()`). In a profile of an Apache Pulsar broker writing 8 KB messages, the checks on the root buffer were 3.3 % of the I/O threads' CPU. Modification: Existing behavior that this relies on: - The address of an `AdaptiveByteBuf` is the root buffer's address plus the segment's offset in the chunk (`startIndex`). - The allocator calls `AdaptiveByteBuf.init()` when it hands out a buffer. When a buffer grows beyond its segment, the reallocation moves it to a new segment and calls `init()` again. Changes: - `Chunk` computes the root buffer's memory address once, when the chunk is created. - `AdaptiveByteBuf.init()` stores the buffer's memory address, the root buffer's address plus `startIndex`, in a new field. When the root buffer has no memory address, it stores 0, as `AbstractByteBuf._memoryAddress()` returns for buffers without one. - `AdaptiveByteBuf._memoryAddress()` returns the field, instead of calling the root buffer's `_memoryAddress()`. The public `memoryAddress()` still checks that the `AdaptiveByteBuf` itself is accessible, and, since #17820, throws `UnsupportedOperationException` when it has no memory address. - `deallocate()` resets the field, so `_memoryAddress()` still returns 0 after the buffer is deallocated. - A new test in `AdaptiveByteBufAllocatorTest`, `testDirectBufferMemoryAddressFollowsItsSegment`, allocates two direct buffers, so that the second one is likely at an offset above zero in the chunk. It compares each buffer's `memoryAddress()` with the address of the NIO buffer of the same range, which is computed from the chunk's buffer separately. It compares again after a buffer grows into another segment, and checks that `_memoryAddress()` returns 0 after release. `AbstractByteBufTest.testMemoryAddressUnsupportedWhenNoMemoryAddress` (#17820) covers the heap buffers. - A new benchmark, `AdaptiveByteBufMemoryAddressBenchmark`, gets the memory addresses of 16 direct buffers, as a gathering write does. Result: Each `memoryAddress()` call checks a reference count once, the `AdaptiveByteBuf`'s own, instead of three times. The root buffer's address is computed once per chunk; allocating and releasing a buffer each add one field write. `AdaptiveByteBufMemoryAddressBenchmark` with 16 buffers of 8 KB, on an Intel i9-9980HK at a fixed 2.4 GHz, with Unsafe enabled (`-Dio.netty.noUnsafe=false`), 2 alternating runs per side. Before is 4.2 at 60d9baf with only the benchmark added: | | Before | After | |---|---:|---:| | JDK 21, run 1, ops/s | 18,497,697 ± 74,560 | 30,904,677 ± 69,876 | | JDK 21, run 2, ops/s | 18,514,434 ± 31,468 | 30,915,307 ± 74,873 | | JDK 25, run 1, ops/s | 18,532,696 ± 50,826 | 30,851,633 ± 296,462 | | JDK 25, run 2, ops/s | 18,558,233 ± 23,243 | 30,877,691 ± 173,624 | That's 3.38 ns per buffer before and 2.02 ns after on both JDKs: 67 % more throughput, with warm caches and a monomorphic call site. <details> <summary>JMH output</summary> ``` Benchmark (buffers) (size) Mode Cnt Score Error Units JDK 21 before: AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 18497696.574 ± 74560.210 ops/s AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 18514434.341 ± 31467.599 ops/s JDK 21 after: AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 30904677.380 ± 69875.648 ops/s AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 30915306.886 ± 74873.233 ops/s JDK 25 before: AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 18532696.343 ± 50826.312 ops/s AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 18558232.849 ± 23243.203 ops/s JDK 25 after: AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 30851632.966 ± 296461.862 ops/s AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 30877691.180 ± 173623.904 ops/s ``` </details>
|
Port for 5.0 #17852 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Motivation: An `AdaptiveByteBuf` is a segment of a chunk's root buffer. `AdaptiveByteBuf._memoryAddress()` computes its address by calling the root buffer's `_memoryAddress()` and adding the segment's offset. On every call, the root buffer's `_memoryAddress()`: 1. checks `isAccessible()`, 2. calls `hasMemoryAddress()`, 3. calls `memoryAddress()`, which checks the reference count again in `ensureAccessible()`. These checks on the root buffer are redundant. The root buffer stays accessible while the `AdaptiveByteBuf` holds a segment of its chunk, and the root buffer's address doesn't change. A gathering write in the native transports gets the memory address of each buffer that it writes (`IovArray.add()`). In a profile of an Apache Pulsar broker writing 8 KB messages, the checks on the root buffer were 3.3 % of the I/O threads' CPU. Modification: Existing behavior that this relies on: - The address of an `AdaptiveByteBuf` is the root buffer's address plus the segment's offset in the chunk (`startIndex`). - The allocator calls `AdaptiveByteBuf.init()` when it hands out a buffer. When a buffer grows beyond its segment, the reallocation moves it to a new segment and calls `init()` again. Changes: - `Chunk` computes the root buffer's memory address once, when the chunk is created. - `AdaptiveByteBuf.init()` stores the buffer's memory address, the root buffer's address plus `startIndex`, in a new field. When the root buffer has no memory address, it stores 0, as `AbstractByteBuf._memoryAddress()` returns for buffers without one. - `AdaptiveByteBuf._memoryAddress()` returns the field, instead of calling the root buffer's `_memoryAddress()`. The public `memoryAddress()` still checks that the `AdaptiveByteBuf` itself is accessible, and, since #17820, throws `UnsupportedOperationException` when it has no memory address. - `deallocate()` resets the field, so `_memoryAddress()` still returns 0 after the buffer is deallocated. - A new test in `AdaptiveByteBufAllocatorTest`, `testDirectBufferMemoryAddressFollowsItsSegment`, allocates two direct buffers, so that the second one is likely at an offset above zero in the chunk. It compares each buffer's `memoryAddress()` with the address of the NIO buffer of the same range, which is computed from the chunk's buffer separately. It compares again after a buffer grows into another segment, and checks that `_memoryAddress()` returns 0 after release. `AbstractByteBufTest.testMemoryAddressUnsupportedWhenNoMemoryAddress` (#17820) covers the heap buffers. - A new benchmark, `AdaptiveByteBufMemoryAddressBenchmark`, gets the memory addresses of 16 direct buffers, as a gathering write does. Result: Each `memoryAddress()` call checks a reference count once, the `AdaptiveByteBuf`'s own, instead of three times. The root buffer's address is computed once per chunk; allocating and releasing a buffer each add one field write. `AdaptiveByteBufMemoryAddressBenchmark` with 16 buffers of 8 KB, on an Intel i9-9980HK at a fixed 2.4 GHz, with Unsafe enabled (`-Dio.netty.noUnsafe=false`), 2 alternating runs per side. Before is 4.2 at 60d9baf with only the benchmark added: | | Before | After | |---|---:|---:| | JDK 21, run 1, ops/s | 18,497,697 ± 74,560 | 30,904,677 ± 69,876 | | JDK 21, run 2, ops/s | 18,514,434 ± 31,468 | 30,915,307 ± 74,873 | | JDK 25, run 1, ops/s | 18,532,696 ± 50,826 | 30,851,633 ± 296,462 | | JDK 25, run 2, ops/s | 18,558,233 ± 23,243 | 30,877,691 ± 173,624 | That's 3.38 ns per buffer before and 2.02 ns after on both JDKs: 67 % more throughput, with warm caches and a monomorphic call site. <details> <summary>JMH output</summary> ``` Benchmark (buffers) (size) Mode Cnt Score Error Units JDK 21 before: AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 18497696.574 ± 74560.210 ops/s AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 18514434.341 ± 31467.599 ops/s JDK 21 after: AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 30904677.380 ± 69875.648 ops/s AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 30915306.886 ± 74873.233 ops/s JDK 25 before: AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 18532696.343 ± 50826.312 ops/s AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 18558232.849 ± 23243.203 ops/s JDK 25 after: AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 30851632.966 ± 296461.862 ops/s AdaptiveByteBufMemoryAddressBenchmark.memoryAddress 16 8192 thrpt 20 30877691.180 ± 173623.904 ops/s ``` </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.