Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions zlib-rs/src/deflate/algorithm/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,27 @@ pub fn deflate_quick(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockSt
}
}

let lc: u8;
if state.lookahead >= WANT_MIN_MATCH {
let hash_head = StandardHashCalc::quick_insert_string(state, state.strstart);
macro_rules! first_four_bytes {
($slice:expr, $offset:expr) => {
u32::from_le_bytes($slice[$offset..$offset + 4].try_into().unwrap())
};
}

let str_val = {
let str_start = &state.window.filled()[state.strstart..];
first_four_bytes!(str_start, 0)
};
let hash_head = StandardHashCalc::quick_insert_value(state, state.strstart, str_val);
let dist = state.strstart as isize - hash_head as isize;

if dist <= state.max_dist() as isize && dist > 0 {
let str_start = &state.window.filled()[state.strstart..];
let match_start = &state.window.filled()[hash_head as usize..];

macro_rules! first_two_bytes {
($slice:expr, $offset:expr) => {
u16::from_le_bytes($slice[$offset..$offset + 2].try_into().unwrap())
};
}

if first_two_bytes!(str_start, 0) == first_two_bytes!(match_start, 0) {
if str_val == first_four_bytes!(match_start, 0) {
let mut match_len = crate::deflate::compare256::compare256_slice(
&str_start[2..],
&state.window.filled()[state.strstart + 2..],
&match_start[2..],
) + 2;

Expand All @@ -133,9 +137,10 @@ pub fn deflate_quick(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockSt
}
}
}
lc = str_val as u8;
} else {
lc = state.window.filled()[state.strstart];
}

let lc = state.window.filled()[state.strstart];
state.bit_writer.emit_lit(StaticTreeDesc::L.static_tree, lc);
state.strstart += 1;
state.lookahead -= 1;
Expand Down
5 changes: 5 additions & 0 deletions zlib-rs/src/deflate/hash_calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ impl StandardHashCalc {
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for me, this needs an #[inline], otherwise level 2 regresses by a couple of percent. Interestingly I then see some improvements for the other compression levels too (mostly in instructions), maybe because different (better) inlining decisions are made.

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

let head = state.head.as_slice()[hm];
Expand Down
Loading