GeezerVison is showing github.com — Optimize capacity bumping for adaptive ByteBufs by chrisvest · Pull Request #15080 · netty/netty
Conversation
chrisvest
commented
Apr 24, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Motivation:
It is quite common in downstream code to not specify a buffer size when allocating.
People instead rely on the ByteBuf ability to increase its size automatically.
The ByteBufs allocated by the adaptive allocator also support this, but has thus far required a round-trip through the allocator every time.
In contrast, the pooled allocator is reserving the chunk run-size when allocating buffers, and very cheaply allow smaller buffers to bump up to the run-size limit.
This is an important performance optimisation in practice.
We can do something similar in the adaptive allocator, because we keep statistics about the buffer sizes that we allocate.
Modification:
The adaptive allocator now reserves space equal to the 99-percentile buffer size, when allocating buffers, and allow its ByteBufs to cheaply bump their capacities up to this limit.
We already compute this percentile during histogram rotation, so it is simply a matter of storing this value in the magazine, and then using it when reserving space out of chunks.
The collection of buffer size statistics is now also delayed and moved to when the ByteBuf instances are reused. This way, we collect statistics about the final buffer size, rather than the requested sizes or the sizes of capacity bumps.
This prevents oscillations that would otherwise be caused by undersized buffers producing a lot of statistics as they resize to their final size, and then no statistics being produced for the final buffer sizes if they are correctly predicted at allocation time.
Result:
Calls like ByteBuf.ensureWritable() will now on average be much cheaper when using the adaptive allocator.
Based on #15062
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
franz1981 and onebeastchris reacted with heart emoji
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to re-read it better - but since magazine(s) allocations are bump pointer allocations - I was hoping there was the chance of a super fast path where enlarging doesn't cause at all any bytes copy - assuming the current buffer hasn't changed
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Been doing some benchmarking. One thing that shows up is that the histograms have limited fidelity, and the smallest buffer size they can record is 8 KiB. This means this change ends up always allocating 8 KiB for buffers smaller than that, and this ends up using a large amount of memory when the default size is 256 bytes.
The pooled allocator has more granularity in its size classes, and is always able to exact-size the buffers in the ByteBufAllocatorBenchmark.
I'll try to see if increasing histogram fidelity helps.
I have to re-read it better - but since magazine(s) allocations are bump pointer allocations - I was hoping there was the chance of a super fast path where enlarging doesn't cause at all any bytes copy - assuming the current buffer hasn't changed
I worry that this will be too rare to be profitable. Will need a more realistic workload to measure and make sure, though.
|
franz1981 reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
franz1981
commented
Apr 25, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
I am not sure is that uncommon; @vietj can confirm: basically there are encoders (of reactive postgres) which doesn't estimate the requirements for the encoded capacity upfront, allocating a single buffer without any capacity set.
Later on, during encoding (spanning several classes which perform it on the event loop), the buffer get enlarged few times (without any further ByteBuf allocation) and, once complete, written to the outer layers (likely SSL or via socket).
In such cases, there is not real need of any copies, assuming the super fast path I have explained, to be implemented.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Current numbers from the ByteBufAllocateAndGrowBenchmark on my M1 Pro
Benchmark (size) Mode Cnt Score Error Units
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 00256 thrpt 20 22072985.500 ± 540696.942 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 00256 thrpt 20 254.901 ± 0.292 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 00256 thrpt 20 254.901 ± 0.292 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 01024 thrpt 20 22757524.412 ± 675428.307 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 01024 thrpt 20 986.720 ± 1.049 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 01024 thrpt 20 594.644 ± 0.632 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 04096 thrpt 20 15590426.293 ± 783115.792 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 04096 thrpt 20 3962.101 ± 5.240 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 04096 thrpt 20 1581.438 ± 2.095 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 16384 thrpt 20 6723092.103 ± 248097.254 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 16384 thrpt 20 16020.518 ± 22.969 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 16384 thrpt 20 4625.471 ± 6.625 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 65536 thrpt 20 3453546.118 ± 122597.658 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 65536 thrpt 20 64256.402 ± 84.232 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 65536 thrpt 20 14474.448 ± 18.976 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 00256 thrpt 20 16073435.780 ± 229309.257 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 00256 thrpt 20 254.838 ± 0.251 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 00256 thrpt 20 254.838 ± 0.251 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 01024 thrpt 20 5727647.502 ± 421022.762 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 01024 thrpt 20 595.003 ± 0.823 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 01024 thrpt 20 595.003 ± 0.823 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 04096 thrpt 20 2889475.147 ± 166005.486 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 04096 thrpt 20 1576.353 ± 4.355 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 04096 thrpt 20 1576.353 ± 4.355 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 16384 thrpt 20 1292365.419 ± 8228.473 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 16384 thrpt 20 4612.601 ± 13.999 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 16384 thrpt 20 4612.601 ± 13.999 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 65536 thrpt 20 533048.077 ± 2538.240 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 65536 thrpt 20 14452.720 ± 49.240 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 65536 thrpt 20 14452.720 ± 49.240 ops/s
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Marked this as ready for review, but the so-called "super-fast path" is still an interesting idea. Could perhaps be a separate PR.
|
franz1981 reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
chrisvest
commented
Apr 26, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
It just occurred to me that this change can cause oscillations.
If the allocator correctly predicts the final size of the buffer, it won't get to record that final size in its statistics. It only gets feedback if it predicts too low. So it will predict too low and get feedback about this until the 99-percentile get's high enough, then it predicts correctly for a while, but the buffer size statistics shifts down towards the initial sizes, and eventually the 99-percentile will drop and the allocator will start predicting too low again.
So this needs more work. One idea I've been pondering is to record the allocation sizes on de-allocation instead. That would give the correct final size, but… de-allocation happens outside the magazine lock. So either we have to retake it, or somehow make the histogram concurrent.
|
normanmaurer reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@chrisvest let me know once this is ready for review
|
chrisvest reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
chrisvest
commented
May 7, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
@franz1981 @normanmaurer This is ready for review. I delayed the collection of buffer size statistics until the time where the pooled ByteBuf instances are reused. This slows down the initial warm up a bit, but in turn it means that we only collect statistics about the final size of the buffers, not the initial requested allocations or any capacity bumps.
The benchmarks now look like this, and indicate that this is profitable for all but the smallest buffer sizes:
Outdated benchmark results
Before:
Benchmark (size) Mode Cnt Score Error Units
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 00256 thrpt 20 20971200.008 ± 270576.623 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 00256 thrpt 20 254.972 ± 0.381 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 00256 thrpt 20 254.972 ± 0.381 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 01024 thrpt 20 7990032.019 ± 269886.195 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 01024 thrpt 20 595.055 ± 0.896 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 01024 thrpt 20 595.055 ± 0.896 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 04096 thrpt 20 3183902.971 ± 30325.489 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 04096 thrpt 20 1579.819 ± 1.505 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 04096 thrpt 20 1579.819 ± 1.505 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 16384 thrpt 20 680506.519 ± 6548.563 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 16384 thrpt 20 4626.348 ± 8.011 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 16384 thrpt 20 4626.348 ± 8.011 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 65536 thrpt 20 236048.099 ± 2162.341 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 65536 thrpt 20 14469.252 ± 15.437 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 65536 thrpt 20 14469.252 ± 15.437 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 00256 thrpt 20 16120468.646 ± 186072.050 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 00256 thrpt 20 254.994 ± 0.345 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 00256 thrpt 20 254.994 ± 0.345 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 01024 thrpt 20 5174639.204 ± 22944.132 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 01024 thrpt 20 594.757 ± 0.665 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 01024 thrpt 20 594.757 ± 0.665 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 04096 thrpt 20 2726448.069 ± 69778.054 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 04096 thrpt 20 1581.071 ± 2.372 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 04096 thrpt 20 1581.071 ± 2.372 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 16384 thrpt 20 1098746.939 ± 29226.704 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 16384 thrpt 20 4624.096 ± 5.646 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 16384 thrpt 20 4624.096 ± 5.646 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 65536 thrpt 20 492292.545 ± 8670.280 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 65536 thrpt 20 14472.056 ± 19.128 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 65536 thrpt 20 14472.056 ± 19.128 ops/s
Benchmark (size) Mode Cnt Score Error Units
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 00064 thrpt 20 201842061.879 ± 7408330.093 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 00256 thrpt 20 202515303.168 ± 7891442.985 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 01024 thrpt 20 205190031.880 ± 7193097.865 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 04096 thrpt 20 208397591.000 ± 16463211.669 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 00064 thrpt 20 103259175.327 ± 2139519.286 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 00256 thrpt 20 153544433.191 ± 3340157.177 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 01024 thrpt 20 100893062.481 ± 5291814.744 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 04096 thrpt 20 122298801.791 ± 16112824.579 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseUnpooled 00064 thrpt 5 22436.659 ± 22835.194 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseUnpooled 00256 thrpt 20 1426013.400 ± 105589.900 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseUnpooled 01024 thrpt 20 1178116.811 ± 57092.008 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseUnpooled 04096 thrpt 20 1005929.958 ± 24847.333 ops/s
This PR:
Benchmark (size) Mode Cnt Score Error Units
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 00256 thrpt 20 20343962.801 ± 173329.475 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 00256 thrpt 20 254.879 ± 0.254 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 00256 thrpt 20 254.879 ± 0.254 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 01024 thrpt 20 18976631.103 ± 268202.240 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 01024 thrpt 20 1019.946 ± 1.314 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 01024 thrpt 20 594.969 ± 0.767 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 04096 thrpt 20 15143659.856 ± 288958.291 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 04096 thrpt 20 4078.384 ± 4.366 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 04096 thrpt 20 1580.376 ± 1.692 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 16384 thrpt 20 6061695.690 ± 92124.984 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 16384 thrpt 20 16311.035 ± 19.535 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 16384 thrpt 20 4623.908 ± 5.537 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 65536 thrpt 20 4674994.849 ± 116397.078 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 65536 thrpt 20 65229.797 ± 112.870 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 65536 thrpt 20 14467.283 ± 25.029 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 00256 thrpt 20 16230847.487 ± 109882.531 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 00256 thrpt 20 254.869 ± 0.318 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 00256 thrpt 20 254.869 ± 0.318 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 01024 thrpt 20 5247087.457 ± 62515.600 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 01024 thrpt 20 595.050 ± 0.808 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 01024 thrpt 20 595.050 ± 0.808 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 04096 thrpt 20 2894436.400 ± 26465.850 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 04096 thrpt 20 1580.243 ± 1.996 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 04096 thrpt 20 1580.243 ± 1.996 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 16384 thrpt 20 1124120.804 ± 20695.162 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 16384 thrpt 20 4623.633 ± 4.167 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 16384 thrpt 20 4623.633 ± 4.167 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 65536 thrpt 20 494726.460 ± 7822.986 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 65536 thrpt 20 14470.926 ± 14.555 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 65536 thrpt 20 14470.926 ± 14.555 ops/s
Benchmark (size) Mode Cnt Score Error Units
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 00064 thrpt 20 248339416.246 ± 14125791.523 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 00256 thrpt 20 260500181.446 ± 2436690.826 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 01024 thrpt 20 227955732.717 ± 9079507.372 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 04096 thrpt 20 257718939.639 ± 1832511.844 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 00064 thrpt 20 156082396.646 ± 1431961.114 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 00256 thrpt 20 128717229.421 ± 20244674.070 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 01024 thrpt 20 123697877.459 ± 17572853.583 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 04096 thrpt 20 127395986.190 ± 27092444.618 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseUnpooled 00064 thrpt 6 16583.195 ± 22116.845 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseUnpooled 00256 thrpt 20 1391611.046 ± 133657.735 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseUnpooled 01024 thrpt 20 1193813.142 ± 59223.684 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseUnpooled 04096 thrpt 20 1027168.954 ± 23529.722 ops/s
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Putting it back to draft until the build failures are fixed.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Motivation:
It is quite common in downstream code to not specify a buffer size when allocating.
People instead rely on the ByteBuf ability to increase its size automatically.
The ByteBufs allocated by the adaptive allocator also support this, but has thus far required a round-trip through the allocator every time.
In contrast, the pooled allocator is reserving the chunk run-size when allocating buffers, and very cheaply allow smaller buffers to bump up to the run-size limit.
This is an important performance optimisation in practice.
We can do something similar in the adaptive allocator, because we keep statistics about the buffer sizes that we allocate.
Modification:
The adaptive allocator now reserves space equal to the 99-percentile buffer size, when allocating buffers, and allow its ByteBufs to cheaply bump their capacities up to this limit.
We already compute this percentile during histogram rotation, so it is simply a matter of storing this value in the magazine, and then using it when reserving space out of chunks.
Result:
Calls like ByteBuf.ensureWritable() will now on average be much cheaper when using the adaptive allocator.
This allows us to get much better estimates for how much memory to reserve in buffers
At these points we know from earlier if-clauses that 'remainingCapacity' must be less-than-or-equal to 'startingCapacity', so there's no point to taking the minimum of the two; it's always going to be 'remainingCapacity'.
They stress fragmentation too hard
This fixes the chunk reuse test
|
@normanmaurer I think this is ready for review. I'll rerun the benchmarks and post updated results.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Updated benchmark numbers:
Before:
Benchmark (size) Mode Cnt Score Error Units
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 00256 thrpt 20 20971200.008 ± 270576.623 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 00256 thrpt 20 254.972 ± 0.381 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 00256 thrpt 20 254.972 ± 0.381 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 01024 thrpt 20 7990032.019 ± 269886.195 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 01024 thrpt 20 595.055 ± 0.896 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 01024 thrpt 20 595.055 ± 0.896 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 04096 thrpt 20 3183902.971 ± 30325.489 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 04096 thrpt 20 1579.819 ± 1.505 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 04096 thrpt 20 1579.819 ± 1.505 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 16384 thrpt 20 680506.519 ± 6548.563 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 16384 thrpt 20 4626.348 ± 8.011 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 16384 thrpt 20 4626.348 ± 8.011 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 65536 thrpt 20 236048.099 ± 2162.341 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 65536 thrpt 20 14469.252 ± 15.437 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 65536 thrpt 20 14469.252 ± 15.437 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 00256 thrpt 20 16120468.646 ± 186072.050 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 00256 thrpt 20 254.994 ± 0.345 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 00256 thrpt 20 254.994 ± 0.345 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 01024 thrpt 20 5174639.204 ± 22944.132 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 01024 thrpt 20 594.757 ± 0.665 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 01024 thrpt 20 594.757 ± 0.665 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 04096 thrpt 20 2726448.069 ± 69778.054 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 04096 thrpt 20 1581.071 ± 2.372 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 04096 thrpt 20 1581.071 ± 2.372 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 16384 thrpt 20 1098746.939 ± 29226.704 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 16384 thrpt 20 4624.096 ± 5.646 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 16384 thrpt 20 4624.096 ± 5.646 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 65536 thrpt 20 492292.545 ± 8670.280 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 65536 thrpt 20 14472.056 ± 19.128 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 65536 thrpt 20 14472.056 ± 19.128 ops/s
Benchmark (size) Mode Cnt Score Error Units
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 00064 thrpt 20 201842061.879 ± 7408330.093 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 00256 thrpt 20 202515303.168 ± 7891442.985 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 01024 thrpt 20 205190031.880 ± 7193097.865 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 04096 thrpt 20 208397591.000 ± 16463211.669 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 00064 thrpt 20 103259175.327 ± 2139519.286 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 00256 thrpt 20 153544433.191 ± 3340157.177 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 01024 thrpt 20 100893062.481 ± 5291814.744 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 04096 thrpt 20 122298801.791 ± 16112824.579 ops/s
This PR:
Benchmark (size) Mode Cnt Score Error Units
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 00256 thrpt 20 20204883.919 ± 155665.077 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 00256 thrpt 20 254.897 ± 0.333 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 00256 thrpt 20 254.897 ± 0.333 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 01024 thrpt 20 18918858.462 ± 179733.488 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 01024 thrpt 20 1019.208 ± 0.839 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 01024 thrpt 20 594.538 ± 0.490 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 04096 thrpt 20 15523727.078 ± 405531.595 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 04096 thrpt 20 4079.680 ± 6.765 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 04096 thrpt 20 1580.878 ± 2.621 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 16384 thrpt 20 9243164.630 ± 160261.870 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 16384 thrpt 20 16301.324 ± 0.291 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 16384 thrpt 20 4621.146 ± 0.082 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree 65536 thrpt 20 559539.575 ± 3968.185 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgFastCap 65536 thrpt 20 26566.302 ± 29.449 ops/s
ByteBufAllocateAndGrowBenchmark.adaptiveDirectAllocAndFree:avgSize 65536 thrpt 20 14463.173 ± 15.995 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 00256 thrpt 20 16372139.698 ± 158659.064 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 00256 thrpt 20 254.923 ± 0.329 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 00256 thrpt 20 254.923 ± 0.329 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 01024 thrpt 20 5220700.826 ± 70815.218 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 01024 thrpt 20 594.641 ± 0.697 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 01024 thrpt 20 594.641 ± 0.697 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 04096 thrpt 20 2660145.234 ± 298486.949 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 04096 thrpt 20 1576.687 ± 3.558 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 04096 thrpt 20 1576.687 ± 3.558 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 16384 thrpt 20 1071112.369 ± 20461.369 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 16384 thrpt 20 4605.694 ± 12.151 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 16384 thrpt 20 4605.694 ± 12.151 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree 65536 thrpt 20 510891.405 ± 9991.924 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgFastCap 65536 thrpt 20 14411.772 ± 35.812 ops/s
ByteBufAllocateAndGrowBenchmark.pooledDirectAllocAndFree:avgSize 65536 thrpt 20 14411.772 ± 35.812 ops/s
Benchmark (size) Mode Cnt Score Error Units
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 00064 thrpt 20 262596258.342 ± 2512306.008 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 00256 thrpt 20 261749014.122 ± 4449862.992 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 01024 thrpt 20 261634949.282 ± 1754384.536 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleaseAdaptive 04096 thrpt 20 250364667.885 ± 15435882.658 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 00064 thrpt 20 130504611.004 ± 23449340.154 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 00256 thrpt 20 131186224.153 ± 21852011.727 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 01024 thrpt 20 132995228.621 ± 22680742.143 ops/s
ByteBufAllocatorConcurrentBenchmark.allocateReleasePooled 04096 thrpt 20 131811383.896 ± 22952993.708 ops/s
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
normanmaurer
pushed a commit
that referenced
this pull request
Jun 17, 2025
Motivation:
It is quite common in downstream code to not specify a buffer size when
allocating.
People instead rely on the ByteBuf ability to increase its size
automatically.
The ByteBufs allocated by the adaptive allocator also support this, but
has thus far required a round-trip through the allocator every time.
In contrast, the pooled allocator is reserving the chunk run-size when
allocating buffers, and very cheaply allow smaller buffers to bump up to
the run-size limit.
This is an important performance optimisation in practice.
We can do something similar in the adaptive allocator, because we keep
statistics about the buffer sizes that we allocate.
Modification:
The adaptive allocator now reserves space equal to the 99-percentile
buffer size (though with some reasonable heuristic limits for smaller buffers),
when allocating buffers, and allow its ByteBufs to cheaply
bump their capacities up to this limit.
We already compute this percentile during histogram rotation, so it is
simply a matter of storing this value in the magazine, and then using it
when reserving space out of chunks.
The collection of buffer size statistics is now also delayed and moved
to when the `ByteBuf` instances are reused. This way, we collect
statistics about the _final_ buffer size, rather than the requested
sizes or the sizes of capacity bumps.
This prevents oscillations that would otherwise be caused by undersized
buffers producing a lot of statistics as they resize to their final
size, and then no statistics being produced for the final buffer sizes
if they are correctly predicted at allocation time.
Result:
Calls like `ByteBuf.ensureWritable()` will now on average be much
cheaper when using the adaptive allocator.
Based on #15062
normanmaurer
added a commit
that referenced
this pull request
Jun 17, 2025
Motivation:
It is quite common in downstream code to not specify a buffer size when
allocating.
People instead rely on the ByteBuf ability to increase its size
automatically.
The ByteBufs allocated by the adaptive allocator also support this, but
has thus far required a round-trip through the allocator every time. In
contrast, the pooled allocator is reserving the chunk run-size when
allocating buffers, and very cheaply allow smaller buffers to bump up to
the run-size limit.
This is an important performance optimisation in practice. We can do
something similar in the adaptive allocator, because we keep statistics
about the buffer sizes that we allocate.
Modification:
The adaptive allocator now reserves space equal to the 99-percentile
buffer size (though with some reasonable heuristic limits for smaller
buffers), when allocating buffers, and allow its ByteBufs to cheaply
bump their capacities up to this limit.
We already compute this percentile during histogram rotation, so it is
simply a matter of storing this value in the magazine, and then using it
when reserving space out of chunks.
The collection of buffer size statistics is now also delayed and moved
to when the `ByteBuf` instances are reused. This way, we collect
statistics about the _final_ buffer size, rather than the requested
sizes or the sizes of capacity bumps.
This prevents oscillations that would otherwise be caused by undersized
buffers producing a lot of statistics as they resize to their final
size, and then no statistics being produced for the final buffer sizes
if they are correctly predicted at allocation time.
Result:
Calls like `ByteBuf.ensureWritable()` will now on average be much
cheaper when using the adaptive allocator.
Based on #15062
Co-authored-by: Chris Vest <[email protected]>
This was referenced Sep 14, 2026
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|