Home / Class/ PoolThreadLocalCache Class — netty Architecture

PoolThreadLocalCache Class — netty Architecture

Architecture documentation for the PoolThreadLocalCache class in PooledByteBufAllocator.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  9ad685de_b1d1_da3f_7071_bd6a25943246["PoolThreadLocalCache"]
  07bd0cf0_220d_d51e_1919_92a58786c6c3["PooledByteBufAllocator.java"]
  9ad685de_b1d1_da3f_7071_bd6a25943246 -->|defined in| 07bd0cf0_220d_d51e_1919_92a58786c6c3
  9e3cca92_cb0f_7d14_b519_7ba32799f355["PoolThreadLocalCache()"]
  9ad685de_b1d1_da3f_7071_bd6a25943246 -->|method| 9e3cca92_cb0f_7d14_b519_7ba32799f355
  f96ecd0d_de3e_5b33_3b04_dafb79f28944["PoolThreadCache()"]
  9ad685de_b1d1_da3f_7071_bd6a25943246 -->|method| f96ecd0d_de3e_5b33_3b04_dafb79f28944
  e451a9de_93c3_5fd3_6e4d_4a00c94645ba["onRemoval()"]
  9ad685de_b1d1_da3f_7071_bd6a25943246 -->|method| e451a9de_93c3_5fd3_6e4d_4a00c94645ba
  3a397dda_86fd_1377_4f30_948b9bdc738f["leastUsedArena()"]
  9ad685de_b1d1_da3f_7071_bd6a25943246 -->|method| 3a397dda_86fd_1377_4f30_948b9bdc738f

Relationship Graph

Source Code

buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java lines 518–580

    private final class PoolThreadLocalCache extends FastThreadLocal<PoolThreadCache> {
        private final boolean useCacheForAllThreads;

        PoolThreadLocalCache(boolean useCacheForAllThreads) {
            this.useCacheForAllThreads = useCacheForAllThreads;
        }

        @Override
        protected synchronized PoolThreadCache initialValue() {
            final PoolArena<byte[]> heapArena = leastUsedArena(heapArenas);
            final PoolArena<ByteBuffer> directArena = leastUsedArena(directArenas);

            final Thread current = Thread.currentThread();
            final EventExecutor executor = ThreadExecutorMap.currentExecutor();

            if (useCacheForAllThreads ||
                    // If the current thread is a FastThreadLocalThread we will always use the cache
                    FastThreadLocalThread.currentThreadHasFastThreadLocal() ||
                    // 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, useCacheFinalizers());

                if (DEFAULT_CACHE_TRIM_INTERVAL_MILLIS > 0) {
                    if (executor != null) {
                        executor.scheduleAtFixedRate(trimTask, DEFAULT_CACHE_TRIM_INTERVAL_MILLIS,
                                DEFAULT_CACHE_TRIM_INTERVAL_MILLIS, TimeUnit.MILLISECONDS);
                    }
                }
                return cache;
            }
            // No caching so just use 0 as sizes.
            return new PoolThreadCache(heapArena, directArena, 0, 0, 0, 0, false);
        }

        @Override
        protected void onRemoval(PoolThreadCache threadCache) {
            threadCache.free(false);
        }

        private <T> PoolArena<T> leastUsedArena(PoolArena<T>[] arenas) {
            if (arenas == null || arenas.length == 0) {
                return null;
            }

            PoolArena<T> minArena = arenas[0];
            //optimized
            //If it is the first execution, directly return minarena and reduce the number of for loop comparisons below
            if (minArena.numThreadCaches.get() == CACHE_NOT_USED) {
                return minArena;
            }
            for (int i = 1; i < arenas.length; i++) {
                PoolArena<T> arena = arenas[i];
                if (arena.numThreadCaches.get() < minArena.numThreadCaches.get()) {
                    minArena = arena;
                }
            }

            return minArena;
        }
    }

Frequently Asked Questions

What is the PoolThreadLocalCache class?
PoolThreadLocalCache is a class in the netty codebase, defined in buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java.
Where is PoolThreadLocalCache defined?
PoolThreadLocalCache is defined in buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java at line 518.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free