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 — AdaptiveByteBufAllocator: Make pooling of AdaptiveByteBuf magazine local by chrisvest · Pull Request #14414 · netty/netty

Conversation

@chrisvest

@chrisvest chrisvest commented Oct 23, 2024

Copy link
Copy Markdown
Member

Motivation:

To reduce the usage of FastThreadLocal and make the AdaptiveByteBufAllocator also play nice with virtual threads we should just pool the AdapativeByteBuf instances in the Magazines itself.

Modifications:

Remove usage of Recycler for pooling AdaptiveByteBuf Use a bounded MPMC queue that is stored in the Magazine itself for pooling these instances Result:

Fixes #14279

Sorry, something went wrong.

Uh oh!

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

franz1981 and He-Pin reacted with heart emoji
All reactions
  • ❤️ 2 reactions
@chrisvest
chrisvest requested review from franz1981 and normanmaurer October 23, 2024 21:55
@chrisvest

chrisvest commented Oct 23, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

@normanmaurer I was investigating why there were test failures on #14332 and tried to recreate the PR from first principles. Regardless, I ended up with a very similar but slightly different implementation.

All reactions

Sorry, something went wrong.

Uh oh!

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

chrisvest
chrisvest commented Oct 23, 2024
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java Resolved

Uh oh!

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

@chrisvest chrisvest mentioned this pull request Oct 23, 2024
AdaptiveByteBufAllocator: Make pooling of AdaptiveByteBuf magazine local #14332
Closed
normanmaurer
normanmaurer requested changes Oct 24, 2024
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java Outdated

@normanmaurer normanmaurer Oct 24, 2024

Copy link
Copy Markdown
Member

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.

imho this usage is really odd as if this methods returns non null its a ByteBuf which has a lifetime. I know that this would just be the same as into and so its fine to not release but this is more an implementation detail and very confusing and fragile.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java Outdated

@normanmaurer normanmaurer Oct 24, 2024

Copy link
Copy Markdown
Member

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.

Like stated below I think this method implementation is fragile and confusing. Depending on if buf is null or not we might need to release the returned AdaptiveByteBuf. This makes lifecycle management error-prone. Let's rework this somehow

Sorry, something went wrong.

Uh oh!

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

All reactions

@chrisvest chrisvest Oct 24, 2024

Copy link
Copy Markdown
Member 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.

I've been thinking a lot about the need to signal failure and the need to return a buffer, and the conflict between those two will always produce complicated code. So instead I'll try to side-step the problem by pushing the fallback paths down, and make this method infallible.

Sorry, something went wrong.

Uh oh!

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

All reactions
@franz1981

franz1981 commented Oct 24, 2024

Copy link
Copy Markdown
Contributor

I'm all in to have this - it trades the virtue of simplicity with some cost in the optimized common path i.e.

  • if a thread is an event loop one it has to perform a CAS using the mpsc q to release the buffer wrapper

Another option could be to have a Recycler impl which has the same tunable we're used to AND using a pluggable concept of ownership. I'm preparing a small PoC to show what I mean

All reactions

Sorry, something went wrong.

Uh oh!

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

@chrisvest

chrisvest commented Oct 24, 2024

Copy link
Copy Markdown
Member Author

@normanmaurer Take a look. The allocate method does the fallback allocation now, instead of communicating potential allocation failure.

All reactions

Sorry, something went wrong.

Uh oh!

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

normanmaurer
normanmaurer approved these changes Oct 25, 2024
View reviewed changes
@normanmaurer

normanmaurer commented Oct 25, 2024

Copy link
Copy Markdown
Member

@franz1981 any other comments or can we merge this one ?

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer normanmaurer added this to the 4.1.115.Final milestone Oct 25, 2024
@franz1981

franz1981 commented Oct 25, 2024

Copy link
Copy Markdown
Contributor

Yep this morning I can share my proposal, yesterday I have been sidetracked the whole day : 🙏

All reactions

Sorry, something went wrong.

Uh oh!

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

@normanmaurer

normanmaurer commented Oct 25, 2024

Copy link
Copy Markdown
Member

@franz1981 ok just so I am sure I understood correctly... we should hold back until you shared your proposal, correct ?

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981

franz1981 commented Oct 25, 2024

Copy link
Copy Markdown
Contributor

Yess please 🥺

All reactions

Sorry, something went wrong.

Uh oh!

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

franz1981
franz1981 requested changes Oct 25, 2024
View reviewed changes
Comment thread buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java Outdated

@franz1981 franz1981 Oct 25, 2024

Copy link
Copy Markdown
Contributor

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.

if this is happening why holding the lock there's no need the q to be mpmc

Sorry, something went wrong.

Uh oh!

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

All reactions

@franz1981 franz1981 Nov 7, 2024

Copy link
Copy Markdown
Contributor

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.

if this is happening why holding the lock there's no need the q to be mpmc

This @chrisvest ?

Sorry, something went wrong.

Uh oh!

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

All reactions

@chrisvest chrisvest Nov 7, 2024

Copy link
Copy Markdown
Member 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.

Perhaps, but I want to have a full CI run backing up any move away from MPMC.

Sorry, something went wrong.

Uh oh!

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

All reactions

@chrisvest chrisvest Nov 23, 2024

Copy link
Copy Markdown
Member 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.

