Skip to content

Commit

Permalink
Enable thread-local buffer cache for Thread that is used by EventExec…
Browse files Browse the repository at this point in the history
…utor by default (#12158)

Motivation:

#12109 changed the default of using the thread local cache for buffers only for FastThreadLocalThread. While this is good it also means that it would affect everyone that use a custom ThreadFactory when construct the EventLoop / EventExecutor

Modifications:

Make use of ThreadExecutorMap to check if the Thread is used by any EventExecutor and if so also enable the cache.

Result:

Fixes #12109 (comment)
  • Loading branch information
normanmaurer committed Mar 9, 2022
1 parent 669a4ba commit 2401841
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,13 +511,19 @@ protected synchronized PoolThreadCache initialValue() {
final PoolArena<ByteBuffer> directArena = leastUsedArena(directArenas);

final Thread current = Thread.currentThread();
if (useCacheForAllThreads || current instanceof FastThreadLocalThread) {
final EventExecutor executor = ThreadExecutorMap.currentExecutor();

if (useCacheForAllThreads ||
// If the current thread is a FastThreadLocalThread we will always use the cache
current instanceof FastThreadLocalThread ||
// The Thread is used by an EventExecutor, let's use the cache as the chances are good that we
// will allocate a lot!
executor != null) {
final PoolThreadCache cache = new PoolThreadCache(
heapArena, directArena, smallCacheSize, normalCacheSize,
DEFAULT_MAX_CACHED_BUFFER_CAPACITY, DEFAULT_CACHE_TRIM_INTERVAL);

if (DEFAULT_CACHE_TRIM_INTERVAL_MILLIS > 0) {
final EventExecutor executor = ThreadExecutorMap.currentExecutor();
if (executor != null) {
executor.scheduleAtFixedRate(trimTask, DEFAULT_CACHE_TRIM_INTERVAL_MILLIS,
DEFAULT_CACHE_TRIM_INTERVAL_MILLIS, TimeUnit.MILLISECONDS);
Expand Down

0 comments on commit 2401841

Please sign in to comment.