From 42bd5eebdb0b55ad388e4a064e653da98ca6028a Mon Sep 17 00:00:00 2001
From: asonix <asonix@asonix.dog>
Date: Sun, 19 May 2024 16:34:43 -0500
Subject: [PATCH] Add buffer heuristic

---
 actix-http/src/h1/encoder.rs | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/actix-http/src/h1/encoder.rs b/actix-http/src/h1/encoder.rs
index 97ba0adf..75aa8a82 100644
--- a/actix-http/src/h1/encoder.rs
+++ b/actix-http/src/h1/encoder.rs
@@ -429,7 +429,11 @@ impl TransferEncoding {
         match self.kind {
             TransferEncodingKind::Eof => {
                 let eof = msg.is_empty();
-                buf.put_bytes(msg);
+                if msg.len() > 1024 * 64 {
+                    buf.put_bytes(msg);
+                } else {
+                    buf.buffer_mut().extend_from_slice(&msg);
+                }
                 Ok(eof)
             }
             TransferEncodingKind::Chunked(ref mut eof) => {
@@ -444,7 +448,12 @@ impl TransferEncoding {
                     writeln!(helpers::MutWriter(buf.buffer_mut()), "{:X}\r", msg.len())
                         .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
 
-                    buf.put_bytes(msg);
+                    if msg.len() > 1024 * 64 {
+                        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");
                 }
                 Ok(*eof)
@@ -456,7 +465,11 @@ impl TransferEncoding {
                     }
                     let len = cmp::min(*remaining, msg.len() as u64);
 
-                    buf.put_bytes(msg.slice(..len as usize));
+                    if len > 1024 * 64 {
+                        buf.put_bytes(msg.slice(..len as usize));
+                    } else {
+                        buf.buffer_mut().extend_from_slice(&msg[..len as usize]);
+                    }
 
                     *remaining -= len;
                     Ok(*remaining == 0)