Skip to content

Commit 862005b

Browse files
brianpanefolkertdev
authored andcommitted
Hard-code the hash selection for the fast and medium deflate algorithms
Note: This only changes the quick_insert_string calls. In testing, bypassing the hash_calc_variant check on the insert_string calls in these algorithms appears to (counterintuitively) hurt performance.
1 parent f10deb4 commit 862005b

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

zlib-rs/src/deflate/algorithm/fast.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![forbid(unsafe_code)]
22

3+
use crate::deflate::hash_calc::StandardHashCalc;
34
use crate::{
45
deflate::{
56
fill_window, BlockState, DeflateStream, MIN_LOOKAHEAD, STD_MIN_MATCH, WANT_MIN_MATCH,
@@ -33,7 +34,7 @@ pub fn deflate_fast(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockSta
3334
// dictionary, and set hash_head to the head of the hash chain:
3435

3536
if state.lookahead >= WANT_MIN_MATCH {
36-
let hash_head = state.quick_insert_string(state.strstart);
37+
let hash_head = StandardHashCalc::quick_insert_string(state, state.strstart);
3738
dist = state.strstart as isize - hash_head as isize;
3839

3940
/* Find the longest match, discarding those <= prev_length.
@@ -70,7 +71,7 @@ pub fn deflate_fast(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockSta
7071
state.strstart += match_len;
7172
} else {
7273
state.strstart += match_len;
73-
state.quick_insert_string(state.strstart + 2 - STD_MIN_MATCH);
74+
StandardHashCalc::quick_insert_string(state, state.strstart + 2 - STD_MIN_MATCH);
7475

7576
/* If lookahead < STD_MIN_MATCH, ins_h is garbage, but it does not
7677
* matter since it will be recomputed at next deflate call.

zlib-rs/src/deflate/algorithm/medium.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![forbid(unsafe_code)]
22

3+
use crate::deflate::hash_calc::StandardHashCalc;
34
use crate::{
45
deflate::{
56
fill_window, BlockState, DeflateStream, State, MIN_LOOKAHEAD, STD_MIN_MATCH, WANT_MIN_MATCH,
@@ -60,7 +61,7 @@ pub fn deflate_medium(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockS
6061
} else {
6162
hash_head = 0;
6263
if state.lookahead >= WANT_MIN_MATCH {
63-
hash_head = state.quick_insert_string(state.strstart);
64+
hash_head = StandardHashCalc::quick_insert_string(state, state.strstart);
6465
}
6566

6667
current_match.strstart = state.strstart as u16;
@@ -104,7 +105,7 @@ pub fn deflate_medium(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockS
104105
< (state.window_size - MIN_LOOKAHEAD)
105106
{
106107
state.strstart = (current_match.strstart + current_match.match_length) as usize;
107-
hash_head = state.quick_insert_string(state.strstart);
108+
hash_head = StandardHashCalc::quick_insert_string(state, state.strstart);
108109

109110
next_match.strstart = state.strstart as u16;
110111
next_match.orgstart = next_match.strstart;
@@ -253,7 +254,7 @@ fn insert_match(state: &mut State, mut m: Match) {
253254
m.match_length = 0;
254255

255256
if (m.strstart as usize) >= (STD_MIN_MATCH - 2) {
256-
state.quick_insert_string(m.strstart as usize + 2 - STD_MIN_MATCH);
257+
StandardHashCalc::quick_insert_string(state, m.strstart as usize + 2 - STD_MIN_MATCH);
257258
}
258259

259260
/* If lookahead < WANT_MIN_MATCH, ins_h is garbage, but it does not

0 commit comments

Comments
 (0)