rusty_zlib 0.1.3

Remade-With-Rust fork of zlib-rs: a memory-safe, pure-Rust zlib (deflate/inflate, zlib and gzip), measured and tuned for the MATA stack
Documentation
#![forbid(unsafe_code)]

use crate::deflate::{State, HASH_SIZE, STD_MIN_MATCH};
use crate::weak_slice::{WeakArrayMut, WeakSliceMut};

#[derive(Debug, Clone, Copy)]
pub enum HashCalcVariant {
    Standard,
    Roll,
}

impl HashCalcVariant {
    /// Use rolling hash for deflate_slow algorithm with level 9. It allows us to
    /// properly lookup different hash chains to speed up longest_match search.
    pub fn for_max_chain_length(max_chain_length: u16) -> Self {
        if max_chain_length > 1024 {
            HashCalcVariant::Roll
        } else {
            HashCalcVariant::Standard
        }
    }
}

pub struct StandardHashCalc;

impl StandardHashCalc {
    const HASH_CALC_OFFSET: usize = 0;

    const HASH_CALC_MASK: u32 = (HASH_SIZE - 1) as u32;

    fn hash_calc(_: u32, val: u32) -> u32 {
        const HASH_SLIDE: u32 = 16;
        val.wrapping_mul(2654435761) >> HASH_SLIDE
    }

    pub fn update_hash(h: u32, val: u32) -> u32 {
        Self::hash_calc(h, val) & Self::HASH_CALC_MASK
    }

    #[inline]
    pub fn quick_insert_string(state: &mut State, string: usize) -> u16 {
        let slice = &state.window.filled()[string + Self::HASH_CALC_OFFSET..];
        let val = u32::from_le_bytes(slice[..4].try_into().unwrap());

        Self::quick_insert_value(state, string, val)
    }

    #[inline]
    pub fn quick_insert_value(state: &mut State, string: usize, val: u32) -> u16 {
        Self::quick_insert_value_in(&mut state.head, &mut state.prev, string, val)
    }

    /// [`Self::quick_insert_value`] on the two tables only, so a caller can keep a borrow of
    /// the window (or any other field of `State`) alive across the insert.
    #[inline(always)]
    pub(crate) fn quick_insert_value_in(
        head: &mut WeakArrayMut<u16, HASH_SIZE>,
        prev: &mut WeakSliceMut<u16>,
        string: usize,
        val: u32,
    ) -> u16 {
        Self::quick_insert_value_slices(head.as_mut_slice(), prev.as_mut_slice(), string, val)
    }

    /// [`Self::quick_insert_value`] on materialized table slices, so a loop can hold them
    /// in registers instead of reloading the pointers from `State` after every store.
    /// `head` must be the `HASH_SIZE`-entry head table and `prev` the `w_size`-entry chain.
    #[inline(always)]
    pub(crate) fn quick_insert_value_slices(
        head: &mut [u16],
        prev: &mut [u16],
        string: usize,
        val: u32,
    ) -> u16 {
        let hm = Self::update_hash(0, val) as usize;

        let h = head[hm];
        if h != string as u16 {
            // `prev` holds exactly `w_size` entries, so `len - 1` is `w_mask`. Masking with
            // the slice's own length lets LLVM drop the bounds check down to `len != 0`.
            let w_mask = prev.len().wrapping_sub(1);
            if let Some(p) = prev.get_mut(string & w_mask) {
                *p = h;
            }
            head[hm] = string as u16;
        }

        h
    }

    pub fn insert_string(state: &mut State, string: usize, count: usize) {
        let slice = &state.window.filled()[string + Self::HASH_CALC_OFFSET..];

        // it can happen that insufficient bytes are initialized
        // .take(count) generates worse assembly
        let slice = &slice[..Ord::min(slice.len(), count + 3)];

        // `prev` holds exactly `w_size` entries, so `len - 1` is `w_mask`.
        let head = state.head.as_mut_slice();
        let prev = state.prev.as_mut_slice();
        let w_mask = prev.len().wrapping_sub(1);
        for (i, w) in slice.windows(4).enumerate() {
            let idx = string as u16 + i as u16;

            let val = u32::from_le_bytes(w.try_into().unwrap());

            let hm = Self::update_hash(0, val) as usize;

            let h = head[hm];
            if h != idx {
                // `w_mask < 2^15`, so masking the full-width position gives the same index as
                // masking the u16 `idx`, and keeps `& (len - 1) < len` visible to LLVM.
                if let Some(p) = prev.get_mut((string + i) & w_mask) {
                    *p = h;
                }
                head[hm] = idx;
            }
        }
    }
}

