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 — Introduce size-classes for the adaptive allocator (#15399) by chrisvest · Pull Request #15414 · netty/netty Conversation
Member
Motivation: Modification:
Result: Fixes #15311
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsMotivation: The histograms in the adaptive allocator are best at looking for a single, good chunk size. However, in practice, we have a lot of sizes that follow various patterns. The histograms then tend to bias toward large sizes, which is wasteful in space. It also turns out to be wasteful in time, in the sense that a small allocation can stick around for a long time, and prevent a large chunk from being reused when the rest of it has been deallocated. As such, the histogram-based magazines works best for fewer, larger allocations, and we need something else for small and plentiful allocations. Modification: - Refactor the internals to detangle the magazines from the histogram implementation. - Introduce abstractions so the choice of recording and choosing buffer sizes, and the chunk allocation, is moved out of the magazines and into a chunk controller. - Move the allocation statistics into a histogram chunk controller, and introduce a separate size-classed chunk controller. - The size-classes are chosen from the observation that 1) most allocations aim to be power-of-2 sized, but 2) a lot of such allocations end up in framing protocols that add headers, checksums, etc. so the actual desired size is a power-of-2-plus-a-bit. - Introduce magazine groups. The group holds the magazine array and deals with contention by expanding the array. We will have a group per size class, along size a histogram-based group for large allocations. - Introduce a size-classed chunk which allocates out of a free-list instead of pointer-bump allocation. This greatly enhances the space reuse of such chunks and allows them to absorb a lot of the memory fragmentation that small allocations are prone to. - Add an MpscIntQueue class that we use for said free-list. This is based on the implementations found in JCTools. - Fix a concurrent-access bug in AdaptiveByteBuf.getBytes methods, where the internalNioBuffer had position and limit concurrently modified. Result: This should greatly reduce memory usage caused by chunks that hang around too long due to fragmentation. Fixes netty#15311
|
There was an error while loading. Please reload this page.
|
Another thought on my FIFO suggestion; IDK now (I haven't tried really) how the size class Chunk remaining capacity is handled re the free list: if the free list has any segment available I expect the chunk to report that got some capacity left. Am I right? I couldn't find, reading the PR any evidence of it e.g. allocated bytes to decrease on free lo ist successful offer |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
| } | ||
|
|
||
| private static boolean shouldReleaseSuboptimalChunkSize(int givenSize, int preferredSize) { |
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.
here we could have something like
@Override public int remainingCapacity() { int remainingCapacity = super.remainingCapacity(); if (remainingCapacity > 0) { return remainingCapacity; } if (freeList.isEmpty()) { return 0; } int updatedRemainingCapacity = freeList.size() * segmentSize; assert updatedRemainingCapacity > 0; // update allocatedBytes based on what's available in the free list allocatedBytes = capacity() - updatedRemainingCapacity; return updatedRemainingCapacity; }
which can help to NOT remove the current chunk whilst there is still some available segment.
But since there's an already in place logic which rotate current Chunk once there's a single segment left, it can be changed into:
@Override public int remainingCapacity() { int remainingCapacity = super.remainingCapacity(); if (remainingCapacity > segmentSize) { return remainingCapacity; } int updatedRemainingCapacity = freeList.size() * segmentSize; if (updatedRemainingCapacity == remainingCapacity) { return remainingCapacity; } // update allocatedBytes based on what's available in the free list allocatedBytes = capacity() - updatedRemainingCapacity; return updatedRemainingCapacity; }
without changing the existing logic (which has served well for non size classes chunks).
This will update the capacity snapshot once we approach 1 or no chunks: if there's really a single chunk available, it will cause a rotation, but it's fine.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| int startingCapacity = getStartingCapacity(size, maxCapacity); | ||
| private boolean allocate(int size, int maxCapacity, AdaptiveByteBuf buf, boolean reallocate) { | ||
| int startingCapacity = chunkController.computeBufferCapacity(size, maxCapacity, reallocate); | ||
| Chunk curr = current; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
If we can now reuse more of the existing chunk (for size class chunks) it's not right that we expect here to rotate current (if (remainingCapacity == startingCapacity)) for the last allocation
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
On #15414 (comment) I've better explained what could improve this case:
This is similar to the problem we had before, and size classes has indeed distributed the probability to happen, decoupling buffers with different types from each others, but it can still happen. The second solution in #15414 (comment) it's taking care of using the right logic which won't modify the existing rotation logic. @dreamlike-ocean can you try a patched version which add the mentioned logic? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
FYI #15416 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…#15416) Motivation: SizeClassedChunk is not using the freeList size to update the remaining capacity, prompting unnecessary chunk allocations Modification: Once approaching one or less segment left, update the remaining capacity, to save allocating a new chunk Result: Reduced fragmentation and allocation pressure for buffers which release later
|
I merged #15416 into 4.2 and cherry-picked it here. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Yep sorry, I have been lazy and not used the issues to track my concerns, which, as you rightly pointed, are not related this specific pr 🙏 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
### What changes were proposed in this pull request? This PR aims to Upgrade Netty to 4.1.123.Final. ### Why are the changes needed? To bring the latest bug fixes and also improvement. - https://netty.io/news/2025/07/16/4-1-123-Final.html - netty/netty#15428 - netty/netty#15425 - netty/netty#15414 - netty/netty#15422 - netty/netty#15410 - netty/netty#15393 - netty/netty#15376 - netty/netty#15368 - netty/netty#15330 - netty/netty#15346 - netty/netty#15357 - netty/netty#15355 - netty/netty#15308 ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Pass the CIs. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #51693 from dongjoon-hyun/SPARK-52983. Authored-by: Dongjoon Hyun <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
This PR aims to Upgrade Netty to 4.1.123.Final. To bring the latest bug fixes and also improvement. - https://netty.io/news/2025/07/16/4-1-123-Final.html - netty/netty#15428 - netty/netty#15425 - netty/netty#15414 - netty/netty#15422 - netty/netty#15410 - netty/netty#15393 - netty/netty#15376 - netty/netty#15368 - netty/netty#15330 - netty/netty#15346 - netty/netty#15357 - netty/netty#15355 - netty/netty#15308 No. Pass the CIs. No. Closes apache#51693 from dongjoon-hyun/SPARK-52983. Authored-by: Dongjoon Hyun <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
This PR aims to Upgrade Netty to 4.1.123.Final. To bring the latest bug fixes and also improvement. - https://netty.io/news/2025/07/16/4-1-123-Final.html - netty/netty#15428 - netty/netty#15425 - netty/netty#15414 - netty/netty#15422 - netty/netty#15410 - netty/netty#15393 - netty/netty#15376 - netty/netty#15368 - netty/netty#15330 - netty/netty#15346 - netty/netty#15357 - netty/netty#15355 - netty/netty#15308 No. Pass the CIs. No. Closes apache#51693 from dongjoon-hyun/SPARK-52983. Authored-by: Dongjoon Hyun <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
This PR aims to Upgrade Netty to 4.1.123.Final. To bring the latest bug fixes and also improvement. - https://netty.io/news/2025/07/16/4-1-123-Final.html - netty/netty#15428 - netty/netty#15425 - netty/netty#15414 - netty/netty#15422 - netty/netty#15410 - netty/netty#15393 - netty/netty#15376 - netty/netty#15368 - netty/netty#15330 - netty/netty#15346 - netty/netty#15357 - netty/netty#15355 - netty/netty#15308 No. Pass the CIs. No. Closes apache#51693 from dongjoon-hyun/SPARK-52983. Authored-by: Dongjoon Hyun <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
This PR aims to Upgrade Netty to 4.1.123.Final. To bring the latest bug fixes and also improvement. - https://netty.io/news/2025/07/16/4-1-123-Final.html - netty/netty#15428 - netty/netty#15425 - netty/netty#15414 - netty/netty#15422 - netty/netty#15410 - netty/netty#15393 - netty/netty#15376 - netty/netty#15368 - netty/netty#15330 - netty/netty#15346 - netty/netty#15357 - netty/netty#15355 - netty/netty#15308 No. Pass the CIs. No. Closes apache#51693 from dongjoon-hyun/SPARK-52983. Authored-by: Dongjoon Hyun <[email protected]> Signed-off-by: Dongjoon Hyun <[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.