Skip to content

Commit

Permalink
CompositeByteBuf nioBuffer doesn't always alloc (netty#8176)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vincent-grosbois authored and normanmaurer committed Aug 7, 2018
1 parent 55fec94 commit 0bea8ec
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -1467,9 +1467,13 @@ public ByteBuffer nioBuffer(int index, int length) {
}
}

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);
}
Expand Down

0 comments on commit 0bea8ec

Please sign in to comment.