Reduce even more garbage by exposing ByteBuf.internalNioBuffer()

This commit is contained in:
Trustin Lee 2013-06-13 12:40:26 +09:00
parent 5131c024fa
commit 283feda119
18 changed files with 92 additions and 57 deletions

View File

@ -950,6 +950,11 @@ public abstract class AbstractByteBuf implements ByteBuf {
return nioBuffers(readerIndex, readableBytes()); return nioBuffers(readerIndex, readableBytes());
} }
@Override
public ByteBuffer nioBuffer(int index, int length) {
return internalNioBuffer(index, length).slice();
}
@Override @Override
public String toString(Charset charset) { public String toString(Charset charset) {
return toString(readerIndex, readableBytes(), charset); return toString(readerIndex, readableBytes(), charset);

View File

@ -1784,6 +1784,11 @@ public interface ByteBuf extends ReferenceCounted, Comparable<ByteBuf> {
*/ */
ByteBuffer nioBuffer(int index, int length); ByteBuffer nioBuffer(int index, int length);
/**
* Internal use only: Exposes the internal NIO buffer.
*/
ByteBuffer internalNioBuffer(int index, int length);
/** /**
* Exposes this buffer's readable bytes as an NIO {@link ByteBuffer}'s. The returned buffer * Exposes this buffer's readable bytes as an NIO {@link ByteBuffer}'s. The returned buffer
* shares the content with this buffer, while changing the position and limit of the returned * shares the content with this buffer, while changing the position and limit of the returned

View File

@ -991,9 +991,9 @@ public class DefaultCompositeByteBuf extends AbstractReferenceCountedByteBuf imp
} }
@Override @Override
public ByteBuffer nioBuffer(int index, int length) { public ByteBuffer internalNioBuffer(int index, int length) {
if (components.size() == 1) { if (components.size() == 1) {
return components.get(0).buf.nioBuffer(index, length); return components.get(0).buf.internalNioBuffer(index, length);
} }
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }

View File

@ -282,14 +282,14 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf {
return buffer.nioBufferCount(); return buffer.nioBufferCount();
} }
@Override
public ByteBuffer nioBuffer(int index, int length) {
return buffer.nioBuffer(index, length);
}
@Override @Override
public ByteBuffer[] nioBuffers(int index, int length) { public ByteBuffer[] nioBuffers(int index, int length) {
return buffer.nioBuffers(index, length); return buffer.nioBuffers(index, length);
} }
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
return buffer.internalNioBuffer(index, length);
}
} }

View File

@ -760,6 +760,11 @@ public final class EmptyByteBuf implements ByteBuf {
return nioBuffers(); return nioBuffers();
} }
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
return EMPTY_BYTE_BUFFER;
}
@Override @Override
public boolean hasArray() { public boolean hasArray() {
return true; return true;

View File

@ -257,15 +257,15 @@ final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
} }
@Override @Override
public ByteBuffer nioBuffer(int index, int length) { public ByteBuffer[] nioBuffers(int index, int length) {
checkIndex(index, length); return new ByteBuffer[] { nioBuffer(index, length) };
index = idx(index);
return ((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length)).slice();
} }
@Override @Override
public ByteBuffer[] nioBuffers(int index, int length) { public ByteBuffer internalNioBuffer(int index, int length) {
return new ByteBuffer[] { nioBuffer(index, length) }; checkIndex(index, length);
index = idx(index);
return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
} }
@Override @Override

View File

@ -232,15 +232,15 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
} }
@Override @Override
public ByteBuffer nioBuffer(int index, int length) { public ByteBuffer[] nioBuffers(int index, int length) {
checkIndex(index, length); return new ByteBuffer[] { nioBuffer(index, length) };
index = idx(index);
return ((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length)).slice();
} }
@Override @Override
public ByteBuffer[] nioBuffers(int index, int length) { public ByteBuffer internalNioBuffer(int index, int length) {
return new ByteBuffer[] { nioBuffer(index, length) }; checkIndex(index, length);
index = idx(index);
return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
} }
@Override @Override

View File

@ -294,15 +294,15 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
} }
@Override @Override
public ByteBuffer nioBuffer(int index, int length) { public ByteBuffer[] nioBuffers(int index, int length) {
checkIndex(index, length); return new ByteBuffer[] { nioBuffer(index, length) };
index = idx(index);
return ((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length)).slice();
} }
@Override @Override
public ByteBuffer[] nioBuffers(int index, int length) { public ByteBuffer internalNioBuffer(int index, int length) {
return new ByteBuffer[] { nioBuffer(index, length) }; checkIndex(index, length);
index = idx(index);
return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
} }
@Override @Override

View File

@ -290,6 +290,11 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf {
return buffer.nioBuffers(index, length); return buffer.nioBuffers(index, length);
} }
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
return buffer.internalNioBuffer(index, length);
}
@Override @Override
public int capacity() { public int capacity() {
return buffer.capacity(); return buffer.capacity();

View File

@ -278,18 +278,14 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {
} }
@Override @Override
public ByteBuffer nioBuffer(int index, int length) { public ByteBuffer[] nioBuffers(int index, int length) {
ensureAccessible(); return new ByteBuffer[] { nioBuffer(index, length) };
if (index == 0 && length == capacity()) {
return buffer.duplicate();
} else {
return ((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length)).slice();
}
} }
@Override @Override
public ByteBuffer[] nioBuffers(int index, int length) { public ByteBuffer internalNioBuffer(int index, int length) {
return new ByteBuffer[] { nioBuffer(index, length) }; ensureAccessible();
return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
} }
@Override @Override

View File

@ -267,4 +267,10 @@ public class SlicedByteBuf extends AbstractDerivedByteBuf {
checkIndex(index, length); checkIndex(index, length);
return buffer.nioBuffers(index + adjustment, length); return buffer.nioBuffers(index + adjustment, length);
} }
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
checkIndex(index, length);
return buffer.internalNioBuffer(index + adjustment, length);
}
} }

View File

@ -755,6 +755,12 @@ public final class SwappedByteBuf implements ByteBuf {
return buf.nioBuffer(index, length).order(order); return buf.nioBuffer(index, length).order(order);
} }
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
// Do not mess with the internal buffer's byte order.
return buf.nioBuffer(index, length).order(order);
}
@Override @Override
public ByteBuffer[] nioBuffers() { public ByteBuffer[] nioBuffers() {
ByteBuffer[] nioBuffers = buf.nioBuffers(); ByteBuffer[] nioBuffers = buf.nioBuffers();

View File

@ -470,16 +470,6 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
return 1; return 1;
} }
@Override
public ByteBuffer nioBuffer(int index, int length) {
ensureAccessible();
if (index == 0 && length == capacity()) {
return buffer.duplicate();
} else {
return ((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length)).slice();
}
}
@Override @Override
public ByteBuffer[] nioBuffers(int index, int length) { public ByteBuffer[] nioBuffers(int index, int length) {
return new ByteBuffer[] { nioBuffer(index, length) }; return new ByteBuffer[] { nioBuffer(index, length) };
@ -503,6 +493,11 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
return new UnpooledDirectByteBuf(alloc(), dst, maxCapacity()); return new UnpooledDirectByteBuf(alloc(), dst, maxCapacity());
} }
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
}
private ByteBuffer internalNioBuffer() { private ByteBuffer internalNioBuffer() {
ByteBuffer tmpNioBuf = this.tmpNioBuf; ByteBuffer tmpNioBuf = this.tmpNioBuf;
if (tmpNioBuf == null) { if (tmpNioBuf == null) {

View File

@ -256,6 +256,11 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
return new ByteBuffer[] { nioBuffer(index, length) }; return new ByteBuffer[] { nioBuffer(index, length) };
} }
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
}
@Override @Override
public byte getByte(int index) { public byte getByte(int index) {
ensureAccessible(); ensureAccessible();

View File

@ -409,16 +409,6 @@ public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf
return 1; return 1;
} }
@Override
public ByteBuffer nioBuffer(int index, int length) {
ensureAccessible();
if (index == 0 && length == capacity()) {
return buffer.duplicate();
} else {
return ((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length)).slice();
}
}
@Override @Override
public ByteBuffer[] nioBuffers(int index, int length) { public ByteBuffer[] nioBuffers(int index, int length) {
return new ByteBuffer[] { nioBuffer(index, length) }; return new ByteBuffer[] { nioBuffer(index, length) };
@ -435,6 +425,11 @@ public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf
return copy; return copy;
} }
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
}
private ByteBuffer internalNioBuffer() { private ByteBuffer internalNioBuffer() {
ByteBuffer tmpNioBuf = this.tmpNioBuf; ByteBuffer tmpNioBuf = this.tmpNioBuf;
if (tmpNioBuf == null) { if (tmpNioBuf == null) {

View File

@ -771,6 +771,11 @@ final class UnreleasableByteBuf implements ByteBuf {
return buf.nioBuffers(index, length); return buf.nioBuffers(index, length);
} }
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
return buf.internalNioBuffer(index, length);
}
@Override @Override
public boolean hasArray() { public boolean hasArray() {
return buf.hasArray(); return buf.hasArray();

View File

@ -729,6 +729,12 @@ final class ReplayingDecoderBuffer implements ByteBuf {
return buffer.nioBuffers(index, length); return buffer.nioBuffers(index, length);
} }
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
checkIndex(index, length);
return buffer.internalNioBuffer(index, length);
}
@Override @Override
public String toString(int index, int length, Charset charset) { public String toString(int index, int length, Charset charset) {
checkIndex(index, length); checkIndex(index, length);

View File

@ -280,6 +280,7 @@ public class NioSocketChannel extends AbstractNioByteChannel implements io.netty
long expectedWrittenBytes = 0; long expectedWrittenBytes = 0;
for (int i = index; i < size; i++) { for (int i = index; i < size; i++) {
ByteBuf buf = bufs.get(i); ByteBuf buf = bufs.get(i);
int readerIndex = buf.readerIndex();
int readableBytes = buf.readableBytes(); int readableBytes = buf.readableBytes();
expectedWrittenBytes += readableBytes; expectedWrittenBytes += readableBytes;
@ -289,7 +290,7 @@ public class NioSocketChannel extends AbstractNioByteChannel implements io.netty
if (nioBufferCnt == nioBuffers.length) { if (nioBufferCnt == nioBuffers.length) {
nioBuffers = doubleNioBufferArray(nioBuffers, nioBufferCnt); nioBuffers = doubleNioBufferArray(nioBuffers, nioBufferCnt);
} }
nioBuffers[nioBufferCnt ++] = buf.nioBuffer(); nioBuffers[nioBufferCnt ++] = buf.internalNioBuffer(readerIndex, readableBytes);
} else { } else {
ByteBuffer[] nioBufs = buf.nioBuffers(); ByteBuffer[] nioBufs = buf.nioBuffers();
if (nioBufferCnt + nioBufs.length == nioBuffers.length + 1) { if (nioBufferCnt + nioBufs.length == nioBuffers.length + 1) {
@ -304,13 +305,13 @@ public class NioSocketChannel extends AbstractNioByteChannel implements io.netty
} }
} else { } else {
ByteBuf directBuf = alloc().directBuffer(readableBytes); ByteBuf directBuf = alloc().directBuffer(readableBytes);
directBuf.writeBytes(buf, buf.readerIndex(), readableBytes); directBuf.writeBytes(buf, readerIndex, readableBytes);
buf.release(); buf.release();
bufs.set(i, directBuf); bufs.set(i, directBuf);
if (nioBufferCnt == nioBuffers.length) { if (nioBufferCnt == nioBuffers.length) {
nioBuffers = doubleNioBufferArray(nioBuffers, nioBufferCnt); nioBuffers = doubleNioBufferArray(nioBuffers, nioBufferCnt);
} }
nioBuffers[nioBufferCnt ++] = directBuf.nioBuffer(); nioBuffers[nioBufferCnt ++] = directBuf.internalNioBuffer(0, readableBytes);
} }
} }