Home / Class/ AllocationPatternState Class — netty Architecture

AllocationPatternState Class — netty Architecture

Architecture documentation for the AllocationPatternState class in ByteBufAllocatorAllocPatternBenchmark.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  2b9d2c55_b3ab_2241_3c94_2c28d1742399["AllocationPatternState"]
  7d4fb9c8_3875_0393_13aa_871ca0c13647["ByteBufAllocatorAllocPatternBenchmark.java"]
  2b9d2c55_b3ab_2241_3c94_2c28d1742399 -->|defined in| 7d4fb9c8_3875_0393_13aa_871ca0c13647
  5434ee01_3cf8_3416_b95e_92d02dcc98a8["setup()"]
  2b9d2c55_b3ab_2241_3c94_2c28d1742399 -->|method| 5434ee01_3cf8_3416_b95e_92d02dcc98a8
  bbe06704_584b_9c24_dc13_4fce32d179d4["getNextReleaseIndex()"]
  2b9d2c55_b3ab_2241_3c94_2c28d1742399 -->|method| bbe06704_584b_9c24_dc13_4fce32d179d4
  eb3b24cc_32a0_7289_8880_2dc69bc0b4b8["getNextSizeIndex()"]
  2b9d2c55_b3ab_2241_3c94_2c28d1742399 -->|method| eb3b24cc_32a0_7289_8880_2dc69bc0b4b8
  d17baac7_a97f_d30f_97ad_c7dad906b64b["release()"]
  2b9d2c55_b3ab_2241_3c94_2c28d1742399 -->|method| d17baac7_a97f_d30f_97ad_c7dad906b64b
  fc9f188a_ea95_26d2_52e9_d9834b8b3561["ByteBuf()"]
  2b9d2c55_b3ab_2241_3c94_2c28d1742399 -->|method| fc9f188a_ea95_26d2_52e9_d9834b8b3561
  fe84fc40_3a97_c97a_73cb_2217baa430ed["performDirectAllocation()"]
  2b9d2c55_b3ab_2241_3c94_2c28d1742399 -->|method| fe84fc40_3a97_c97a_73cb_2217baa430ed
  26bf866c_3b1b_fbfe_7ea6_9d3c0dcb9920["performHeapAllocation()"]
  2b9d2c55_b3ab_2241_3c94_2c28d1742399 -->|method| 26bf866c_3b1b_fbfe_7ea6_9d3c0dcb9920
  204ca013_c395_16e2_bbf1_588d738fe0d6["releaseBufferArray()"]
  2b9d2c55_b3ab_2241_3c94_2c28d1742399 -->|method| 204ca013_c395_16e2_bbf1_588d738fe0d6
  52227b39_333e_671f_f152_23356efc596d["tearDown()"]
  2b9d2c55_b3ab_2241_3c94_2c28d1742399 -->|method| 52227b39_333e_671f_f152_23356efc596d

Relationship Graph

Source Code

microbench/src/main/java/io/netty/microbench/buffer/ByteBufAllocatorAllocPatternBenchmark.java lines 76–173

    @State(Scope.Thread)
    public static class AllocationPatternState {
        private int[] releaseIndexes;
        private int[] sizes;
        private int nextReleaseIndex;
        private int nextSizeIndex;
        private ByteBuf[] buffers;
        private ByteBufAllocator allocator;

        @Setup
        public void setup(ByteBufAllocatorAllocPatternBenchmark benchmark) {
            this.allocator = benchmark.allocator;
            releaseIndexes = new int[MAX_LIVE_BUFFERS];
            sizes = new int[MathUtil.findNextPositivePowerOfTwo(FLATTEND_SIZE_ARRAY.length)];
            SplittableRandom rand = new SplittableRandom(SEED);
            // Pre-generate the to be released index.
            for (int i = 0; i < releaseIndexes.length; i++) {
                releaseIndexes[i] = rand.nextInt(releaseIndexes.length);
            }
            // Shuffle the `flattendSizeArray` to `sizes`.
            for (int i = 0; i < sizes.length; i++) {
                int sizeIndex = rand.nextInt(FLATTEND_SIZE_ARRAY.length);
                sizes[i] = FLATTEND_SIZE_ARRAY[sizeIndex];
            }
            nextReleaseIndex = 0;
            nextSizeIndex = 0;
            buffers = new ByteBuf[MAX_LIVE_BUFFERS];
        }

        private int getNextReleaseIndex() {
            int index = nextReleaseIndex;
            nextReleaseIndex = (nextReleaseIndex + 1) & (releaseIndexes.length - 1);
            return releaseIndexes[index];
        }

        private int getNextSizeIndex() {
            int index = nextSizeIndex;
            nextSizeIndex = (nextSizeIndex + 1) & (sizes.length - 1);
            return index;
        }

        @CompilerControl(CompilerControl.Mode.DONT_INLINE)
        private static void release(ByteBuf buf) {
            buf.release();
        }

        @CompilerControl(CompilerControl.Mode.DONT_INLINE)
        private static ByteBuf allocateHeap(ByteBufAllocator allocator, int size) {
            return allocator.heapBuffer(size);
        }

        @CompilerControl(CompilerControl.Mode.DONT_INLINE)
        private static ByteBuf allocateDirect(ByteBufAllocator allocator, int size) {
            return allocator.directBuffer(size);
        }

        @CompilerControl(CompilerControl.Mode.DONT_INLINE)
        public void performDirectAllocation() {
            int size = sizes[getNextSizeIndex()];
            int releaseIndex = getNextReleaseIndex();
            ByteBuf[] buffers = this.buffers;
            ByteBuf oldBuf = buffers[releaseIndex];
            if (oldBuf != null) {
                release(oldBuf);
            }
            ByteBuf newBuf = allocateDirect(allocator, size);
            buffers[releaseIndex] = newBuf;
        }

        @CompilerControl(CompilerControl.Mode.DONT_INLINE)
        public void performHeapAllocation() {
            int size = sizes[getNextSizeIndex()];
            int releaseIndex = getNextReleaseIndex();
            ByteBuf oldBuf = buffers[releaseIndex];
            if (oldBuf != null) {
                release(oldBuf);
            }
            ByteBuf newBuf = allocateHeap(allocator, size);
            buffers[releaseIndex] = newBuf;
        }

Frequently Asked Questions

What is the AllocationPatternState class?
AllocationPatternState is a class in the netty codebase, defined in microbench/src/main/java/io/netty/microbench/buffer/ByteBufAllocatorAllocPatternBenchmark.java.
Where is AllocationPatternState defined?
AllocationPatternState is defined in microbench/src/main/java/io/netty/microbench/buffer/ByteBufAllocatorAllocPatternBenchmark.java at line 76.

Analyze Your Own Codebase

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

Try Supermodel Free