Skip to content

Commit baa5053

Browse files
Reuse capacity when possible in <BytesMut as Buf>::advance impl (#698)
1 parent ce09d7d commit baa5053

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/bytes_mut.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,14 @@ impl Buf for BytesMut {
10661066

10671067
#[inline]
10681068
fn advance(&mut self, cnt: usize) {
1069+
// Advancing by the length is the same as resetting the length to 0,
1070+
// except this way we get to reuse the full capacity.
1071+
if cnt == self.remaining() {
1072+
// SAFETY: Zero is not greater than the capacity.
1073+
unsafe { self.set_len(0) };
1074+
return;
1075+
}
1076+
10691077
assert!(
10701078
cnt <= self.remaining(),
10711079
"cannot advance past `remaining`: {:?} <= {:?}",

0 commit comments

Comments
 (0)