From 8c0f1428af513e7f4226a8be8d63b650fc5472b9 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 16 Jul 2020 19:40:40 +0200 Subject: [PATCH] Reduce the scope of synchronized block in PoolArena (#10410) Motivation: We shouldn't call incSmallAllocation() in a synchronized block as its backed by a concurrent datastructure Modifications: Move call of incSmallAllocation() out of synchronized block Result: Minimize scope of synchronized block --- buffer/src/main/java/io/netty/buffer/PoolArena.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/PoolArena.java b/buffer/src/main/java/io/netty/buffer/PoolArena.java index 427e55c8ec..a60feb0753 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolArena.java +++ b/buffer/src/main/java/io/netty/buffer/PoolArena.java @@ -158,21 +158,24 @@ abstract class PoolArena extends SizeClasses implements PoolArenaMetric { * {@link PoolChunk#free(long)} may modify the doubly linked list as well. */ final PoolSubpage head = smallSubpagePools[sizeIdx]; + final boolean needsNormalAllocation; synchronized (head) { final PoolSubpage s = head.next; - if (s != head) { + needsNormalAllocation = s == head; + if (!needsNormalAllocation) { assert s.doNotDestroy && s.elemSize == sizeIdx2size(sizeIdx); long handle = s.allocate(); assert handle >= 0; s.chunk.initBufWithSubpage(buf, null, handle, reqCapacity, cache); - incSmallAllocation(); - return; } } - synchronized (this) { - allocateNormal(buf, reqCapacity, sizeIdx, cache); + if (needsNormalAllocation) { + synchronized (this) { + allocateNormal(buf, reqCapacity, sizeIdx, cache); + } } + incSmallAllocation(); }