Skip to content

Commit

Permalink
deflate::Window: cleanup unused fields
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Jan 6, 2025
1 parent 891e313 commit 6a8c364
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 40 deletions.
9 changes: 0 additions & 9 deletions zlib-rs/src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) };

Expand All @@ -1646,15 +1645,13 @@ 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) };

let data = &stream.state.window.filled()[offset..][..len];
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) };
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down
35 changes: 4 additions & 31 deletions zlib-rs/src/deflate/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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<Self> {
Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -76,25 +64,10 @@ impl<'a> Window<'a> {
pub unsafe fn copy_and_initialize(&mut self, range: core::ops::Range<usize>, 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() {
Expand Down

0 comments on commit 6a8c364

Please sign in to comment.