O(1) buffer next capacity computation (#11641)

Motivation:

Enlarging buffers approaching 4 MiB size requires n iterations

Modification:

Use a single instruction to compute the next buffer capacity

Result:

Faster/Simpler calculateNewCapacity
This commit is contained in:
Francesco Nigro 2021-09-01 17:48:26 +02:00 committed by Norman Maurer
parent a39fea736d
commit 23601902ab

View File

@ -20,6 +20,7 @@ import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.ResourceLeakDetector; import io.netty.util.ResourceLeakDetector;
import io.netty.util.ResourceLeakTracker; import io.netty.util.ResourceLeakTracker;
import io.netty.util.internal.MathUtil;
import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
@ -272,12 +273,8 @@ public abstract class AbstractByteBufAllocator implements ByteBufAllocator {
return newCapacity; return newCapacity;
} }
// Not over threshold. Double up to 4 MiB, starting from 64. // 64 <= newCapacity is a power of 2 <= threshold
int newCapacity = 64; final int newCapacity = MathUtil.findNextPositivePowerOfTwo(Math.max(minNewCapacity, 64));
while (newCapacity < minNewCapacity) {
newCapacity <<= 1;
}
return Math.min(newCapacity, maxCapacity); return Math.min(newCapacity, maxCapacity);
} }
} }