GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 4.2 per magazine cache by franz1981 · Pull Request #17047 · netty/netty

Conversation

@franz1981

@franz1981 franz1981 commented Jul 3, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Fixes #17028

This is introducing few changes:

  1. different expansion mechanism i.e. magazines are now allocated lazily as the lock acquisition failure is considered a concurrency hint, mostly
  2. a recycled buffer from a magazine won't float around to other magazines after attempting to allocate from shared magazines
  3. fallback one-off chunks doesn't really need a magazine, but just a recycler: try first to pick the "local" one, than use a fallback recycler
  4. uses mpmc xadd unbounded queue on the shared cache as the capacity limits doesn't buy us nothing if not churning: the thread local path relies on a an evinction/retirement protocol and so the shared cache as well
  5. make shared cache per-magazine to reduce contention among the mpmc queue

@laosijikaichele this is not solving the buddy cache issue - which BTW, could benefit as well by the 5th point (I know it would, with the benchmark you provided), but it won't be enough to fix all the gaps.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

laosijikaichele reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@franz1981 franz1981 mentioned this pull request Jul 3, 2026
AdaptivePoolingAllocator: Performance gap in latency/RSS in several benchmark cases compared with other allocators #17028
Open
@franz1981

franz1981 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

FYI @laosijikaichele since I'm using CLQ if Unsafe is NOT available, you have to pass --sun-misc-unsafe-memory-access=allow or whatever required, so we use my xadd queue.
So far we have some bimodal JIT compilation issue which makes few forks to have some unstable numbers (swinging from 1 -> 1.5X diff in latency), and the buddy allocation issue which create RSS churn (and heap allocations too in case of heap buffers).
The mimalloc one is very remarkable in the release path too 👍

laosijikaichele reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

Wow I have experimented with removing the allocation on magazine without lock and...it improves concurrent behaviour by removing contention over the mpmc array queue used by the magazine recycler.
It is extremely visible with 32 cores but not negligible w 8 too.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

FYI @laosijikaichele since I'm using CLQ if Unsafe is NOT available, you have to pass --sun-misc-unsafe-memory-access=allow or whatever required, so we use my xadd queue. So far we have some bimodal JIT compilation issue which makes few forks to have some unstable numbers (swinging from 1 -> 1.5X diff in latency), and the buddy allocation issue which create RSS churn (and heap allocations too in case of heap buffers). The mimalloc one is very remarkable in the release path too 👍

  • Please check the benchmark results with this PR:
    https://neoionet.github.io/netty-allocator-benchmark-report/franz/benchmark.html
  • The specification are the same, except the adaptive code, which is based on this PR:
    https://neoionet.github.io/netty-allocator-benchmark-report/latest/specification
  • The raw results data:
    https://github.com/neoionet/netty-allocator-benchmark-report/tree/main/franz/raw

