From 6a8c364d57df6dedbcd10054ab32682a34e01f9e Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Mon, 23 Dec 2024 17:01:23 +0100 Subject: [PATCH] `deflate::Window`: cleanup unused fields --- zlib-rs/src/deflate.rs | 9 --------- zlib-rs/src/deflate/window.rs | 35 ++++------------------------------- 2 files changed, 4 insertions(+), 40 deletions(-) diff --git a/zlib-rs/src/deflate.rs b/zlib-rs/src/deflate.rs index 73be115..44af6cf 100644 --- a/zlib-rs/src/deflate.rs +++ b/zlib-rs/src/deflate.rs @@ -1636,7 +1636,6 @@ pub(crate) fn read_buf_window(stream: &mut DeflateStream, offset: usize, size: u // we likely cannot fuse the crc32 and the copy here because the input can be changed by // a concurrent thread. Therefore it cannot be converted into a slice! let window = &mut stream.state.window; - window.initialize_at_least(offset + len); // SAFETY: len is bounded by avail_in, so this copy is in bounds. unsafe { window.copy_and_initialize(offset..offset + len, stream.next_in) }; @@ -1646,7 +1645,6 @@ pub(crate) fn read_buf_window(stream: &mut DeflateStream, offset: usize, size: u // we likely cannot fuse the adler32 and the copy here because the input can be changed by // a concurrent thread. Therefore it cannot be converted into a slice! let window = &mut stream.state.window; - window.initialize_at_least(offset + len); // SAFETY: len is bounded by avail_in, so this copy is in bounds. unsafe { window.copy_and_initialize(offset..offset + len, stream.next_in) }; @@ -1654,7 +1652,6 @@ pub(crate) fn read_buf_window(stream: &mut DeflateStream, offset: usize, size: u stream.adler = adler32(stream.adler as u32, data) as _; } else { let window = &mut stream.state.window; - window.initialize_at_least(offset + len); // SAFETY: len is bounded by avail_in, so this copy is in bounds. unsafe { window.copy_and_initialize(offset..offset + len, stream.next_in) }; } @@ -1728,7 +1725,6 @@ pub(crate) fn fill_window(stream: &mut DeflateStream) { // explicitly initialize them with zeros. // // see also the "fill_window_out_of_bounds" test. - state.window.initialize_at_least(2 * wsize); state.window.filled_mut().copy_within(wsize..2 * wsize, 0); if state.match_start as usize >= wsize { @@ -1797,11 +1793,6 @@ pub(crate) fn fill_window(stream: &mut DeflateStream) { } } - // initialize some memory at the end of the (filled) window, so SIMD operations can go "out of - // bounds" without violating any requirements. The window allocation is already slightly bigger - // to allow for this. - stream.state.window.initialize_out_of_bounds(); - assert!( stream.state.strstart <= stream.state.window_size - MIN_LOOKAHEAD, "not enough room for search" diff --git a/zlib-rs/src/deflate/window.rs b/zlib-rs/src/deflate/window.rs index b935007..3002624 100644 --- a/zlib-rs/src/deflate/window.rs +++ b/zlib-rs/src/deflate/window.rs @@ -6,12 +6,7 @@ pub struct Window<'a> { // perform bounds checks. buf: WeakSliceMut<'a, u8>, - // number of initialized bytes - filled: usize, - window_bits: usize, - - high_water: usize, } impl<'a> Window<'a> { @@ -21,12 +16,7 @@ impl<'a> Window<'a> { // SAFETY: freshly allocated buffer let buf = unsafe { WeakSliceMut::from_raw_parts_mut(ptr, len) }; - Some(Self { - buf, - filled: len, - window_bits, - high_water: len, - }) + Some(Self { buf, window_bits }) } pub fn clone_in(&self, alloc: &Allocator<'a>) -> Option { @@ -36,8 +26,6 @@ impl<'a> Window<'a> { .buf .as_mut_slice() .copy_from_slice(self.buf.as_slice()); - clone.filled = self.filled; - clone.high_water = self.high_water; Some(clone) } @@ -60,14 +48,14 @@ impl<'a> Window<'a> { #[inline] pub fn filled(&self) -> &[u8] { // SAFETY: `self.buf` has been initialized for at least `filled` elements - unsafe { core::slice::from_raw_parts(self.buf.as_ptr().cast(), self.filled) } + unsafe { core::slice::from_raw_parts(self.buf.as_ptr().cast(), self.buf.len()) } } /// Returns a mutable reference to the filled portion of the buffer. #[inline] pub fn filled_mut(&mut self) -> &mut [u8] { // SAFETY: `self.buf` has been initialized for at least `filled` elements - unsafe { core::slice::from_raw_parts_mut(self.buf.as_mut_ptr().cast(), self.filled) } + unsafe { core::slice::from_raw_parts_mut(self.buf.as_mut_ptr().cast(), self.buf.len()) } } /// # Safety @@ -76,25 +64,10 @@ impl<'a> Window<'a> { pub unsafe fn copy_and_initialize(&mut self, range: core::ops::Range, src: *const u8) { let (start, end) = (range.start, range.end); - let dst = self.buf.as_mut_slice()[range].as_mut_ptr() as *mut u8; + let dst = self.buf.as_mut_slice()[range].as_mut_ptr(); unsafe { core::ptr::copy_nonoverlapping(src, dst, end - start) }; - - if start >= self.filled { - self.filled = Ord::max(self.filled, end); - } - - self.high_water = Ord::max(self.high_water, self.filled); } - // this library has many functions that operated in a chunked fashion on memory. For - // performance, we want to minimize bounds checks. Therefore we reserve initialize some extra - // memory at the end of the window so that chunked operations can use the whole buffer. If they - // go slightly over `self.capacity` that's okay, we account for that here by making sure the - // memory there is initialized! - pub fn initialize_out_of_bounds(&mut self) {} - - pub fn initialize_at_least(&mut self, at_least: usize) {} - // padding required so that SIMD operations going out-of-bounds are not a problem pub fn padding() -> usize { if crate::cpu_features::is_enabled_pclmulqdq() {