Skip to content

Commit

Permalink
Reuse stack-allocated Vec reading chunk
Browse files Browse the repository at this point in the history
This avoids building a new Vec chunk on each iteration
  • Loading branch information
tuffy committed Aug 21, 2024
1 parent 6d02c3e commit 3afa56c
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1641,11 +1641,12 @@ fn read_to_vec(
}
mut bytes => {
let mut whole = Vec::with_capacity(MAX_CHUNK);
let mut chunk: [u8; MAX_CHUNK] = [0; MAX_CHUNK];
while bytes > 0 {
let chunk_size = bytes.min(MAX_CHUNK);
let mut chunk = vec![0; chunk_size];
read(&mut chunk)?;
whole.extend(chunk);
let chunk = &mut chunk[0..chunk_size];
read(chunk)?;
whole.extend_from_slice(chunk);
bytes -= chunk_size;
}
Ok(whole)
Expand Down

0 comments on commit 3afa56c

Please sign in to comment.