Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduced reallocations #153

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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