From 705905ab4696ab12f788ca28757a1ff6de5f0411 Mon Sep 17 00:00:00 2001 From: "Jorge C. Leitao" Date: Sun, 26 Jun 2022 12:17:58 +0000 Subject: [PATCH] Reduced allocations --- src/read/compression.rs | 11 ++++++++--- src/read/page/reader.rs | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/read/compression.rs b/src/read/compression.rs index 002a6fa4a..9966a05e6 100644 --- a/src/read/compression.rs +++ b/src/read/compression.rs @@ -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); } diff --git a/src/read/page/reader.rs b/src/read/page/reader.rs index b67d71bc3..15252dbaf 100644 --- a/src/read/page/reader.rs +++ b/src/read/page/reader.rs @@ -197,9 +197,14 @@ pub(super) fn build_page( 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); }