Skip to content

Commit

Permalink
backport: improve BufList::copy_to_bytes if len == remaining
Browse files Browse the repository at this point in the history
  • Loading branch information
rtimush committed May 14, 2024
1 parent a8913c5 commit 758d3e8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,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

0 comments on commit 758d3e8

Please sign in to comment.