Here in the thread-local case, we'll go to the Magazine.EVENT_LOOP_LOCAL_BUFFER_POOL, which uses MPSC under the hood.

When magazines are sharable, they may be used as fallbacks, which do not involve locking the magazine.

Sorry, something went wrong.

Uh oh!

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

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

franz1981 commented Oct 25, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

@normanmaurer @chrisvest it -> https://github.com/franz1981/netty/tree/4.1-mag-local-pool

I'm not fixing the comment at #14414 (comment) or the ones from Norman, but proposing a different fast(er)-path for event loop confined magazines (as usual :P )
I would have preferred to avoid another fasthreadlocal::get TBH, given that we already do it for the event loop-local magazine, but in this way we can reuse the Recycler without modifying it.
I believe there is some bench which correctly use fast thread local threads on JMH which can be used to compare against the 2.

The branch I've sent got a sole concern, perf wise:

  • I made the q-based handle (shared magazines) to be an EnhancedHandle child (which impl the recycle methods)
  • The Recycler already have 2 EnhancedHandle i.e NOOP_HANDLE and the default one
  • it means that AdpativeByteBuf::deallocate can be megamorphic (having 3 concrete types which can be called there)

An "easy" solution to this would be to have AdaptiveByteBuf to just use an Handle (meaning that the shared magazines won't create a child of EnhancedHandle anymore, but just Handle - and perform an instance check on the buffer deallocate vs EnhancedHandle - this will make possible to have 2 call-sites:

  • Handle::recycle which will observe a single type i.e. the one using the queue
  • Handle::unguardedRecycle which can observe 2 concrete types and get in the fast path of virtual call (making the JIT to emit an bimorphic guard vs the 2 types observed)

wdyt? I can add an additional commit to fix this

All reactions

Sorry, something went wrong.

Uh oh!

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

@chrisvest

chrisvest commented Oct 25, 2024

Copy link
Copy Markdown
Member Author

@franz1981 The two approaches sound equivalent in terms of code style, so we could just let the benchmarks decide. So how about we look at that in a follow-up? I know it can take lot of time to tweak benchmarks back and forth.

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981

franz1981 commented Oct 25, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Right now I suggest already to bring the fast thread local solution in too - which uses array deq for the fastest path - which is very difficult to beat, unless there is something very wrong in fast thread local or elsewhere (the handle availability).
If you don't feel confident for this I can send a separate PR with the follow up changes

All reactions

Sorry, something went wrong.

Uh oh!

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

@franz1981
franz1981 force-pushed the 4.1-mag-local-pool branch from 2baa8c2 to c5a0cc6 Compare October 28, 2024 12:56
@franz1981

franz1981 commented Oct 28, 2024

Copy link
Copy Markdown
Contributor

@chrisvest I've sent by accident my changes into your branch - now I've forcibly pushed your original commits removing mine :"(
I'll send a separate draft PR on top of your changes here to help @normanmaurer as well to see what changes I'm proposing

chrisvest 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 Oct 28, 2024
Specialize Adaptive's allocator Recycler based on magazine's owner #14421
Merged
chrisvest added 3 commits November 6, 2024 13:32
@chrisvest
AdaptiveByteBufAllocator: Make pooling of AdaptiveByteBuf magazine local
7cba76e
Motivation:

To reduce the usage of FastThreadLocal and make the AdaptiveByteBufAllocator also play nice with virtual threads we should just pool the AdapativeByteBuf instances in the Magazines itself.

Modifications:

Remove usage of Recycler for pooling AdaptiveByteBuf
Use a bounded MPMC queue that is stored in the Magazine itself for pooling these instances
Result:

Fixes netty#14279
@chrisvest
Change default magazine queue capacity
0050cb0
@chrisvest
Make the internal allocate method infallible to simplify error handling
b2bbd8d
@chrisvest
chrisvest force-pushed the 4.1-mag-local-pool branch from c5a0cc6 to b2bbd8d Compare November 6, 2024 21:54
@chrisvest
chrisvest merged commit a434eef into netty:4.1 Nov 7, 2024
@chrisvest
chrisvest deleted the 4.1-mag-local-pool branch November 7, 2024 18:44
franz1981 pushed a commit that referenced this pull request Nov 11, 2024
@chrisvest @franz1981
AdaptiveByteBufAllocator: Make pooling of AdaptiveByteBuf magazine lo…
0cf14f6
…cal #14414

Motivation:

To reduce the usage of FastThreadLocal and make the AdaptiveByteBufAllocator also play nice with virtual threads we should just pool the AdapativeByteBuf instances in the Magazines itself.

Modifications:

Remove usage of Recycler for pooling AdaptiveByteBuf
Use a bounded MPMC queue that is stored in the Magazine itself for pooling these instances
Result:

Fixes #14279
@normanmaurer normanmaurer mentioned this pull request Sep 14, 2026
Http2: Reserve promised stream for PUSH_PROMISE after local RST_STREAM #17471
Closed
@normanmaurer normanmaurer mentioned this pull request Sep 21, 2026
cache failure 1233e4a5 #17565
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@franz1981 franz1981 franz1981 requested changes
@normanmaurer normanmaurer normanmaurer approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.1.115.Final

Development

Successfully merging this pull request may close these issues.

Don't use Netty recyclers for the adaptive ByteBuf pool, but leverage magazine-local ones

Uh oh!

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

3 participants

@chrisvest @franz1981 @normanmaurer
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.