Optimize log2 in PoolThreadCache (#10128)

Motivation:
The current implementation of log2 in PoolThreadCache uses a loop and less efficient than an version based on Integer.numberOfLeadingZeros (intrinsic).

Modifications:
Replace the current log2 implementation in PoolThreadCache with a version based on Integer.numberOfLeadingZeros

Result:
It can improve performance slightly during allocation and de-allocation of ByteBuf using pooled allocator.
This commit is contained in:
Dmitry Konstantinov 2020-03-24 13:13:52 +03:00 committed by Norman Maurer
parent cb8fdd6c73
commit 71b7dbc009

View File

@ -41,6 +41,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
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;
@ -150,13 +151,9 @@ final class PoolThreadCache {
}
}
// val > 0
private static int log2(int val) {
int res = 0;
while (val > 1) {
val >>= 1;
res++;
}
return res;
return INTEGER_SIZE_MINUS_ONE - Integer.numberOfLeadingZeros(val);
}
/**
@ -322,6 +319,7 @@ final class PoolThreadCache {
private MemoryRegionCache<?> cacheForNormal(PoolArena<?> area, int normCapacity) {
if (area.isDirect()) {
// sizeClass == Normal => normCapacity >= pageSize => the shifted value > 0
int idx = log2(normCapacity >> numShiftsNormalDirect);
return cache(normalDirectCaches, idx);
}