Skip to content

Conversation

@camshaft
Copy link
Contributor

@camshaft camshaft commented Nov 29, 2023

Description of changes:

For an application that needs to copy data out into, for example, a tokio::io::ReadBuf, the current ReceiveBuffer::pop interface is less than ideal. The way it is implemented, it assumes that the BytesMut will be passed around for copy-avoidance. Splitting bytes off from other chunks only to copy it and drop the new reference requires shared promotion and refcount increment/decrement.

This change, instead, provides a copy_into_buf function that avoids the split calls and instead uses BufMut::advance_mut, which avoids the previously-mentioned downsides.

Call-outs:

I updated the benchmarks to use the copy_into_buf function instead of pop to avoid oversampling on the pop, especially since the benchmarks just discard the received chunk.

Testing:

I've included a unit test showing it all working. Most of the implementation is shared with pop via the internal pop_transform method.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@camshaft camshaft marked this pull request as ready for review November 29, 2023 20:15
@camshaft camshaft merged commit 665b3e3 into main Dec 2, 2023
@camshaft camshaft deleted the camshaft/buffer-copy-buf branch December 2, 2023 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants