aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Paolo Barbolini <paolo@paolo565.org> 2024-04-25 09:08:16 +0200
committerGravatar GitHub <noreply@github.com> 2024-04-25 09:08:16 +0200
commitbaa5053572ed9e88ca1058ec2b5a3f08046c5a40 (patch)
tree74eb353f18ae5a6d63ea8106d346b856c1d5b7a8 /src
parentce09d7d358ab1d1d31ed9d0b52a747c0a21ea401 (diff)
downloadbytes-baa5053572ed9e88ca1058ec2b5a3f08046c5a40.tar.gz
bytes-baa5053572ed9e88ca1058ec2b5a3f08046c5a40.tar.zst
bytes-baa5053572ed9e88ca1058ec2b5a3f08046c5a40.zip
Reuse capacity when possible in <BytesMut as Buf>::advance impl (#698)
Diffstat (limited to 'src')
-rw-r--r--src/bytes_mut.rs8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs
index 0ea0272..35e1900 100644
--- a/src/bytes_mut.rs
+++ b/src/bytes_mut.rs
@@ -1066,6 +1066,14 @@ impl Buf for BytesMut {
#[inline]
fn advance(&mut self, cnt: usize) {
+ // Advancing by the length is the same as resetting the length to 0,
+ // except this way we get to reuse the full capacity.
+ if cnt == self.remaining() {
+ // SAFETY: Zero is not greater than the capacity.
+ unsafe { self.set_len(0) };
+ return;
+ }
+
assert!(
cnt <= self.remaining(),
"cannot advance past `remaining`: {:?} <= {:?}",