[#2573] UnpooledUnsafeDirectByteBuf.setBytes(int,ByteBuf,int,int) fails to use fast-path when src has array

Motivation:

UnpooledUnsafeDirectByteBuf.setBytes(int,ByteBuf,int,int) fails to use fast-path when src uses an array as backing storage. This is because the if else uses the wrong ByteBuf for its check.

Modifications:

- Use correct ByteBuf when check for array as backing storage
- Also eliminate unecessary check in UnpooledDirectByteBuf which always fails anyway

Result:

Faster setBytes(...) when src ByteBuf is backed by an array.

No more IndexOutOfBoundsException or data-corruption.
This commit is contained in:
Norman Maurer 2014-06-16 11:08:47 +02:00
parent a4bd566cef
commit 6fdf1138ca
2 changed files with 2 additions and 4 deletions

View File

@ -411,9 +411,7 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
@Override @Override
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
checkSrcIndex(index, length, srcIndex, src.capacity()); checkSrcIndex(index, length, srcIndex, src.capacity());
if (buffer.hasArray()) { if (src.nioBufferCount() > 0) {
src.getBytes(srcIndex, buffer.array(), index + buffer.arrayOffset(), length);
} else if (src.nioBufferCount() > 0) {
for (ByteBuffer bb: src.nioBuffers(srcIndex, length)) { for (ByteBuffer bb: src.nioBuffers(srcIndex, length)) {
int bbLen = bb.remaining(); int bbLen = bb.remaining();
setBytes(index, bb); setBytes(index, bb);

View File

@ -355,7 +355,7 @@ public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf
if (length != 0) { if (length != 0) {
if (src.hasMemoryAddress()) { if (src.hasMemoryAddress()) {
PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, addr(index), length); PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, addr(index), length);
} else if (buffer.hasArray()) { } else if (src.hasArray()) {
PlatformDependent.copyMemory(src.array(), src.arrayOffset() + srcIndex, addr(index), length); PlatformDependent.copyMemory(src.array(), src.arrayOffset() + srcIndex, addr(index), length);
} else { } else {
src.getBytes(srcIndex, this, index, length); src.getBytes(srcIndex, this, index, length);