1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-12-01 02:54:36 +01:00

Add buffer heuristic

This commit is contained in:
asonix 2024-05-19 16:34:43 -05:00
parent 8eb1d10bae
commit 42bd5eebdb

View File

@ -429,7 +429,11 @@ impl TransferEncoding {
match self.kind { match self.kind {
TransferEncodingKind::Eof => { TransferEncodingKind::Eof => {
let eof = msg.is_empty(); let eof = msg.is_empty();
if msg.len() > 1024 * 64 {
buf.put_bytes(msg); buf.put_bytes(msg);
} else {
buf.buffer_mut().extend_from_slice(&msg);
}
Ok(eof) Ok(eof)
} }
TransferEncodingKind::Chunked(ref mut eof) => { TransferEncodingKind::Chunked(ref mut eof) => {
@ -444,7 +448,12 @@ impl TransferEncoding {
writeln!(helpers::MutWriter(buf.buffer_mut()), "{:X}\r", msg.len()) writeln!(helpers::MutWriter(buf.buffer_mut()), "{:X}\r", msg.len())
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
if msg.len() > 1024 * 64 {
buf.put_bytes(msg); buf.put_bytes(msg);
} else {
buf.buffer_mut().reserve(msg.len() + 2);
buf.buffer_mut().extend_from_slice(&msg);
}
buf.buffer_mut().extend_from_slice(b"\r\n"); buf.buffer_mut().extend_from_slice(b"\r\n");
} }
Ok(*eof) Ok(*eof)
@ -456,7 +465,11 @@ impl TransferEncoding {
} }
let len = cmp::min(*remaining, msg.len() as u64); let len = cmp::min(*remaining, msg.len() as u64);
if len > 1024 * 64 {
buf.put_bytes(msg.slice(..len as usize)); buf.put_bytes(msg.slice(..len as usize));
} else {
buf.buffer_mut().extend_from_slice(&msg[..len as usize]);
}
*remaining -= len; *remaining -= len;
Ok(*remaining == 0) Ok(*remaining == 0)