Split up the nioBuffers() method to allow for inline. Related to #1812
This move less common method patterns to extra methods and so make the nioBuffers() method with most common pattern (backed by one ByteBuffer) small enough for inlining.
This commit is contained in:
parent
d75897bb2d
commit
9331226406
@ -315,12 +315,11 @@ public final class ChannelOutboundBuffer {
|
|||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public ByteBuffer[] nioBuffers() {
|
public ByteBuffer[] nioBuffers() {
|
||||||
ByteBuffer[] nioBuffers = this.nioBuffers;
|
|
||||||
long nioBufferSize = 0;
|
long nioBufferSize = 0;
|
||||||
int nioBufferCount = 0;
|
int nioBufferCount = 0;
|
||||||
|
|
||||||
final int mask = buffer.length - 1;
|
final int mask = buffer.length - 1;
|
||||||
final ByteBufAllocator alloc = channel.alloc();
|
final ByteBufAllocator alloc = channel.alloc();
|
||||||
|
ByteBuffer[] nioBuffers = this.nioBuffers;
|
||||||
Object m;
|
Object m;
|
||||||
int i = flushed;
|
int i = flushed;
|
||||||
while (i != unflushed && (m = buffer[i].msg) != null) {
|
while (i != unflushed && (m = buffer[i].msg) != null) {
|
||||||
@ -331,53 +330,58 @@ public final class ChannelOutboundBuffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ByteBuf buf = (ByteBuf) m;
|
ByteBuf buf = (ByteBuf) m;
|
||||||
|
|
||||||
final int readerIndex = buf.readerIndex();
|
final int readerIndex = buf.readerIndex();
|
||||||
final int readableBytes = buf.writerIndex() - readerIndex;
|
final int readableBytes = buf.writerIndex() - readerIndex;
|
||||||
|
|
||||||
if (readableBytes > 0) {
|
if (readableBytes > 0) {
|
||||||
nioBufferSize += readableBytes;
|
nioBufferSize += readableBytes;
|
||||||
|
int count = buf.nioBufferCount();
|
||||||
|
if (nioBufferCount + count > nioBuffers.length) {
|
||||||
|
this.nioBuffers = nioBuffers = doubleNioBufferArray(nioBuffers, nioBufferCount);
|
||||||
|
}
|
||||||
if (buf.isDirect() || !alloc.isDirectBufferPooled()) {
|
if (buf.isDirect() || !alloc.isDirectBufferPooled()) {
|
||||||
int count = buf.nioBufferCount();
|
if (buf.nioBufferCount() == 1) {
|
||||||
if (count == 1) {
|
|
||||||
if (nioBufferCount == nioBuffers.length) {
|
|
||||||
this.nioBuffers = nioBuffers = doubleNioBufferArray(nioBuffers, nioBufferCount);
|
|
||||||
}
|
|
||||||
nioBuffers[nioBufferCount ++] = buf.internalNioBuffer(readerIndex, readableBytes);
|
nioBuffers[nioBufferCount ++] = buf.internalNioBuffer(readerIndex, readableBytes);
|
||||||
} else {
|
} else {
|
||||||
ByteBuffer[] nioBufs = buf.nioBuffers();
|
nioBufferCount = fillBufferArray(buf, nioBuffers, nioBufferCount);
|
||||||
if (nioBufferCount + nioBufs.length > nioBuffers.length) {
|
|
||||||
this.nioBuffers = nioBuffers = doubleNioBufferArray(nioBuffers, nioBufferCount);
|
|
||||||
}
|
|
||||||
for (ByteBuffer nioBuf: nioBufs) {
|
|
||||||
if (nioBuf == null) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
nioBuffers[nioBufferCount ++] = nioBuf;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ByteBuf directBuf = alloc.directBuffer(readableBytes);
|
nioBufferCount = fillBufferArrayNonDirect(buffer[i], buf, readerIndex,
|
||||||
directBuf.writeBytes(buf, readerIndex, readableBytes);
|
readableBytes, alloc, nioBuffers, nioBufferCount);
|
||||||
buf.release();
|
|
||||||
buffer[i].msg = directBuf;
|
|
||||||
if (nioBufferCount == nioBuffers.length) {
|
|
||||||
nioBuffers = doubleNioBufferArray(nioBuffers, nioBufferCount);
|
|
||||||
}
|
|
||||||
nioBuffers[nioBufferCount ++] = directBuf.internalNioBuffer(0, readableBytes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
i = i + 1 & mask;
|
i = i + 1 & mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.nioBufferCount = nioBufferCount;
|
this.nioBufferCount = nioBufferCount;
|
||||||
this.nioBufferSize = nioBufferSize;
|
this.nioBufferSize = nioBufferSize;
|
||||||
|
|
||||||
return nioBuffers;
|
return nioBuffers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int fillBufferArray(ByteBuf buf, ByteBuffer[] nioBuffers, int nioBufferCount) {
|
||||||
|
ByteBuffer[] nioBufs = buf.nioBuffers();
|
||||||
|
for (ByteBuffer nioBuf: nioBufs) {
|
||||||
|
if (nioBuf == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
nioBuffers[nioBufferCount ++] = nioBuf;
|
||||||
|
}
|
||||||
|
return nioBufferCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int fillBufferArrayNonDirect(Entry entry, ByteBuf buf, int readerIndex, int readableBytes,
|
||||||
|
ByteBufAllocator alloc, ByteBuffer[] nioBuffers, int nioBufferCount) {
|
||||||
|
ByteBuf directBuf = alloc.directBuffer(readableBytes);
|
||||||
|
directBuf.writeBytes(buf, readerIndex, readableBytes);
|
||||||
|
buf.release();
|
||||||
|
entry.msg = directBuf;
|
||||||
|
if (nioBufferCount == nioBuffers.length) {
|
||||||
|
nioBuffers = doubleNioBufferArray(nioBuffers, nioBufferCount);
|
||||||
|
}
|
||||||
|
nioBuffers[nioBufferCount ++] = directBuf.internalNioBuffer(0, readableBytes);
|
||||||
|
return nioBufferCount;
|
||||||
|
}
|
||||||
|
|
||||||
private static ByteBuffer[] doubleNioBufferArray(ByteBuffer[] array, int size) {
|
private static ByteBuffer[] doubleNioBufferArray(ByteBuffer[] array, int size) {
|
||||||
int newCapacity = array.length << 1;
|
int newCapacity = array.length << 1;
|
||||||
if (newCapacity < 0) {
|
if (newCapacity < 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user