Specialize BytesMut::put::<Bytes> (#793)

This commit is contained in:
Stepan Koltsov
2025-08-14 08:55:18 +00:00
committed by GitHub
parent 8b4f54d0f3
commit ef7f25736c
2 changed files with 34 additions and 5 deletions
+15 -5
View File
@@ -1201,11 +1201,21 @@ unsafe impl BufMut for BytesMut {
where
Self: Sized,
{
while src.has_remaining() {
let s = src.chunk();
let l = s.len();
self.extend_from_slice(s);
src.advance(l);
// When capacity is zero, try reusing allocation of `src`.
if self.capacity() == 0 {
let src_copy = src.copy_to_bytes(src.remaining());
drop(src);
match src_copy.try_into_mut() {
Ok(bytes_mut) => *self = bytes_mut,
Err(bytes) => self.extend_from_slice(&bytes),
}
} else {
while src.has_remaining() {
let s = src.chunk();
let l = s.len();
self.extend_from_slice(s);
src.advance(l);
}
}
}