Skip to content

Conversation

stepancheg
Copy link
Contributor

Alternative version to #791.

src/bytes_mut.rs Outdated
self.extend_from_slice(s);
src.advance(l);
if self.capacity() == 0 {
let src = src.copy_to_bytes(src.remaining());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should drop src before calling try_into_mut().

Suggested change
let src = src.copy_to_bytes(src.remaining());
let src_copy = src.copy_to_bytes(src.remaining());
drop(src);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Typical code looks like this:

```
let mut bytes = BytesMut::new();
for chunk in chunks {
    bytes.put(chunk);
}
```

Avoid allocation on the first chunk. This is especially important
when there's only one chunk, which is common for short HTTP responses
for example.
@stepancheg stepancheg force-pushed the bytes-mut-put-bytes-alt branch from 6a1eeae to 2860505 Compare August 14, 2025 08:38
@stepancheg stepancheg requested a review from Darksonn August 14, 2025 08:39
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@Darksonn Darksonn enabled auto-merge (squash) August 14, 2025 08:42
@Darksonn Darksonn merged commit ef7f257 into tokio-rs:master Aug 14, 2025
18 checks passed
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