Move calculateNewCapacity(...) to ByteBufAllocator
Motivation: Currently we have the algorithm of calculate the new capacity of a ByteBuf implemented in AbstractByteBuf. The problem with this is that it is impossible for a user to change it if it not fits well it's use-case. We should better move it to ByteBufAllocator and so let the user implement it's own by either write his/her own ByteBufAllocator or just override the default implementation in one of our provided ByteBufAllocators. Modifications: Move calculateNewCapacity(...) to ByteBufAllocator and move the implementation (which was part of AbstractByteBuf) to AbstractByteBufAllocator. Result: The user can now override the default calculation algorithm when needed.
This commit is contained in:
parent
375da788e7
commit
ad86ec798d
@ -245,7 +245,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Normalize the current capacity to the power of 2.
|
// Normalize the current capacity to the power of 2.
|
||||||
int newCapacity = calculateNewCapacity(writerIndex + minWritableBytes);
|
int newCapacity = alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity);
|
||||||
|
|
||||||
// Adjust to the new capacity.
|
// Adjust to the new capacity.
|
||||||
capacity(newCapacity);
|
capacity(newCapacity);
|
||||||
@ -275,41 +275,13 @@ public abstract class AbstractByteBuf extends ByteBuf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Normalize the current capacity to the power of 2.
|
// Normalize the current capacity to the power of 2.
|
||||||
int newCapacity = calculateNewCapacity(writerIndex + minWritableBytes);
|
int newCapacity = alloc().calculateNewCapacity(writerIndex + minWritableBytes, maxCapacity);
|
||||||
|
|
||||||
// Adjust to the new capacity.
|
// Adjust to the new capacity.
|
||||||
capacity(newCapacity);
|
capacity(newCapacity);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int calculateNewCapacity(int minNewCapacity) {
|
|
||||||
final int maxCapacity = this.maxCapacity;
|
|
||||||
final int threshold = 1048576 * 4; // 4 MiB page
|
|
||||||
|
|
||||||
if (minNewCapacity == threshold) {
|
|
||||||
return threshold;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If over threshold, do not double but just increase by threshold.
|
|
||||||
if (minNewCapacity > threshold) {
|
|
||||||
int newCapacity = minNewCapacity / threshold * threshold;
|
|
||||||
if (newCapacity > maxCapacity - threshold) {
|
|
||||||
newCapacity = maxCapacity;
|
|
||||||
} else {
|
|
||||||
newCapacity += threshold;
|
|
||||||
}
|
|
||||||
return newCapacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not over threshold. Double up to 4 MiB, starting from 64.
|
|
||||||
int newCapacity = 64;
|
|
||||||
while (newCapacity < minNewCapacity) {
|
|
||||||
newCapacity <<= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Math.min(newCapacity, maxCapacity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuf order(ByteOrder endianness) {
|
public ByteBuf order(ByteOrder endianness) {
|
||||||
if (endianness == null) {
|
if (endianness == null) {
|
||||||
|
@ -216,4 +216,40 @@ public abstract class AbstractByteBufAllocator implements ByteBufAllocator {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return StringUtil.simpleClassName(this) + "(directByDefault: " + directByDefault + ')';
|
return StringUtil.simpleClassName(this) + "(directByDefault: " + directByDefault + ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int calculateNewCapacity(int minNewCapacity, int maxCapacity) {
|
||||||
|
if (minNewCapacity < 0) {
|
||||||
|
throw new IllegalArgumentException("minNewCapacity: " + minNewCapacity + " (expectd: 0+)");
|
||||||
|
}
|
||||||
|
if (minNewCapacity > maxCapacity) {
|
||||||
|
throw new IllegalArgumentException(String.format(
|
||||||
|
"minNewCapacity: %d (expected: not greater than maxCapacity(%d)",
|
||||||
|
minNewCapacity, maxCapacity));
|
||||||
|
}
|
||||||
|
final int threshold = 1048576 * 4; // 4 MiB page
|
||||||
|
|
||||||
|
if (minNewCapacity == threshold) {
|
||||||
|
return threshold;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If over threshold, do not double but just increase by threshold.
|
||||||
|
if (minNewCapacity > threshold) {
|
||||||
|
int newCapacity = minNewCapacity / threshold * threshold;
|
||||||
|
if (newCapacity > maxCapacity - threshold) {
|
||||||
|
newCapacity = maxCapacity;
|
||||||
|
} else {
|
||||||
|
newCapacity += threshold;
|
||||||
|
}
|
||||||
|
return newCapacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not over threshold. Double up to 4 MiB, starting from 64.
|
||||||
|
int newCapacity = 64;
|
||||||
|
while (newCapacity < minNewCapacity) {
|
||||||
|
newCapacity <<= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.min(newCapacity, maxCapacity);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,4 +125,10 @@ public interface ByteBufAllocator {
|
|||||||
* Returns {@code true} if direct {@link ByteBuf}'s are pooled
|
* Returns {@code true} if direct {@link ByteBuf}'s are pooled
|
||||||
*/
|
*/
|
||||||
boolean isDirectBufferPooled();
|
boolean isDirectBufferPooled();
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* Calculate the new capacity of a {@link ByteBuf} that is used when a {@link ByteBuf} needs to expand by the
|
||||||
|
* {@code minNewCapacity} with {@code maxCapacity} as upper-bound.
|
||||||
|
*/
|
||||||
|
int calculateNewCapacity(int minNewCapacity, int maxCapacity);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user