Skip to content

Commit

Permalink
perf: improve BufList::copy_to_bytes if len == remaining
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Mar 11, 2024
1 parent 33ea848 commit 76b19e6
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions http-body-util/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,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 76b19e6

Please sign in to comment.