Home / Class/ PoolThreadCache Class — netty Architecture

PoolThreadCache Class — netty Architecture

Architecture documentation for the PoolThreadCache class in PoolThreadCache.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba["PoolThreadCache"]
  2a7d96ed_6f58_dca1_f092_d9cac20e28ae["PoolThreadCache.java"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|defined in| 2a7d96ed_6f58_dca1_f092_d9cac20e28ae
  0e603157_90bc_3e55_d20f_3030a8c4d853["PoolThreadCache()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| 0e603157_90bc_3e55_d20f_3030a8c4d853
  147c7a2a_c059_8556_8d09_72ecb6e7607d["createSubPageCaches()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| 147c7a2a_c059_8556_8d09_72ecb6e7607d
  889d822a_52d8_de93_dcf2_0a4944151cf9["createNormalCaches()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| 889d822a_52d8_de93_dcf2_0a4944151cf9
  1b26e308_14e8_15c2_712b_9b4cf41bed03["log2()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| 1b26e308_14e8_15c2_712b_9b4cf41bed03
  3d3d2bff_2825_15d9_1c0a_1051549ec76d["allocateSmall()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| 3d3d2bff_2825_15d9_1c0a_1051549ec76d
  e8e3b809_f6ce_ff31_6add_bfbdca5637eb["allocateNormal()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| e8e3b809_f6ce_ff31_6add_bfbdca5637eb
  610ff9ad_3424_2b92_6d1b_26798bcd0ba8["allocate()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| 610ff9ad_3424_2b92_6d1b_26798bcd0ba8
  5ba074e2_e7b8_74b3_3d57_4dac0324b0f2["add()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| 5ba074e2_e7b8_74b3_3d57_4dac0324b0f2
  0cb1e698_5776_a20e_cb9d_1f9bc7c12e07["cache()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| 0cb1e698_5776_a20e_cb9d_1f9bc7c12e07
  67fe01c2_aedc_a81c_c637_b03b96610998["free()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| 67fe01c2_aedc_a81c_c637_b03b96610998
  91b877f4_1f34_8ac6_e3a4_4e8a9f7590d1["trim()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| 91b877f4_1f34_8ac6_e3a4_4e8a9f7590d1
  2e5e41fb_5512_318e_5f4b_1097ff34f0ba["cacheForSmall()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| 2e5e41fb_5512_318e_5f4b_1097ff34f0ba
  59ad3e91_bd0f_e86a_c592_5fbef948c057["cacheForNormal()"]
  f4c407ea_9e8c_d00d_ea7e_e355c94f8aba -->|method| 59ad3e91_bd0f_e86a_c592_5fbef948c057

Relationship Graph

Source Code

buffer/src/main/java/io/netty/buffer/PoolThreadCache.java lines 44–499

final class PoolThreadCache {

    private static final InternalLogger logger = InternalLoggerFactory.getInstance(PoolThreadCache.class);
    private static final int INTEGER_SIZE_MINUS_ONE = Integer.SIZE - 1;

    final PoolArena<byte[]> heapArena;
    final PoolArena<ByteBuffer> directArena;

    // Hold the caches for the different size classes, which are small and normal.
    private final MemoryRegionCache<byte[]>[] smallSubPageHeapCaches;
    private final MemoryRegionCache<ByteBuffer>[] smallSubPageDirectCaches;
    private final MemoryRegionCache<byte[]>[] normalHeapCaches;
    private final MemoryRegionCache<ByteBuffer>[] normalDirectCaches;

    private final int freeSweepAllocationThreshold;
    private final AtomicBoolean freed = new AtomicBoolean();
    @SuppressWarnings("unused") // Field is only here for the finalizer.
    private final FreeOnFinalize freeOnFinalize;

    private int allocations;

    // TODO: Test if adding padding helps under contention
    //private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;

    PoolThreadCache(PoolArena<byte[]> heapArena, PoolArena<ByteBuffer> directArena,
                    int smallCacheSize, int normalCacheSize, int maxCachedBufferCapacity,
                    int freeSweepAllocationThreshold, boolean useFinalizer) {
        checkPositiveOrZero(maxCachedBufferCapacity, "maxCachedBufferCapacity");
        this.freeSweepAllocationThreshold = freeSweepAllocationThreshold;
        this.heapArena = heapArena;
        this.directArena = directArena;
        if (directArena != null) {
            smallSubPageDirectCaches = createSubPageCaches(smallCacheSize, directArena.sizeClass.nSubpages);
            normalDirectCaches = createNormalCaches(normalCacheSize, maxCachedBufferCapacity, directArena);
            directArena.numThreadCaches.getAndIncrement();
        } else {
            // No directArea is configured so just null out all caches
            smallSubPageDirectCaches = null;
            normalDirectCaches = null;
        }
        if (heapArena != null) {
            // Create the caches for the heap allocations
            smallSubPageHeapCaches = createSubPageCaches(smallCacheSize, heapArena.sizeClass.nSubpages);
            normalHeapCaches = createNormalCaches(normalCacheSize, maxCachedBufferCapacity, heapArena);
            heapArena.numThreadCaches.getAndIncrement();
        } else {
            // No heapArea is configured so just null out all caches
            smallSubPageHeapCaches = null;
            normalHeapCaches = null;
        }

        // Only check if there are caches in use.
        if ((smallSubPageDirectCaches != null || normalDirectCaches != null
                || smallSubPageHeapCaches != null || normalHeapCaches != null)
                && freeSweepAllocationThreshold < 1) {
            throw new IllegalArgumentException("freeSweepAllocationThreshold: "
                    + freeSweepAllocationThreshold + " (expected: > 0)");
        }
        freeOnFinalize = useFinalizer ? new FreeOnFinalize(this) : null;
    }

    private static <T> MemoryRegionCache<T>[] createSubPageCaches(
            int cacheSize, int numCaches) {
        if (cacheSize > 0 && numCaches > 0) {
            @SuppressWarnings("unchecked")
            MemoryRegionCache<T>[] cache = new MemoryRegionCache[numCaches];
            for (int i = 0; i < cache.length; i++) {
                // TODO: maybe use cacheSize / cache.length
                cache[i] = new SubPageMemoryRegionCache<T>(cacheSize);
            }
            return cache;
        } else {
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    private static <T> MemoryRegionCache<T>[] createNormalCaches(
            int cacheSize, int maxCachedBufferCapacity, PoolArena<T> area) {
        if (cacheSize > 0 && maxCachedBufferCapacity > 0) {
            int max = Math.min(area.sizeClass.chunkSize, maxCachedBufferCapacity);

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free