pub struct RollHashCalc;

impl RollHashCalc {
    const HASH_CALC_OFFSET: usize = STD_MIN_MATCH - 1;

    const HASH_CALC_MASK: u32 = (1 << 15) - 1;

    fn hash_calc(h: u32, val: u32) -> u32 {
        const HASH_SLIDE: u32 = 5;
        (h << HASH_SLIDE) ^ val
    }

    pub fn update_hash(h: u32, val: u32) -> u32 {
        Self::hash_calc(h, val) & Self::HASH_CALC_MASK
    }

    pub fn quick_insert_string(state: &mut State, string: usize) -> u16 {
        let val = state.window.filled()[string + Self::HASH_CALC_OFFSET] as u32;

        state.ins_h = Self::hash_calc(state.ins_h, val);
        state.ins_h &= Self::HASH_CALC_MASK;

        let hm = state.ins_h as usize;

        let head = state.head.as_slice()[hm];
        if head != string as u16 {
            // `prev` holds exactly `w_size` entries, so `len - 1` is `w_mask`.
            let prev = state.prev.as_mut_slice();
            let w_mask = prev.len().wrapping_sub(1);
            if let Some(p) = prev.get_mut(string & w_mask) {
                *p = head;
            }
            state.head.as_mut_slice()[hm] = string as u16;
        }

        head
    }

    pub fn insert_string(state: &mut State, string: usize, count: usize) {
        let slice = &state.window.filled()[string + Self::HASH_CALC_OFFSET..][..count];

        // `prev` holds exactly `w_size` entries, so `len - 1` is `w_mask`; masking the
        // full-width position (rather than the u16 `idx`) keeps `& (len - 1) < len` visible to
        // LLVM, and is the same index since `w_mask < 2^15`.
        let head = state.head.as_mut_slice();
        let prev = state.prev.as_mut_slice();
        let w_mask = prev.len().wrapping_sub(1);
        let mut ins_h = state.ins_h;
        for (i, val) in slice.iter().copied().enumerate() {
            let idx = string as u16 + i as u16;

            ins_h = Self::hash_calc(ins_h, val as u32);
            ins_h &= Self::HASH_CALC_MASK;
            let hm = ins_h as usize;

            let h = head[hm];
            if h != idx {
                if let Some(p) = prev.get_mut((string + i) & w_mask) {
                    *p = h;
                }
                head[hm] = idx;
            }
        }
        state.ins_h = ins_h;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn roll_hash_calc() {
        assert_eq!(RollHashCalc::hash_calc(2565, 93), 82173);
        assert_eq!(RollHashCalc::hash_calc(16637, 10), 532394);
        assert_eq!(RollHashCalc::hash_calc(8106, 100), 259364);
        assert_eq!(RollHashCalc::hash_calc(29988, 101), 959717);
        assert_eq!(RollHashCalc::hash_calc(9445, 98), 302274);
        assert_eq!(RollHashCalc::hash_calc(7362, 117), 235573);
        assert_eq!(RollHashCalc::hash_calc(6197, 103), 198343);
        assert_eq!(RollHashCalc::hash_calc(1735, 32), 55488);
        assert_eq!(RollHashCalc::hash_calc(22720, 61), 727101);
        assert_eq!(RollHashCalc::hash_calc(6205, 32), 198528);
        assert_eq!(RollHashCalc::hash_calc(3826, 117), 122421);
        assert_eq!(RollHashCalc::hash_calc(24117, 101), 771781);
    }

    #[test]
    fn standard_hash_calc() {
        assert_eq!(StandardHashCalc::hash_calc(0, 807411760), 65468);
        assert_eq!(StandardHashCalc::hash_calc(0, 540024864), 42837);
        assert_eq!(StandardHashCalc::hash_calc(0, 538980384), 33760);
        assert_eq!(StandardHashCalc::hash_calc(0, 775430176), 8925);
        assert_eq!(StandardHashCalc::hash_calc(0, 941629472), 42053);
    }
}