Skip to content

Commit

Permalink
Reuse capacity when possible in <BytesMut as Buf>::advance impl
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobarbolini committed Apr 24, 2024
1 parent 4e2c9c0 commit 85d9c88
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/bytes_mut.rs
Expand Up @@ -1064,6 +1064,15 @@ impl Buf for BytesMut {
cnt,
self.remaining(),
);

// 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.len {
// SAFETY: Zero is not greater than the capacity.
unsafe { self.set_len(0) };
return;
}

unsafe {
// SAFETY: We've checked that `cnt` <= `self.remaining()` and we know that
// `self.remaining()` <= `self.cap`.
Expand Down

0 comments on commit 85d9c88

Please sign in to comment.