CompositeByteBuf nioBuffer doesn't always alloc (#8176)

In nioBuffer(int,int) in CompositeByteBuf , we create a sub-array of nioBuffers for the components that are in range, then concatenate all the components in range into a single bigger buffer.
However, if the call to nioBuffers() returned only one sub-buffer, then we are copying it to a newly-allocated buffer "merged" for no reason.

Motivation:

Profiler for Spark shows a lot of time spent in put() method inside nioBuffer(), while usually no copy of data is required.

Modification:
This change skips this last step and just returns a duplicate of the single buffer returned by the call to nioBuffers(), which will in most implementation not copy the data

Result:
No copy when the source is only 1 buffer
This commit is contained in:
vincent-grosbois 2018-08-07 11:31:24 +02:00 committed by Norman Maurer
parent 55fec94592
commit 0bea8ecf5d

View File

@ -1467,9 +1467,13 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements
}
}
ByteBuffer merged = ByteBuffer.allocate(length).order(order());
ByteBuffer[] buffers = nioBuffers(index, length);
if (buffers.length == 1) {
return buffers[0].duplicate();
}
ByteBuffer merged = ByteBuffer.allocate(length).order(order());
for (ByteBuffer buf: buffers) {
merged.put(buf);
}