From 03743fca0d7f4bc3f96463248b5144fbd94e0069 Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Mon, 17 May 2021 16:22:33 +0200 Subject: [PATCH] Pooling allocator cleanups and checkstyle fixes --- .../io/netty/buffer/api/pool/PoolArena.java | 28 ++++++++----------- .../io/netty/buffer/api/pool/PoolChunk.java | 16 +++++------ .../buffer/api/pool/PoolThreadCache.java | 4 +-- .../api/pool/PooledAllocatorControl.java | 4 +-- .../api/pool/PooledBufferAllocator.java | 5 ++-- .../io/netty/buffer/api/pool/PooledDrop.java | 27 ++++-------------- .../api/pool/UnpooledUnthetheredMemory.java | 3 +- 7 files changed, 33 insertions(+), 54 deletions(-) diff --git a/src/main/java/io/netty/buffer/api/pool/PoolArena.java b/src/main/java/io/netty/buffer/api/pool/PoolArena.java index c4c97eb..b3e87f5 100644 --- a/src/main/java/io/netty/buffer/api/pool/PoolArena.java +++ b/src/main/java/io/netty/buffer/api/pool/PoolArena.java @@ -17,7 +17,6 @@ package io.netty.buffer.api.pool; import io.netty.buffer.api.AllocatorControl; import io.netty.buffer.api.Buffer; -import io.netty.buffer.api.BufferAllocator; import io.netty.buffer.api.MemoryManager; import io.netty.buffer.api.internal.Statics; import io.netty.util.internal.StringUtil; @@ -110,10 +109,6 @@ class PoolArena extends SizeClasses implements PoolArenaMetric, AllocatorControl return new PoolSubpage[size]; } - boolean isDirect() { - return manager.isNative(); - } - UntetheredMemory allocate(PooledAllocatorControl control, PoolThreadCache cache, int size) { final int sizeIdx = size2SizeIdx(size); @@ -164,7 +159,8 @@ class PoolArena extends SizeClasses implements PoolArenaMetric, AllocatorControl return memory; } - private UntetheredMemory tcacheAllocateNormal(PooledAllocatorControl control, PoolThreadCache cache, int size, int sizeIdx) { + private UntetheredMemory tcacheAllocateNormal( + PooledAllocatorControl control, PoolThreadCache cache, int size, int sizeIdx) { UntetheredMemory memory = cache.allocateNormal(this, control, size, sizeIdx); if (memory != null) { // was able to allocate out of the cache so move on @@ -178,7 +174,8 @@ class PoolArena extends SizeClasses implements PoolArenaMetric, AllocatorControl } // Method must be called inside synchronized(this) { ... } block - private UntetheredMemory allocateNormal(int size, int sizeIdx, PoolThreadCache threadCache, PooledAllocatorControl control) { + private UntetheredMemory allocateNormal( + int size, int sizeIdx, PoolThreadCache threadCache, PooledAllocatorControl control) { UntetheredMemory memory = q050.allocate(size, sizeIdx, threadCache, control); if (memory != null) { return memory; @@ -215,7 +212,7 @@ class PoolArena extends SizeClasses implements PoolArenaMetric, AllocatorControl private UntetheredMemory allocateHuge(int size) { activeBytesHuge.add(size); allocationsHuge.increment(); - return new UnpooledUnthetheredMemory(manager, size); + return new UnpooledUnthetheredMemory(parent, manager, size); } void free(PoolChunk chunk, long handle, int normCapacity, PoolThreadCache cache) { @@ -234,15 +231,12 @@ class PoolArena extends SizeClasses implements PoolArenaMetric, AllocatorControl void freeChunk(PoolChunk chunk, long handle, int normCapacity, SizeClass sizeClass) { final boolean destroyChunk; synchronized (this) { - switch (sizeClass) { - case Normal: - ++deallocationsNormal; - break; - case Small: - ++deallocationsSmall; - break; - default: - throw new Error(); + if (sizeClass == SizeClass.Normal) { + ++deallocationsNormal; + } else if (sizeClass == SizeClass.Small) { + ++deallocationsSmall; + } else { + throw new AssertionError("Unexpected size class: " + sizeClass); } destroyChunk = !chunk.parent.free(chunk, handle, normCapacity); } diff --git a/src/main/java/io/netty/buffer/api/pool/PoolChunk.java b/src/main/java/io/netty/buffer/api/pool/PoolChunk.java index e09292e..0d0edc3 100644 --- a/src/main/java/io/netty/buffer/api/pool/PoolChunk.java +++ b/src/main/java/io/netty/buffer/api/pool/PoolChunk.java @@ -515,14 +515,15 @@ final class PoolChunk implements PoolChunkMetric { | (long) inUsed << IS_USED_SHIFT; } - UntetheredMemory allocateBuffer(long handle, int size, PoolThreadCache threadCache, PooledAllocatorControl control) { + UntetheredMemory allocateBuffer(long handle, int size, PoolThreadCache threadCache, + PooledAllocatorControl control) { if (isRun(handle)) { int offset = runOffset(handle) << pageShifts; int maxLength = runSize(pageShifts, handle); PoolThreadCache poolThreadCache = arena.parent.threadCache(); initAllocatorControl(control, poolThreadCache, handle, maxLength); return new UntetheredChunkAllocation( - memory, control, this, poolThreadCache, handle, maxLength, offset, size); + memory, this, poolThreadCache, handle, maxLength, offset, size); } else { return allocateBufferWithSubpage(handle, size, threadCache, control); } @@ -539,13 +540,12 @@ final class PoolChunk implements PoolChunkMetric { int offset = (runOffset << pageShifts) + bitmapIdx * s.elemSize; initAllocatorControl(control, threadCache, handle, s.elemSize); - return new UntetheredChunkAllocation(memory, control, this, threadCache, handle, s.elemSize, offset, size); + return new UntetheredChunkAllocation(memory, this, threadCache, handle, s.elemSize, offset, size); } @SuppressWarnings("unchecked") - private static class UntetheredChunkAllocation implements UntetheredMemory { + private static final class UntetheredChunkAllocation implements UntetheredMemory { private final Object memory; - private final PooledAllocatorControl control; private final PoolChunk chunk; private final PoolThreadCache threadCache; private final long handle; @@ -554,10 +554,9 @@ final class PoolChunk implements PoolChunkMetric { private final int size; private UntetheredChunkAllocation( - Object memory, PooledAllocatorControl control, PoolChunk chunk, PoolThreadCache threadCache, + Object memory, PoolChunk chunk, PoolThreadCache threadCache, long handle, int maxLength, int offset, int size) { this.memory = memory; - this.control = control; this.chunk = chunk; this.threadCache = threadCache; this.handle = handle; @@ -573,7 +572,7 @@ final class PoolChunk implements PoolChunkMetric { @Override public Drop drop() { - return (Drop) new PooledDrop(control, chunk.arena, chunk, threadCache, handle, maxLength); + return (Drop) new PooledDrop(chunk.arena, chunk, threadCache, handle, maxLength); } } @@ -584,7 +583,6 @@ final class PoolChunk implements PoolChunkMetric { control.threadCache = threadCache; control.handle = handle; control.normSize = normSize; - control.updates++; } @Override diff --git a/src/main/java/io/netty/buffer/api/pool/PoolThreadCache.java b/src/main/java/io/netty/buffer/api/pool/PoolThreadCache.java index 14dc67d..2771b7f 100644 --- a/src/main/java/io/netty/buffer/api/pool/PoolThreadCache.java +++ b/src/main/java/io/netty/buffer/api/pool/PoolThreadCache.java @@ -288,8 +288,8 @@ final class PoolThreadCache { /** * Allocate a new {@link UntetheredMemory} using the provided chunk and handle with the capacity restrictions. */ - protected abstract UntetheredMemory allocBuf(PoolChunk chunk, long handle, int size, PoolThreadCache threadCache, - PooledAllocatorControl control); + protected abstract UntetheredMemory allocBuf( + PoolChunk chunk, long handle, int size, PoolThreadCache threadCache, PooledAllocatorControl control); /** * Add to cache if not already full. diff --git a/src/main/java/io/netty/buffer/api/pool/PooledAllocatorControl.java b/src/main/java/io/netty/buffer/api/pool/PooledAllocatorControl.java index 87917da..2e177c8 100644 --- a/src/main/java/io/netty/buffer/api/pool/PooledAllocatorControl.java +++ b/src/main/java/io/netty/buffer/api/pool/PooledAllocatorControl.java @@ -19,16 +19,16 @@ import io.netty.buffer.api.AllocatorControl; import io.netty.buffer.api.Buffer; class PooledAllocatorControl implements AllocatorControl { + public PooledBufferAllocator parent; public PoolArena arena; public PoolChunk chunk; public PoolThreadCache threadCache; public long handle; public int normSize; - public int updates; @Override public UntetheredMemory allocateUntethered(Buffer originator, int size) { - return arena.parent.allocate(this, size); + return parent.allocate(this, size); } @Override diff --git a/src/main/java/io/netty/buffer/api/pool/PooledBufferAllocator.java b/src/main/java/io/netty/buffer/api/pool/PooledBufferAllocator.java index 458318c..211dc2b 100644 --- a/src/main/java/io/netty/buffer/api/pool/PooledBufferAllocator.java +++ b/src/main/java/io/netty/buffer/api/pool/PooledBufferAllocator.java @@ -289,6 +289,7 @@ public class PooledBufferAllocator implements BufferAllocator, BufferAllocatorMe throw new IllegalArgumentException("Allocation size must be positive, but was " + size + '.'); } PooledAllocatorControl control = new PooledAllocatorControl(); + control.parent = this; UntetheredMemory memory = allocate(control, size); Buffer buffer = manager.recoverMemory(control, memory.memory(), memory.drop()); return buffer.fill((byte) 0).order(ByteOrder.nativeOrder()); @@ -305,7 +306,7 @@ public class PooledBufferAllocator implements BufferAllocator, BufferAllocatorMe } private UntetheredMemory allocateUnpooled(int size) { - return new UnpooledUnthetheredMemory(manager, size); + return new UnpooledUnthetheredMemory(this, manager, size); } @Override @@ -423,7 +424,7 @@ public class PooledBufferAllocator implements BufferAllocator, BufferAllocatorMe threadCache.free(); } - private PoolArena leastUsedArena(PoolArena[] arenas) { + private static PoolArena leastUsedArena(PoolArena[] arenas) { if (arenas == null || arenas.length == 0) { return null; } diff --git a/src/main/java/io/netty/buffer/api/pool/PooledDrop.java b/src/main/java/io/netty/buffer/api/pool/PooledDrop.java index efa50af..a3191a2 100644 --- a/src/main/java/io/netty/buffer/api/pool/PooledDrop.java +++ b/src/main/java/io/netty/buffer/api/pool/PooledDrop.java @@ -19,16 +19,13 @@ import io.netty.buffer.api.Buffer; import io.netty.buffer.api.Drop; class PooledDrop implements Drop { - private final PooledAllocatorControl control; - private PoolArena arena; - private PoolChunk chunk; - private PoolThreadCache threadCache; - private long handle; - private int normSize; + private final PoolArena arena; + private final PoolChunk chunk; + private final PoolThreadCache threadCache; + private final long handle; + private final int normSize; - PooledDrop(PooledAllocatorControl control, PoolArena arena, PoolChunk chunk, PoolThreadCache threadCache, - long handle, int normSize) { - this.control = control; + PooledDrop(PoolArena arena, PoolChunk chunk, PoolThreadCache threadCache, long handle, int normSize) { this.arena = arena; this.chunk = chunk; this.threadCache = threadCache; @@ -40,16 +37,4 @@ class PooledDrop implements Drop { public void drop(Buffer obj) { arena.free(chunk, handle, normSize, threadCache); } - - @Override - public void attach(Buffer obj) { - if (control.updates > 0) { - arena = control.arena; - chunk = control.chunk; - threadCache = control.threadCache; - handle = control.handle; - normSize = control.normSize; - control.updates = 0; - } - } } diff --git a/src/main/java/io/netty/buffer/api/pool/UnpooledUnthetheredMemory.java b/src/main/java/io/netty/buffer/api/pool/UnpooledUnthetheredMemory.java index 0f537c8..475087f 100644 --- a/src/main/java/io/netty/buffer/api/pool/UnpooledUnthetheredMemory.java +++ b/src/main/java/io/netty/buffer/api/pool/UnpooledUnthetheredMemory.java @@ -26,9 +26,10 @@ class UnpooledUnthetheredMemory implements AllocatorControl.UntetheredMemory { private final MemoryManager manager; private final Buffer buffer; - UnpooledUnthetheredMemory(MemoryManager manager, int size) { + UnpooledUnthetheredMemory(PooledBufferAllocator allocator, MemoryManager manager, int size) { this.manager = manager; PooledAllocatorControl allocatorControl = new PooledAllocatorControl(); + allocatorControl.parent = allocator; buffer = manager.allocateShared(allocatorControl, size, manager.drop(), Statics.CLEANER); }