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:
@@ -651,12 +651,13 @@ impl ChunkedUploadService {
|
|||||||
let mut hasher = Sha256::new();
|
let mut hasher = Sha256::new();
|
||||||
|
|
||||||
// Stream each chunk into the assembled file + hash
|
// 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 {
|
for chunk in &session.chunks {
|
||||||
let chunk_path = session.temp_dir.join(format!("chunk_{:06}", chunk.index));
|
let chunk_path = session.temp_dir.join(format!("chunk_{:06}", chunk.index));
|
||||||
let mut chunk_file = File::open(&chunk_path)
|
let mut chunk_file = File::open(&chunk_path)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("Failed to open chunk {}: {}", chunk.index, e))?;
|
.map_err(|e| format!("Failed to open chunk {}: {}", chunk.index, e))?;
|
||||||
let mut buf = vec![0u8; 524_288];
|
|
||||||
loop {
|
loop {
|
||||||
let n = tokio::io::AsyncReadExt::read(&mut chunk_file, &mut buf)
|
let n = tokio::io::AsyncReadExt::read(&mut chunk_file, &mut buf)
|
||||||
.await
|
.await
|
||||||
|
|||||||
Reference in New Issue
Block a user