From a298540c7d8c1a98fb919172faa9b7dabf5905fb Mon Sep 17 00:00:00 2001 From: HaRoLd <303926+HarryR@users.noreply.github.com> Date: Sun, 5 Jul 2026 12:14:41 +0000 Subject: [PATCH] http: hand back response body without copying it Extract the HTTP body by reusing the response buffer's allocation (split_off) instead of copying the whole tail into a fresh Vec. On the payload download this drops a full-length memcpy and roughly halves peak memory for the transfer. The returned bytes are identical, so admission and measurement are unchanged. Co-Authored-By: Claude Opus 4.8 --- crates/stage0/src/http.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/stage0/src/http.rs b/crates/stage0/src/http.rs index 0d5636b..a0bb4e2 100644 --- a/crates/stage0/src/http.rs +++ b/crates/stage0/src/http.rs @@ -78,7 +78,7 @@ pub fn fetch( req.push_str("Connection: close\r\nUser-Agent: stage0\r\n\r\n"); crate::sdbg!("stage0: HTTP {} {url}", method.as_str()); - let raw = tcp4::exchange(ip, port, req.as_bytes())?; + let mut raw = tcp4::exchange(ip, port, req.as_bytes())?; let sep = find_subslice(&raw, b"\r\n\r\n").ok_or_else(|| { crate::slog!("stage0: response had no header terminator"); @@ -88,7 +88,9 @@ pub fn fetch( crate::slog!("stage0: could not parse HTTP status line"); Status::PROTOCOL_ERROR })?; - let body = raw[sep + 4..].to_vec(); + // Hand back the body by reusing `raw`'s allocation (drop the headers in place) + // instead of copying the whole payload into a fresh Vec. + let body = raw.split_off(sep + 4); crate::sdbg!("stage0: response {status}, {} body bytes", body.len()); Ok((status, body)) }