From 78ae145af5745d7e481ccdc1c1e64f5e334e93b2 Mon Sep 17 00:00:00 2001 From: Dionisio Date: Tue, 24 Feb 2026 13:03:18 +0100 Subject: [PATCH] perf(chunked-upload): hoist 512KB read buffer out of per-chunk loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit causing N × 512KB alloc+memset+dealloc cycles per upload. Moving it before the loop reuses a single allocation across all chunks. For a 100-chunk upload this eliminates 99 allocations totalling ~50 MB of unnecessary memset work. --- src/infrastructure/services/chunked_upload_service.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/infrastructure/services/chunked_upload_service.rs b/src/infrastructure/services/chunked_upload_service.rs index e4fa885f..01d9eea7 100644 --- a/src/infrastructure/services/chunked_upload_service.rs +++ b/src/infrastructure/services/chunked_upload_service.rs @@ -651,12 +651,13 @@ impl ChunkedUploadService { let mut hasher = Sha256::new(); // Stream each chunk into the assembled file + hash + // Single 512 KB read buffer reused across all chunks (avoids N allocations) + let mut buf = vec![0u8; 524_288]; for chunk in &session.chunks { let chunk_path = session.temp_dir.join(format!("chunk_{:06}", chunk.index)); let mut chunk_file = File::open(&chunk_path) .await .map_err(|e| format!("Failed to open chunk {}: {}", chunk.index, e))?; - let mut buf = vec![0u8; 524_288]; loop { let n = tokio::io::AsyncReadExt::read(&mut chunk_file, &mut buf) .await