Make AdaptiveRecvByteBufAllocator's lookup table simpler / Optimize buffer size normalization

- No need to have fine-grained lookup table because the buffer pool has
  much more coarse capacities available
- No need to use a loop to normalize a buffer capacity
This commit is contained in:
Trustin Lee 2013-06-20 18:38:11 +09:00
parent dba3aa2d4f
commit a2f232720b
2 changed files with 15 additions and 17 deletions

View File

@ -188,10 +188,19 @@ abstract class PoolArena<T> {
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;
}

View File

@ -44,23 +44,12 @@ public class AdaptiveRecvByteBufAllocator implements RecvByteBufAllocator {
static {
List<Integer> sizeTable = new ArrayList<Integer>();
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()];