More accurate default receive buffer size predictor implementation
This commit is contained in:
parent
0831b32090
commit
44c409ca24
@ -22,14 +22,17 @@
|
|||||||
*/
|
*/
|
||||||
package org.jboss.netty.channel.socket.nio;
|
package org.jboss.netty.channel.socket.nio;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default {@link ReceiveBufferSizePredictor} implementation.
|
* The default {@link ReceiveBufferSizePredictor} implementation.
|
||||||
* <p>
|
* <p>
|
||||||
* It doubles the expected number of readable bytes if the previous read
|
* It gradually increases the expected number of readable bytes if the
|
||||||
* filled the allocated buffer. It halves the expected number of readable
|
* previous read filled the allocated buffer. It gradually decreases the
|
||||||
* bytes if the read operation was not able to fill a quarter of the allocated
|
* expected number of readable bytes if the read operation was not able to
|
||||||
* buffer two times consecutively. Otherwise, it keeps returning the previous
|
* fill a certain amount of the allocated buffer two times consecutively.
|
||||||
* prediction.
|
* Otherwise, it keeps returning the previous prediction.
|
||||||
*
|
*
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
* @author Trustin Lee (tlee@redhat.com)
|
||||||
@ -39,19 +42,78 @@ package org.jboss.netty.channel.socket.nio;
|
|||||||
*/
|
*/
|
||||||
public class DefaultReceiveBufferSizePredictor implements
|
public class DefaultReceiveBufferSizePredictor implements
|
||||||
ReceiveBufferSizePredictor {
|
ReceiveBufferSizePredictor {
|
||||||
private static final int DEFAULT_MINIMUM = 256;
|
|
||||||
|
private static final int INDEX_INCREMENT = 4;
|
||||||
|
private static final int INDEX_DECREMENT = 1;
|
||||||
|
|
||||||
|
private static final int[] SIZE_TABLE;
|
||||||
|
|
||||||
|
static {
|
||||||
|
List<Integer> sizeTable = new ArrayList<Integer>();
|
||||||
|
for (int i = 1; i <= 8; i ++) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SIZE_TABLE = new int[sizeTable.size()];
|
||||||
|
for (int i = 0; i < SIZE_TABLE.length; i ++) {
|
||||||
|
SIZE_TABLE[i] = sizeTable.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getSizeTableIndex(final int size) {
|
||||||
|
if (size <= 16) {
|
||||||
|
return size - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bits = 0;
|
||||||
|
int v = size;
|
||||||
|
do {
|
||||||
|
v >>>= 1;
|
||||||
|
bits ++;
|
||||||
|
} while (v != 0);
|
||||||
|
|
||||||
|
final int baseIdx = bits << 3;
|
||||||
|
final int startIdx = baseIdx - 18;
|
||||||
|
final int endIdx = baseIdx - 25;
|
||||||
|
|
||||||
|
for (int i = startIdx; i >= endIdx; i --) {
|
||||||
|
if (size >= SIZE_TABLE[i]) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error("shouldn't reach here; please file a bug report.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final int DEFAULT_MINIMUM = 64;
|
||||||
private static final int DEFAULT_INITIAL = 1024;
|
private static final int DEFAULT_INITIAL = 1024;
|
||||||
private static final int DEFAULT_MAXIMUM = 65536;
|
private static final int DEFAULT_MAXIMUM = 65536;
|
||||||
|
|
||||||
private final int minimum;
|
private final int minIndex;
|
||||||
private final int maximum;
|
private final int maxIndex;
|
||||||
private int nextReceiveBufferSize = 1024;
|
private int index;
|
||||||
private boolean shouldHalveNow;
|
private int nextReceiveBufferSize;
|
||||||
|
private boolean decreaseNow;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new predictor with the default parameters. With the default
|
* Creates a new predictor with the default parameters. With the default
|
||||||
* parameters, the expected buffer size starts from {@code 1024}, doesn't
|
* parameters, the expected buffer size starts from {@code 1024}, doesn't
|
||||||
* go down below {@code 256}, and doesn't go up above {@code 1048576}.
|
* go down below {@code 64}, and doesn't go up above {@code 65536}.
|
||||||
*/
|
*/
|
||||||
public DefaultReceiveBufferSizePredictor() {
|
public DefaultReceiveBufferSizePredictor() {
|
||||||
this(DEFAULT_MINIMUM, DEFAULT_INITIAL, DEFAULT_MAXIMUM);
|
this(DEFAULT_MINIMUM, DEFAULT_INITIAL, DEFAULT_MAXIMUM);
|
||||||
@ -74,9 +136,23 @@ public class DefaultReceiveBufferSizePredictor implements
|
|||||||
if (maximum < initial) {
|
if (maximum < initial) {
|
||||||
throw new IllegalArgumentException("maximum: " + maximum);
|
throw new IllegalArgumentException("maximum: " + maximum);
|
||||||
}
|
}
|
||||||
this.minimum = minimum;
|
|
||||||
nextReceiveBufferSize = initial;
|
int minIndex = getSizeTableIndex(minimum);
|
||||||
this.maximum = maximum;
|
if (SIZE_TABLE[minIndex] < minimum) {
|
||||||
|
this.minIndex = minIndex + 1;
|
||||||
|
} else {
|
||||||
|
this.minIndex = minIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
int maxIndex = getSizeTableIndex(maximum);
|
||||||
|
if (SIZE_TABLE[maxIndex] > maximum) {
|
||||||
|
this.maxIndex = maxIndex - 1;
|
||||||
|
} else {
|
||||||
|
this.maxIndex = maxIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
index = getSizeTableIndex(initial);
|
||||||
|
nextReceiveBufferSize = SIZE_TABLE[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
public int nextReceiveBufferSize() {
|
public int nextReceiveBufferSize() {
|
||||||
@ -84,16 +160,18 @@ public class DefaultReceiveBufferSizePredictor implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void previousReceiveBufferSize(int previousReceiveBufferSize) {
|
public void previousReceiveBufferSize(int previousReceiveBufferSize) {
|
||||||
if (previousReceiveBufferSize < nextReceiveBufferSize >>> 1) {
|
if (previousReceiveBufferSize <= SIZE_TABLE[Math.max(0, index - 2)]) {
|
||||||
if (shouldHalveNow) {
|
if (decreaseNow) {
|
||||||
nextReceiveBufferSize = Math.max(minimum, nextReceiveBufferSize >>> 1);
|
index = Math.max(index - INDEX_DECREMENT, minIndex);
|
||||||
shouldHalveNow = false;
|
nextReceiveBufferSize = SIZE_TABLE[index];
|
||||||
|
decreaseNow = false;
|
||||||
} else {
|
} else {
|
||||||
shouldHalveNow = true;
|
decreaseNow = true;
|
||||||
}
|
}
|
||||||
} else if (previousReceiveBufferSize >= nextReceiveBufferSize) {
|
} else if (previousReceiveBufferSize >= nextReceiveBufferSize) {
|
||||||
nextReceiveBufferSize = Math.min(maximum, nextReceiveBufferSize << 1);
|
index = Math.min(index + INDEX_INCREMENT, maxIndex);
|
||||||
shouldHalveNow = false;
|
nextReceiveBufferSize = SIZE_TABLE[index];
|
||||||
|
decreaseNow = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user