Skip to content

Commit

Permalink
Reduced allocations (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao authored Jun 27, 2022
1 parent 077b02c commit 627e466
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/read/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ pub fn decompress_buffer(

// prepare the compression buffer
let read_size = compressed_page.uncompressed_size();
if read_size > buffer.len() {
// dealloc and ignore region, replacing it by a new region
*buffer = vec![0; read_size]
if read_size > buffer.capacity() {
// dealloc and ignore region, replacing it by a new region.
// This won't reallocate - it frees and calls `alloc_zeroed`
*buffer = vec![0; read_size];
} else if read_size > buffer.len() {
// fill what we need with zeros so that we can use them in `Read`.
// This won't reallocate
buffer.resize(read_size, 0);
} else {
buffer.truncate(read_size);
}
Expand Down
11 changes: 8 additions & 3 deletions src/read/page/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,14 @@ pub(super) fn build_page<R: Read>(

let read_size = page_header.compressed_page_size as usize;
if read_size > 0 {
if read_size > buffer.len() {
// dealloc and ignore region, replacing it by a new region
*buffer = vec![0; read_size]
if read_size > buffer.capacity() {
// dealloc and ignore region, replacing it by a new region.
// This won't reallocate - it frees and calls `alloc_zeroed`
*buffer = vec![0; read_size];
} else if read_size > buffer.len() {
// fill what we need with zeros so that we can use them in `Read`.
// This won't reallocate
buffer.resize(read_size, 0);
} else {
buffer.truncate(read_size);
}
Expand Down

0 comments on commit 627e466

Please sign in to comment.