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 #14279DescriptionCurrently the adaptive pool is splitting the allocation by:
- creating/reusing an "empty"
AdaptiveByteBuf, at
|
AdaptiveByteBuf buf = AdaptiveByteBuf.newInstance(willCleanupFastThreadLocals); |
- filling it with some complex logic, at
|
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:
- 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
- attached to the
Magazine (lazily) -
|
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. Activityfranz1981 commented on Sep 4, 2024 ContributorAuthor More actions 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, 2024normanmaurer commented on Sep 4, 2024 @franz1981 hmm... I wonder why we not always just use the Recycler as we do as of today in PooledByteBufAllocator ? normanmaurer commented on Sep 4, 2024 franz1981 commented 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 normanmaurer commented on Sep 4, 2024 @franz1981 maybe you can do a PR to better show your idea... normanmaurer commented on Sep 4, 2024 @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 franz1981 commented 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 normanmaurer commented on Sep 4, 2024 @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 chrisvest commented on Sep 8, 2024 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 franz1981 commented 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. franz1981 commented 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 (!) normanmaurer commented on Sep 12, 2024 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. franz1981 commented 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... added a commit that references this issue on Nov 6, 2024This issue will close once commit 7cba76e is merged into the '4.1' branch.7cba76e added a commit that references this issue on Nov 7, 2024This issue will close once commit a434eef is merged into the '4.2' branch.a434eef
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|