diff --git a/buffer/src/main/java/io/netty/buffer/PoolArena.java b/buffer/src/main/java/io/netty/buffer/PoolArena.java index 57c4385e92..348b24f6c8 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolArena.java +++ b/buffer/src/main/java/io/netty/buffer/PoolArena.java @@ -188,10 +188,19 @@ abstract class PoolArena { if ((reqCapacity & 0xFFFFFE00) != 0) { // >= 512 // Doubled - int normalizedCapacity = 512; - while (normalizedCapacity < reqCapacity) { - normalizedCapacity <<= 1; + + int normalizedCapacity = reqCapacity; + normalizedCapacity |= normalizedCapacity >>> 1; + normalizedCapacity |= normalizedCapacity >>> 2; + normalizedCapacity |= normalizedCapacity >>> 4; + normalizedCapacity |= normalizedCapacity >>> 8; + normalizedCapacity |= normalizedCapacity >>> 16; + normalizedCapacity ++; + + if (normalizedCapacity < 0) { + normalizedCapacity >>>= 1; } + return normalizedCapacity; } diff --git a/transport/src/main/java/io/netty/channel/AdaptiveRecvByteBufAllocator.java b/transport/src/main/java/io/netty/channel/AdaptiveRecvByteBufAllocator.java index 4c123840b0..9827beeeae 100644 --- a/transport/src/main/java/io/netty/channel/AdaptiveRecvByteBufAllocator.java +++ b/transport/src/main/java/io/netty/channel/AdaptiveRecvByteBufAllocator.java @@ -44,23 +44,12 @@ public class AdaptiveRecvByteBufAllocator implements RecvByteBufAllocator { static { List sizeTable = new ArrayList(); - for (int i = 1; i <= 8; i ++) { + for (int i = 16; i < 512; i += 16) { sizeTable.add(i); } - for (int i = 4; i < 32; i ++) { - long v = 1L << i; - long inc = v >>> 4; - v -= inc << 3; - - for (int j = 0; j < 8; j ++) { - v += inc; - if (v > Integer.MAX_VALUE) { - sizeTable.add(Integer.MAX_VALUE); - } else { - sizeTable.add((int) v); - } - } + for (int i = 512; i > 0; i <<= 1) { + sizeTable.add(i); } SIZE_TABLE = new int[sizeTable.size()];