1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-16 21:26:34 +00:00

Don't reserve unless we need to

This commit is contained in:
asonix 2024-11-04 17:58:37 -06:00
parent 5e28e69e29
commit 36a7e8cc6a

View file

@ -171,21 +171,17 @@ impl Parser {
op.into() op.into()
}; };
let payload_len = pl.len(); let payload_len = pl.len();
let (two, p_len) = if mask { let two = if mask { 0x80 } else { 0 };
(0x80, payload_len + 4)
} else {
(0, payload_len)
};
if payload_len < 126 { if payload_len < 126 {
dst.buffer_mut().reserve(p_len + 2); dst.buffer_mut().reserve(2);
dst.buffer_mut().put_slice(&[one, two | payload_len as u8]); dst.buffer_mut().put_slice(&[one, two | payload_len as u8]);
} else if payload_len <= 65_535 { } else if payload_len <= 65_535 {
dst.buffer_mut().reserve(p_len + 4); dst.buffer_mut().reserve(4);
dst.buffer_mut().put_slice(&[one, two | 126]); dst.buffer_mut().put_slice(&[one, two | 126]);
dst.buffer_mut().put_u16(payload_len as u16); dst.buffer_mut().put_u16(payload_len as u16);
} else { } else {
dst.buffer_mut().reserve(p_len + 10); dst.buffer_mut().reserve(10);
dst.buffer_mut().put_slice(&[one, two | 127]); dst.buffer_mut().put_slice(&[one, two | 127]);
dst.buffer_mut().put_u64(payload_len as u64); dst.buffer_mut().put_u64(payload_len as u64);
}; };
@ -195,11 +191,16 @@ impl Parser {
dst.buffer_mut().put_slice(mask.as_ref()); dst.buffer_mut().put_slice(mask.as_ref());
match pl.try_into_mut() { match pl.try_into_mut() {
// Avoid copying bytes by mutating in-place
Ok(mut pl_mut) => { Ok(mut pl_mut) => {
apply_mask(&mut pl_mut, mask); apply_mask(&mut pl_mut, mask);
dst.put_bytes(pl_mut.freeze()); dst.put_bytes(pl_mut.freeze());
} }
// We need to copy the bytes anyway at this point, so put them in the buffer
// directly
Err(pl) => { Err(pl) => {
dst.buffer_mut().reserve(pl.len());
dst.buffer_mut().put_slice(pl.as_ref()); dst.buffer_mut().put_slice(pl.as_ref());
let pos = dst.buffer_mut().len() - payload_len; let pos = dst.buffer_mut().len() - payload_len;
apply_mask(&mut dst.buffer_mut()[pos..], mask); apply_mask(&mut dst.buffer_mut()[pos..], mask);