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 — Don't use Netty recyclers for the adaptive ByteBuf pool, but leverage magazine-local ones · Issue #14279 · netty/netty

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

New issue
Copy link
New issue
Copy link
Closed
#14414
Closed
Don't use Netty recyclers for the adaptive ByteBuf pool, but leverage magazine-local ones#14279
#14414
Copy link
Labels
importantimprovement

Description

@franz1981
franz1981
opened on Sep 4, 2024
Issue body actions

Currently the adaptive pool is splitting the allocation by:

  • creating/reusing an "empty" AdaptiveByteBuf, at

    netty/buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java

    Line 193 in c0fdb8e

    AdaptiveByteBuf buf = AdaptiveByteBuf.newInstance(willCleanupFastThreadLocals);
  • filling it with some complex logic, at

    netty/buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java

    Line 417 in c0fdb8e

    public AdaptiveByteBuf allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) {

This is nice, but suboptimal for non FastThreadLocalThreads as reported at #14272 (see #14272 (comment)).

Instead, it would be better to have 2 recyclers:

  1. attached to non event-loop magazines - It's fine(ish) to reuse the existing Recycler too if we don't want to create an extra type
  2. attached to the Magazine (lazily) -

    netty/buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java

    Line 223 in c0fdb8e

    long writeLock = mag.tryWriteLock();
    : which access is already protected by a lock and it allow release to happen from other threads

For the former, just reusing the existing logic is fine - while for the latter it is important to find the appropriate quiescient state to release such.
Clearly the issue will be that an explosion of magazine (due to lack of CPU resources i.e. when the allocators are from a non Netty event loop and they oversubscribe the system) will likely cause an explosion of magazine-locals recyclers - which means more memory; but the benefits, perf-wise should be obvious.

Activity

  1. franz1981
    added
    improvement
    important
    on Sep 4, 2024
  2. franz1981 commented on Sep 4, 2024

    @franz1981
    franz1981
    on Sep 4, 2024
    ContributorAuthor
    More actions

    @chrisvest @normanmaurer wdyt?

  3. franz1981
    changed the title [-]Don't use Netty recyclers for the adaptive ByteBuf pool, but leverage the striped/thread-local magazines[/-] [+]Don't use Netty recyclers for the adaptive ByteBuf pool, but leverage magazine-local ones[/+] on Sep 4, 2024
  4. normanmaurer commented on Sep 4, 2024

    @normanmaurer
    normanmaurer
    on Sep 4, 2024
    Member
    More actions

    @franz1981 hmm... I wonder why we not always just use the Recycler as we do as of today in PooledByteBufAllocator ?

  5. normanmaurer commented on Sep 4, 2024

    @normanmaurer
    normanmaurer
    on Sep 4, 2024
    Member
    More actions

    Also /cc @chrisvest for my question :)

  6. franz1981 commented on Sep 4, 2024

    @franz1981
    franz1981
    on Sep 4, 2024
    ContributorAuthor
    More actions

    @normanmaurer this is because of virtual threads: these threads are supposely abundant and created while never been cached - allocating the whole ThreadLocal stuff there will kill perf -> see FasterXML/jackson-core#919

    The thing is that I see no point to pooling per thread, given that until the magazine is avaialble and "owned" there is no point to allocating an empty wrapper

  7. normanmaurer commented on Sep 4, 2024

    @normanmaurer
    normanmaurer
    on Sep 4, 2024
    Member
    More actions

    @franz1981 maybe you can do a PR to better show your idea...

  8. normanmaurer commented on Sep 4, 2024

    @normanmaurer
    normanmaurer
    on Sep 4, 2024
    Member
    More actions

    @franz1981 also we could just use a methodhandle to check if the thread is virtual or not and based on it use the recycler or not via using Thread.currentThread.isVirtual(). The method handle will ensure we can still support java8

  9. franz1981 commented on Sep 4, 2024

    @franz1981
    franz1981
    on Sep 4, 2024
    ContributorAuthor
    More actions

    @normanmaurer fully agree - it was what i do in other libs - but the problem si that recyclers retain lot of garbage and most of the time higher level frameworks/libs have blocking thread pools as well with TONS of half-idle threads, which can get destroyed and recreated while active again...
    having all of them to retain N direct buffers from being used elsewhere or released, usually cost memory/RSS.
    Here we're lucky that is just a wrapper, so maybe we can make an exception...
    But in general, that's why I prefer users to have a conscious choice and extends FastThreadLocalThread: it means they really need that

  10. normanmaurer commented on Sep 4, 2024

    @normanmaurer
    normanmaurer
    on Sep 4, 2024
    Member
    More actions

    @franz1981 not sure about that... I think both approaches have trade-offs . That said I am looking forward to see a PR with your ideas :)

    Reacted by Francesco Nigro and Quantriple
  11. chrisvest commented on Sep 8, 2024

    @chrisvest
    chrisvest
    on Sep 8, 2024
    Member
    More actions

    If magazines can directly recycle AdaptiveByteBufs without the traditional Recycler machinery, then we could perhaps also reduce some of the event-loop special-case code in the allocator.

    I don't understand the "explosion of magazines" problem that you are anticipating, @franz1981. The ByteBufs being recycled or not should not impact the number of magazines floating around, no?

    Reacted by Francesco Nigro
  12. franz1981 commented on Sep 8, 2024

    @franz1981
    franz1981
    on Sep 8, 2024
    ContributorAuthor
    More actions

    Let's say you have a blocking thread pool which grows due to a sudden burst of parallel work, it "could" cause magazine expansion due to 3 failures in a row to acquire the stamped lock; if later on these threads die (which is not uncommon for elastic thead pools), given that the buffer wrappers doesn't belong to them, they won't GC'd but will still be bound to the newly allocated magazine.

  13. franz1981 commented on Sep 12, 2024

    @franz1981
    franz1981
    on Sep 12, 2024
    ContributorAuthor
    More actions

    @chrisvest if you are fine with this proposal - which I believe you got perfectly despite my poor language to explain it - fell free to send a PR or I will do it in the next week(s)...sadly right now I'm a bit overwhelmed...
    The good thing of this is that recycling can be made fast like this, for ANY thread, including virtual ones, and will compensate with #14322 which had a performance impact - atomic ops in the hot path are never free (!)

  14. normanmaurer commented on Sep 12, 2024

    @normanmaurer
    normanmaurer
    on Sep 12, 2024
    Member
    More actions

    I think the "downside" of this approach would be:

    • AdapativeByteBuf pooling will be per magazine, which might result into more objects pooled.
    • The queue that is used for pooling would need to be MPSC as the offer might happen from any thread (there is no guarantees on which thread might call release(...). that said we could use some form of relaxedOffer(...) I guess

    I am not saying its worse then what we have as of today but something to keep in mind.

  15. franz1981 commented on Sep 12, 2024

    @franz1981
    franz1981
    on Sep 12, 2024
    ContributorAuthor
    More actions

    yeah @normanmaurer fully agree on both points; My suggestion was to let any threads (virtual, event loops, whatever) to have the same mechanism to recycle such AdapativeByteBuf.
    Virtual threads cannot really use ThreadLocals for pooling if not with big performance problems and not using pooling is equally bad...

  16. normanmaurer
    added a commit that references this issue on Sep 13, 2024

    AdaptiveByteBufAllocator: Make pooling of AdaptiveByteBuf magazine local

    abf3989
  17. chrisvest
    added a commit that references this issue on Nov 6, 2024

    AdaptiveByteBufAllocator: Make pooling of AdaptiveByteBuf magazine local

    This issue will close once commit 7cba76e is merged into the '4.1' branch.
    7cba76e
  18. chrisvest
    closed this as completedin #14414on Nov 7, 2024
  19. chrisvest
    added a commit that references this issue on Nov 7, 2024

    AdaptiveByteBufAllocator: Make pooling of AdaptiveByteBuf magazine lo…

    This issue will close once commit a434eef is merged into the '4.2' branch.
    a434eef
  20. normanmaurer
    added a commit that references this issue on Nov 12, 2024

    Specialize Adaptive's allocator Recycler based on magazine's owner (#…

    293931f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    importantimprovement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.