perf(chunked-upload): hoist 512KB read buffer out of per-chunk loop

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.
This commit is contained in:
Dionisio
2026-02-24 13:03:18 +01:00
parent cd5b937065
commit 78ae145af5
@@ -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