From 4a4e63c30f6db5678f3e6033270a0894d421d39b Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 1 Mar 2024 22:10:40 +0100 Subject: [PATCH 1/3] Use individual boxed slices in HashBuffers --- miniz_oxide/src/deflate/buffer.rs | 19 ++++++++++++------- miniz_oxide/src/deflate/core.rs | 4 ++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/miniz_oxide/src/deflate/buffer.rs b/miniz_oxide/src/deflate/buffer.rs index f246c07d..110836c9 100644 --- a/miniz_oxide/src/deflate/buffer.rs +++ b/miniz_oxide/src/deflate/buffer.rs @@ -2,6 +2,9 @@ //! to avoid stack copies. Box::new() doesn't at the moment, and using a vec means we would lose //! static length info. +use alloc::boxed::Box; +use alloc::vec; + use crate::deflate::core::{LZ_DICT_SIZE, MAX_MATCH_LEN}; /// Size of the buffer of lz77 encoded data. @@ -23,24 +26,26 @@ pub fn update_hash(current_hash: u16, byte: u8) -> u16 { } pub struct HashBuffers { - pub dict: [u8; LZ_DICT_FULL_SIZE], - pub next: [u16; LZ_DICT_SIZE], - pub hash: [u16; LZ_DICT_SIZE], + pub dict: Box<[u8]>, + pub next: Box<[u16]>, + pub hash: Box<[u16]>, } impl HashBuffers { #[inline] pub fn reset(&mut self) { - *self = HashBuffers::default(); + self.dict.fill(0); + self.next.fill(0); + self.hash.fill(0); } } impl Default for HashBuffers { fn default() -> HashBuffers { HashBuffers { - dict: [0; LZ_DICT_FULL_SIZE], - next: [0; LZ_DICT_SIZE], - hash: [0; LZ_DICT_SIZE], + dict: vec![0; LZ_DICT_FULL_SIZE].into_boxed_slice(), + next: vec![0; LZ_DICT_SIZE].into_boxed_slice(), + hash: vec![0; LZ_DICT_SIZE].into_boxed_slice(), } } } diff --git a/miniz_oxide/src/deflate/core.rs b/miniz_oxide/src/deflate/core.rs index b0a532dc..79cdeb44 100644 --- a/miniz_oxide/src/deflate/core.rs +++ b/miniz_oxide/src/deflate/core.rs @@ -1195,7 +1195,7 @@ struct DictOxide { pub max_probes: [u32; 2], /// Buffer of input data. /// Padded with 1 byte to simplify matching code in `compress_fast`. - pub b: Box, + pub b: HashBuffers, pub code_buf_dict_pos: usize, pub lookahead_size: usize, @@ -1214,7 +1214,7 @@ impl DictOxide { fn new(flags: u32) -> Self { DictOxide { max_probes: probes_from_flags(flags), - b: Box::default(), + b: HashBuffers::default(), code_buf_dict_pos: 0, lookahead_size: 0, lookahead_pos: 0, From 4dfbc8deee4b2d85d4a1b2fff8dc001cb3c62f1c Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 3 Mar 2024 21:01:56 +0100 Subject: [PATCH 2/3] Use boxed slice in LocalBuf --- miniz_oxide/src/deflate/buffer.rs | 4 ++-- miniz_oxide/src/deflate/core.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/miniz_oxide/src/deflate/buffer.rs b/miniz_oxide/src/deflate/buffer.rs index 110836c9..6ec59c33 100644 --- a/miniz_oxide/src/deflate/buffer.rs +++ b/miniz_oxide/src/deflate/buffer.rs @@ -51,13 +51,13 @@ impl Default for HashBuffers { } pub struct LocalBuf { - pub b: [u8; OUT_BUF_SIZE], + pub b: Box<[u8]>, } impl Default for LocalBuf { fn default() -> LocalBuf { LocalBuf { - b: [0; OUT_BUF_SIZE], + b: vec![0; OUT_BUF_SIZE].into_boxed_slice(), } } } diff --git a/miniz_oxide/src/deflate/core.rs b/miniz_oxide/src/deflate/core.rs index 79cdeb44..87ea9891 100644 --- a/miniz_oxide/src/deflate/core.rs +++ b/miniz_oxide/src/deflate/core.rs @@ -1401,7 +1401,7 @@ struct ParamsOxide { pub saved_bit_buffer: u32, pub saved_bits_in: u32, - pub local_buf: Box, + pub local_buf: LocalBuf, } impl ParamsOxide { @@ -1423,7 +1423,7 @@ impl ParamsOxide { prev_return_status: TDEFLStatus::Okay, saved_bit_buffer: 0, saved_bits_in: 0, - local_buf: Box::default(), + local_buf: LocalBuf::default(), } } @@ -1448,7 +1448,7 @@ impl ParamsOxide { self.prev_return_status = TDEFLStatus::Okay; self.saved_bit_buffer = 0; self.saved_bits_in = 0; - self.local_buf.b = [0; OUT_BUF_SIZE]; + self.local_buf.b.fill(0); } } From 6f017019af911a922d622d6183b77c8e97b563fc Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 6 Mar 2024 18:49:00 +0100 Subject: [PATCH 3/3] Convert buffer slices into heap arrays --- miniz_oxide/src/deflate/buffer.rs | 20 ++++++++++++-------- miniz_oxide/src/deflate/core.rs | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/miniz_oxide/src/deflate/buffer.rs b/miniz_oxide/src/deflate/buffer.rs index 6ec59c33..64a6f839 100644 --- a/miniz_oxide/src/deflate/buffer.rs +++ b/miniz_oxide/src/deflate/buffer.rs @@ -4,6 +4,7 @@ use alloc::boxed::Box; use alloc::vec; +use core::convert::TryInto; use crate::deflate::core::{LZ_DICT_SIZE, MAX_MATCH_LEN}; @@ -26,9 +27,9 @@ pub fn update_hash(current_hash: u16, byte: u8) -> u16 { } pub struct HashBuffers { - pub dict: Box<[u8]>, - pub next: Box<[u16]>, - pub hash: Box<[u16]>, + pub dict: Box<[u8; LZ_DICT_FULL_SIZE]>, + pub next: Box<[u16; LZ_DICT_SIZE]>, + pub hash: Box<[u16; LZ_DICT_SIZE]>, } impl HashBuffers { @@ -43,21 +44,24 @@ impl HashBuffers { impl Default for HashBuffers { fn default() -> HashBuffers { HashBuffers { - dict: vec![0; LZ_DICT_FULL_SIZE].into_boxed_slice(), - next: vec![0; LZ_DICT_SIZE].into_boxed_slice(), - hash: vec![0; LZ_DICT_SIZE].into_boxed_slice(), + dict: vec![0; LZ_DICT_FULL_SIZE] + .into_boxed_slice() + .try_into() + .unwrap(), + next: vec![0; LZ_DICT_SIZE].into_boxed_slice().try_into().unwrap(), + hash: vec![0; LZ_DICT_SIZE].into_boxed_slice().try_into().unwrap(), } } } pub struct LocalBuf { - pub b: Box<[u8]>, + pub b: Box<[u8; OUT_BUF_SIZE]>, } impl Default for LocalBuf { fn default() -> LocalBuf { LocalBuf { - b: vec![0; OUT_BUF_SIZE].into_boxed_slice(), + b: vec![0; OUT_BUF_SIZE].into_boxed_slice().try_into().unwrap(), } } } diff --git a/miniz_oxide/src/deflate/core.rs b/miniz_oxide/src/deflate/core.rs index 87ea9891..a9d4e5e1 100644 --- a/miniz_oxide/src/deflate/core.rs +++ b/miniz_oxide/src/deflate/core.rs @@ -1628,7 +1628,7 @@ fn flush_block( { let mut output = callback .out - .new_output_buffer(&mut d.params.local_buf.b, d.params.out_buf_ofs); + .new_output_buffer(d.params.local_buf.b.as_mut(), d.params.out_buf_ofs); output.bit_buffer = d.params.saved_bit_buffer; output.bits_in = d.params.saved_bits_in;