From ece2c1adf2ca0201868c4e36c69a5f0d8eaf8490 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sat, 26 Apr 2025 21:10:27 +0200 Subject: [PATCH] Preallocate incoming body --- crates/lune-std-net/src/shared/response.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/lune-std-net/src/shared/response.rs b/crates/lune-std-net/src/shared/response.rs index 9332479..df3a183 100644 --- a/crates/lune-std-net/src/shared/response.rs +++ b/crates/lune-std-net/src/shared/response.rs @@ -2,7 +2,7 @@ use futures_lite::prelude::*; use http_body_util::BodyStream; use hyper::{ - body::{Bytes, Incoming}, + body::{Body, Bytes, Incoming}, HeaderMap, Response as HyperResponse, }; @@ -23,8 +23,10 @@ impl Response { ) -> LuaResult { let (parts, body) = incoming.into_parts(); + let size = body.size_hint().lower() as usize; + let buffer = Vec::::with_capacity(size); let body = BodyStream::new(body) - .try_fold(Vec::::new(), |mut body, chunk| { + .try_fold(buffer, |mut body, chunk| { if let Some(chunk) = chunk.data_ref() { body.extend_from_slice(chunk); }