you have to pass `--sun-misc-unsafe-memory-access=allow

Since I use Java 21, so it should not matter.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Wow I have experimented with removing the allocation on magazine without lock and...it improves concurrent behaviour by removing contention over the mpmc array queue used by the magazine recycler. It is extremely visible with 32 cores but not negligible w 8 too.

To disable tryAllocateWithoutLock on this PR, I replaced the following line with return null:
https://github.com/franz1981/netty/blob/92a9db9ff889fe364d4b7ece1e11d60d1ea16ea8/buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java#L1287.

  • The benchmark results:
    https://neoionet.github.io/netty-allocator-benchmark-report/franz/benchmark-disable-withoutlock.html
  • The raw results data:
    https://github.com/neoionet/netty-allocator-benchmark-report/tree/main/franz/raw-disable-withoutlock
franz1981 reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 6, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@laosijikaichele thanks!
I have added two things (not on the branch, still unpushed):

  • eagerly init the first magazine (now is lazily allocated!)
  • disable allocate without lock as you have done
  • allocate with fallback is using its own shared recycler
  • since none can allocate magazines outside of the magazine lock, I have turned it into a mpsc instead of mpmc.
    Now, the interesting thing is...and if we have no more the shared chunk cache to be shared, maybe we can just use the same single threaded variant I made for the event loop, which should improve quite a lot performance for platform threads.
    Wdyt?
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 6, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

And more: magazine owned buffer deallocations are always paying the multi producer cost of recyclers, but...if we remember the magazine where the chunk is coming from, we could attempt locking the magazine and just release into a local array instead, saving the atomic operation to compete with other platform threads which has allocated from the same magazine and are releasing on the same recycler.
If the attempt to lock the magazine for release would fail, we can just fallback to the mpsc release.
Currently there is no recycler with this semantic, afaik.

laosijikaichele reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

And more: magazine owned buffer deallocations are always paying the multi producer cost of recyclers, but...if we remember the magazine where the chunk is coming from, we could attempt locking the magazine and just release into a local array instead, saving the atomic operation to compete with other platform threads which has allocated from the same magazine and are releasing on the same recycler. If the attempt to lock the magazine for release would fail, we can just fallback to the mpsc release. Currently there is no recycler with this semantic, afaik.

I think this should work, mimalloc uses similar logic on the release path: https://github.com/neoionet/netty-allocator/blob/cb6058c443fa8fa6713f3a76d9d2663305be1aea/mimalloc/src/main/java/io/github/neoionet/netty/mimalloc/MiMallocByteBufAllocator.java#L1956-L1994

franz1981 reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

I think this should work, mimalloc uses similar logic on the release path

I think it's a smart move as well (well done to have thought of it!) because it is a good way to exploit what is a common pattern and still get the lock-free benefits of releases, if not possible
That said, the core optimization so far IMHO would be to remove the shared cache thanks to removing the allocate without lock: allocate without lock was good as we were not good at using the striped mechanism, but it is basically bypassing the "concurrency" feedback signal to the striped array, which is not good - as it force using lock-free instead of plain thread local (like) constructs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981
franz1981 force-pushed the 4.2_per_magazine_cache branch from 92a9db9 to c49092e Compare July 10, 2026 08:16
@franz1981

franz1981 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

I've changed something so @laosijikaichele can you give it a shot? It should help both latencies and RSS

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

franz1981 added 3 commits July 10, 2026 10:48
@franz1981
SharedMagazineRef: separate lock from Magazine with lazy init
1ac6547
Introduce SharedMagazineRef to decouple StampedLock from Magazine lifecycle.
Magazine fields (chunkController, chunkCache, recycler) are always fully
initialized - no null checks on the hot path. Magazines are lazily allocated
via volatile reference with intent-revealing tryAllocate/free methods.
@franz1981
Simplify allocation paths, MPSC per-magazine recycler, MPMC fallback
861812e
Remove allocateWithoutLock path - all magazine access goes through
tryAllocate under StampedLock. Replace nextInLine volatile + AtomicRef
with plain field (all access under lock). Eagerly init magazine 0.

Decouple fallback allocation from Magazine - new Chunk constructor takes
AdaptivePoolingAllocator directly. Inline newFallbackBuffer to use the
shared fallbackRecycler (MPMC, truly contended) directly instead of
routing through MagazineGroup.

Add MPSC recycler variant to Recycler using MpscChunkedArrayQueue - used
for per-magazine recyclers where the single consumer is under the lock.
The shared fallbackRecycler stays MPMC (MpmcArrayQueue).
@franz1981
Per-magazine ThreadLocalSizeClassedChunkCache, remove dead SharedSize…
43399c4
…ClassedChunkCache

Each magazine now gets its own ThreadLocalSizeClassedChunkCache (ring
buffer, no atomics) instead of sharing the group-level concurrent cache.
This eliminates CAS contention on the chunk cache hot path for size-classed
allocations.

The buddy path (sizes > 16896) continues using the group-level
ConcurrentSkipListChunkCache since those allocations are infrequent and
the cache needs cross-magazine visibility.

Remove SharedSizeClassedChunkCache and MpmcUnboundedXaddArrayQueue
dependency - dead code now that all size-class caches are per-magazine.
Inline newFallbackBuffer, remove stale comments and constants.
@franz1981
franz1981 force-pushed the 4.2_per_magazine_cache branch from c49092e to 43399c4 Compare July 10, 2026 08:59
franz1981 added 2 commits July 10, 2026 13:15
@franz1981
Simplify allocateFallback method by removing unused parameters `cur…
2610f9b
…rentThread` and `originGroup`.
@franz1981
Remove unused originGroup variable in allocate method to simplify…
e91c5d2
… code.
franz1981
franz1981 commented Jul 10, 2026
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
public ChunkCache createChunkCache(boolean isThreadLocal) {
return SizeClassedChunkCache.create(isThreadLocal);
public ChunkCache createChunkCache() {
return new ThreadLocalSizeClassedChunkCache();

@franz1981 franz1981 Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

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.

The name is not great, it can be just SizeClassedChunkCache and the doc can say (it is AFAIK) it is not thread safe to access it concurrently by many threads.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
franz1981
franz1981 commented Jul 10, 2026
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
this.chunkController = group.chunkManagementStrategy.createController(group);
this.chunkCache = group.chunkManagementStrategy.hasPerMagazineCache() ?
group.chunkManagementStrategy.createChunkCache() : null;
this.recycler = isThreadLocal ? null : AdaptiveRecycler.sharedMpsc(MAGAZINE_BUFFER_QUEUE_CAPACITY);

@franz1981 franz1981 Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

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.

This could collapse to use the thread local one instead of using EVENT_LOOP_LOCAL_BUFFER_POOL too

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
franz1981
franz1981 commented Jul 10, 2026
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
// In any case we will store the Chunk as the current so it will be used again for the next allocation and
// thus be "reserved" by this Magazine for exclusive usage.
curr = NEXT_IN_LINE.getAndSet(this, null);
curr = nextInLine;

@franz1981 franz1981 Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

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.

In theory since is not used across threads there's little point to use an atomic here, correct @chrisvest ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@laosijikaichele

laosijikaichele commented Jul 10, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

I've changed something so @laosijikaichele can you give it a shot? It should help both latencies and RSS

Currently only the ARM server is available, I run the benchmark with you latest commit,

  • please check the results:
    https://neoionet.github.io/netty-allocator-benchmark-report/franz/0710/benchmark.html
  • raw data:
    https://raw.githubusercontent.com/neoionet/netty-allocator-benchmark-report/refs/heads/main/franz/0710/raw/platform-on-arm.data
    @franz1981
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

thanks @laosijikaichele

#17047 (comment)

That's on 861812e I suppose?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

thanks @laosijikaichele

#17047 (comment)

That's on 861812e I suppose?

It's based on https://github.com/franz1981/netty/tree/2610f9be5d87e53f2be721126ec96c9c1c31bb79

franz1981 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Jul 10, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
123

Since the adaptive showed high RSS when live buffers is 128, I checked the JFR file, seems the frequent Mpsc-queue resizing cause it? Maybe we can use a fixed capacity for it?
@franz1981

franz1981 reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @laosijikaichele yeah I think I should size them differently ! Good finding
The other problem is that adaptive chunks are not reused across different size classes while they could.
More, minalloc have intrusive linked lists which doesn't need to use separate array based structure (int free lists), which means it can freely share pages without being worried to recycle its free lists.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981
Use fixed-capacity MpscArrayQueue for per-magazine recycler
1bc90a3
The shared MPSC recycler (per-magazine ByteBuf wrapper pooling) was using
MpscChunkedArrayQueue which allocates new Object[] chunks as it grows.
With 32 threads and 128 live buffers per thread on API_GATEWAY pattern,
this produced 671 Object[] allocation samples (3.7 B/op) from resize.

Switch to MpscArrayQueue (fixed capacity, single pre-allocated array).
No resize, no Object[] allocations after init.

Verified with JFR: 0 Object[] allocation samples, 0.010 B/op.
@franz1981

franz1981 commented Jul 10, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@laosijikaichele addressed via 1bc90a3

thanks.
Now I'm experimenting with chunk reuse across size classes ;)

laosijikaichele reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

@laosijikaichele FYI https://github.com/franz1981/netty/tree/4.2_chunk_recycling

This is an attempt to bring from mimalloc few interesting ideas:

  1. top level striping instead of finest grain
  2. more chunk sharing across size classes
  3. reusable chunk metadata (free lists)
  4. more shared recyclers

It doesn't fix yet the buddy allocator (which means we still have some cases of churning), but the performance seems much much better - and RSS as well

Let me know wdyt so I can refine it further and eventually open a PR based on this alternative design

laosijikaichele reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981 franz1981 mentioned this pull request Jul 14, 2026
Fix AdaptiveByteBuf._setLongLE calling checked setLongLE #17098
Merged
@laosijikaichele

laosijikaichele commented Jul 15, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

I have run a quick JMH benchmark for verification, only compared ADAPTIVE and MIMALLOC, and set read/write flag to true.

  • Charts: https://neoionet.github.io/netty-allocator-benchmark-report/franz/0715/benchmark.html

  • Raw results: https://raw.githubusercontent.com/neoionet/netty-allocator-benchmark-report/refs/heads/main/franz/0715/raw/platform-on-arm.data

  • Code base:
    -- adaptive: franz1981@4bf31d6
    -- mimalloc: neoionet/netty-allocator@4638c51

@franz1981 As you said, the adaptive seems improved a lot on both performance and RSS. 👍

franz1981 reacted with rocket emoji
All reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

I have to clean up the impl @laosijikaichele and probably will make the buddy allocation NOT to be shared across striped heaps (which is what you get in mimalloc BTW...) and I will make that branch to replace this draft PR, wdyt?
I'll show what my numbers for latency looks like

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

I have to clean up the impl @laosijikaichele and probably will make the buddy allocation NOT to be shared across striped heaps (which is what you get in mimalloc BTW...) and I will make that branch to replace this draft PR, wdyt? I'll show what my numbers for latency looks like

Based on the numbers we currently get, I think it's worth trying.

franz1981 reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 15, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@laosijikaichele it seems the problem with the skip list for buddy allocations is the capacity. With 32 cores you have 64 slots and a shared one evict too aggressively buffers (which have ref cnt > 1) .
IIUC we should instead get rid of buffers which have ref cnt 0 as they are occupying the cache for no reason (and for heap buffers they maybe retain the byte[] alive too)
@chrisvest too

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 16, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

ok @laosijikaichele despite I would like to make the skip list thread local, I prefer to ask @chrisvest to fix it as I don't have enough confidence re it: I've created 1ff87f3 which add a tiny change in the buddy cache to release only chunks which are already fully refilled: I don't like this solution tbh because it is not releasing "idle" and "useless" chunks, as we do for size classes, but is better than what we have which can too aggressively prune actively used chunks (which have live buffers which craved some memory out of them).
Any suggestion is welcome but if the numbers of this commit seems better than your last test, I will send a PR and @chrisvest will review it (you too, as you are an expert on this topic!).

my numbers with 32 cores and 64 Gb memory and platform threads with heap buffers

  SOCKET_PROXY (no buddy):

  ┌───────┬──────────────────┬──────────────────┬──────────────┬──────────────┐
  │ MLBuf │ ADAPTIVE latency │ MIMALLOC latency │ ADAPTIVE RSS │ MIMALLOC RSS │
  ├───────┼──────────────────┼──────────────────┼──────────────┼──────────────┤
  │ 128   │ 132 ± 33 ns      │ 91 ± 3 ns        │ 284 MB       │ 446 MB       │
  ├───────┼──────────────────┼──────────────────┼──────────────┼──────────────┤
  │ 1024  │ 146 ± 29 ns      │ 98 ± 11 ns       │ 363 MB       │ 486 MB       │
  ├───────┼──────────────────┼──────────────────┼──────────────┼──────────────┤
  │ 4096  │ 113 ± 18 ns      │ 113 ± 2 ns       │ 657 MB       │ 909 MB       │
  ├───────┼──────────────────┼──────────────────┼──────────────┼──────────────┤
  │ 8192  │ 165 ± 40 ns      │ 278 ± 19 ns      │ 1001 MB      │ 2080 MB      │
  └───────┴──────────────────┴──────────────────┴──────────────┴──────────────┘

  API_GATEWAY (with buddy):

  ┌───────┬──────────────────┬──────────────────┬──────────────┬──────────────┐
  │ MLBuf │ ADAPTIVE latency │ MIMALLOC latency │ ADAPTIVE RSS │ MIMALLOC RSS │
  ├───────┼──────────────────┼──────────────────┼──────────────┼──────────────┤
  │ 128   │ 136 ± 27 ns      │ 248 ± 22 ns      │ 437 MB       │ 1507 MB      │
  ├───────┼──────────────────┼──────────────────┼──────────────┼──────────────┤
  │ 1024  │ 147 ± 28 ns      │ 114 ± 17 ns      │ 741 MB       │ 623 MB       │
  ├───────┼──────────────────┼──────────────────┼──────────────┼──────────────┤
  │ 4096  │ 241 ± 21 ns      │ 121 ± 4 ns       │ 2482 MB      │ 1036 MB      │
  ├───────┼──────────────────┼──────────────────┼──────────────┼──────────────┤
  │ 8192  │ 349 ± 17 ns      │ 428 ± 12 ns      │ 3715 MB      │ 2102 MB      │
  └───────┴──────────────────┴──────────────────┴──────────────┴──────────────┘

and the ratio of latencies are similar to yours

  SP (no buddy):

  ┌───────┬───────────────────────────────┬───────────────────────┐
  │ MLBuf │ ARM ratio (ADAPTIVE/MIMALLOC) │       x86 ratio       │
  ├───────┼───────────────────────────────┼───────────────────────┤
  │ 128   │ 1.45x                         │ 1.45x                 │
  ├───────┼───────────────────────────────┼───────────────────────┤
  │ 1024  │ 1.45x                         │ 1.49x                 │
  ├───────┼───────────────────────────────┼───────────────────────┤
  │ 4096  │ 1.37x                         │ 1.00x                 │
  ├───────┼───────────────────────────────┼───────────────────────┤
  │ 8192  │ 0.68x (ADAPTIVE wins)         │ 0.59x (ADAPTIVE wins) │
  └───────┴───────────────────────────────┴───────────────────────┘

  AG (buddy):

  ┌───────┬───────────┬───────────────────────┐
  │ MLBuf │ ARM ratio │       x86 ratio       │
  ├───────┼───────────┼───────────────────────┤
  │ 128   │ 1.12x     │ 0.55x (ADAPTIVE wins) │
  ├───────┼───────────┼───────────────────────┤
  │ 1024  │ 1.22x     │ 1.29x                 │
  ├───────┼───────────┼───────────────────────┤
  │ 4096  │ 2.13x     │ 1.99x                 │
  ├───────┼───────────┼───────────────────────┤
  │ 8192  │ 1.05x     │ 0.82x (ADAPTIVE wins) │
  └───────┴───────────┴───────────────────────┘

I still believe that both the evinction and capacity floor un buddy chunk cache to be bad....

NOTE thanks to this investigation I've found an important gap in the size class chunk cache: if the allocator is too quick to release a segment, all the allocations will end up being absorbed by the same chunk, over and over, without rotating to another chunk and querying the cache, which makes it unable to idle and age any of the chunks it contains, which clearly, is not ideal.
I'm now experimenting with having a per "striped heap" allocation counter to trigger aging or a per size class and striped heap allocation counter. The former has the advantage of shrinking and aging size classes caches which are effectively unused, but the other is a simpler and less invasive fix over the logic in the branch.

laosijikaichele reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981 franz1981 mentioned this pull request Jul 17, 2026
Direct pool OOM again with netty 4.2.16 #17107
Closed
@franz1981

franz1981 commented Jul 17, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

One qq for you @laosijikaichele in order to enable notification on releases which can trigger purging pages, mimalloc retain as owner thread the one which enter the lock, which can leak VTs with loom (and regardless is not a safe assumptions on Platform thread too..). It could be solved just via acquiring the stamped lock and once there can still safely purge, but it is going to harm concurrency in case the release happen on a different platform thread (which can happen with loom: a VT allocate something and send a write and flush back to the originating event loop which will be responsible to release..) so I am not sure the benchmark excercize a common case...
The fact we don't reacquire a stamped lock on each release is made on purpose for adaptive, although it harms and defer processing...
I have an idea on how to improve it for event loop cases, which can be different, but still...

Did you noticed the temporary leak of owner thread and the use case I mentioned?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Jul 17, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

One qq for you @laosijikaichele in order to enable notification on releases which can trigger purging pages, mimalloc retain as owner thread the one which enter the lock, which can leak VTs with loom (and regardless is not a safe assumptions on Platform thread too..). It could be solved just via acquiring the stamped lock and once there can still safely purge, but it is going to harm concurrency in case the release happen on a different platform thread (which can happen with loom: a VT allocate something and send a write and flush back to the originating event loop which will be responsible to release..) so I am not sure the benchmark excercize a common case... The fact we don't reacquire a stamped lock on each release is made on purpose for adaptive, although it harms and defer processing... I have an idea on how to improve it for event loop cases, which can be different, but still...

Did you noticed the temporary leak of owner thread and the use case I mentioned?

Currently, on allocation(producer) side, mimalloc periodically(using the heartbeat mechanism) collect the deferred releasing memory.
On releasing side, for shared(non-eventloop) releasing, if the segment's owner thread is same with the releasing thread, then it acquire the lock exclusively to release, otherwise, it try acquire the lock once, if not succeeded, it will do deferred releasing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Jul 21, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@laosijikaichele The last time I have checked it looks like mimalloc retain a strong reference to the owner thread for a segment. It means that if the segment won't be released or passed to other Threads which delay releasing it, can leak (temporarly) it, causing excessive GCs and promotion - which can be quite dangerous with VirtualThread(s) i.e. VT once promoted requires to allocate a fresh new StackChunk on the first parking/yield, attaching the previous stack chunk (in old gen) in a sort of linked lists of stack chunks.
I won't do it, if not via threadId tbh, and in any case I've measured that on 1ff87f3 it doesn't bring the same benefits than mimalloc while running on platform threads.

Anyway, the current state of 1ff87f3 seems good enough for a PR; i will further polish it and add some guard to help with OOM I've seen reported recently, with 1 core and small memory (the so called "low memory" use case), because the floor of 128 chunks retained per chunk cache seems to be way too much for adaptive.
More, i would like to bring a global tick mechanism ala mimalloc, to expire/evict more effectively chunks for "idle" size classes, but I'm struggling to make it fast and ensure that a periodic purging can quickly check which chunk caches need attention. If I fail to deliver this right, it means that a periodic purge risk to scan and perform O(3N) iterations over all the active 16 size classes, which can be really too much, even for a slow path op.
This "global" tick is something I can think to deliver later on tbh, but since I'm changing something pretty big already, it would cost me less to have the whole change to review as we do it.

laosijikaichele and leviYX reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@laosijikaichele

laosijikaichele commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

The last time I have checked it looks like mimalloc retain a strong reference to the owner thread for a segment. It means that if the segment won't be released or passed to other Threads which delay releasing it, can leak (temporarly) it, causing excessive GCs and promotion - which can be quite dangerous with VirtualThread(s) i.e. VT once promoted requires to allocate a fresh new StackChunk on the first parking/yield, attaching the previous stack chunk (in old gen) in a sort of linked lists of stack chunks.
I won't do it, if not via threadId tbh, and in any case I've measured that on 1ff87f3 it doesn't bring the same benefits than mimalloc while running on platform threads.

Yes, that's a good point 👍. threadId should be better.

franz1981 reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981 franz1981 mentioned this pull request Jul 27, 2026
Unified StripedHeap model, size-class chunk recycling and allocation-driven purge #17151
Open
@franz1981 franz1981 closed this Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AdaptivePoolingAllocator: Performance gap in latency/RSS in several benchmark cases compared with other allocators

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@franz1981 @laosijikaichele
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.