Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport to_bytes performance optimizations #121

Open
wants to merge 2 commits into
base: 0.4.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ impl<T: Buf> Buf for BufList<T> {
self.bufs.iter().map(|buf| buf.remaining()).sum()
}

#[inline]
fn has_remaining(&self) -> bool {
self.bufs.iter().any(|buf| buf.has_remaining())
}

#[inline]
fn chunk(&self) -> &[u8] {
self.bufs.front().map(Buf::chunk).unwrap_or_default()
Expand Down Expand Up @@ -204,9 +209,15 @@ impl<T: Buf> Buf for BufList<T> {
}
Some(front) if front.remaining() > len => front.copy_to_bytes(len),
_ => {
assert!(len <= self.remaining(), "`len` greater than remaining");
let rem = self.remaining();
assert!(len <= rem, "`len` greater than remaining");
let mut bm = BytesMut::with_capacity(len);
bm.put(self.take(len));
if rem == len {
// .take() costs a lot more, so skip it if we don't need it
bm.put(self);
} else {
bm.put(self.take(len));
}
bm.freeze()
}
}
Expand Down