From: Nathan Moinvaziri Date: Wed, 23 Jun 2021 00:22:52 +0000 (-0700) Subject: Switch longest_match in deflate_slow based on whether or not rolling hash is being... X-Git-Tag: 2.1.0-beta1~543 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e4c622371d8fb4c3700a6da25fd49b88a13f7431;p=thirdparty%2Fzlib-ng.git Switch longest_match in deflate_slow based on whether or not rolling hash is being used. Co-authored-by: Hans Kristian Rosbach --- diff --git a/deflate.c b/deflate.c index fa86df0c6..c66c1e17a 100644 --- a/deflate.c +++ b/deflate.c @@ -100,9 +100,6 @@ const char PREFIX(deflate_copyright)[] = " deflate 1.2.11.f Copyright 1995-2016 /* =========================================================================== * Function prototypes. */ -typedef block_state (*compress_func) (deflate_state *s, int flush); -/* Compression function. Returns the block state after the call. */ - static int deflateStateCheck (PREFIX3(stream) *strm); Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush); Z_INTERNAL block_state deflate_fast (deflate_state *s, int flush); diff --git a/deflate_p.h b/deflate_p.h index 9b23c51eb..da667a0f9 100644 --- a/deflate_p.h +++ b/deflate_p.h @@ -108,4 +108,9 @@ static inline int zng_tr_tally_dist(deflate_state *s, uint32_t dist, uint32_t le /* Maximum stored block length in deflate format (not including header). */ #define MAX_STORED 65535 +/* Compression function. Returns the block state after the call. */ +typedef block_state (*compress_func) (deflate_state *s, int flush); +/* Match function. Returns the longest match. */ +typedef uint32_t (*match_func) (deflate_state *const s, Pos cur_match); + #endif diff --git a/deflate_slow.c b/deflate_slow.c index 19bd09fb6..b8beec9cc 100644 --- a/deflate_slow.c +++ b/deflate_slow.c @@ -19,6 +19,12 @@ Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) { int bflush; /* set if current block must be flushed */ int64_t dist; uint32_t match_len; + match_func longest_match; + + if (s->max_chain_length <= 1024) + longest_match = functable.longest_match; + else + longest_match = functable.longest_match_slow; /* Process the input block. */ for (;;) { @@ -55,7 +61,7 @@ Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) { * of window index 0 (in particular we have to avoid a match * of the string with itself at the start of the input file). */ - match_len = functable.longest_match_slow(s, hash_head); + match_len = longest_match(s, hash_head); /* longest_match() sets match_start */ if (match_len <= 5 && (s->strategy == Z_FILTERED)) { diff --git a/match_tpl.h b/match_tpl.h index 920f715ad..e4e31e981 100644 --- a/match_tpl.h +++ b/match_tpl.h @@ -48,7 +48,6 @@ Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) { Pos limit; #ifdef LONGEST_MATCH_SLOW Pos limit_base; - int32_t rolling_hash; #else int32_t early_exit; #endif @@ -96,11 +95,6 @@ Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) { /* Do not waste too much time if we already have a good match */ chain_length = s->max_chain_length; -#ifdef LONGEST_MATCH_SLOW - rolling_hash = chain_length > 1024; -#else - early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL; -#endif if (best_len >= s->good_match) chain_length >>= 2; nice_match = (uint32_t)s->nice_match; @@ -111,7 +105,7 @@ Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) { limit = strstart > MAX_DIST(s) ? (Pos)(strstart - MAX_DIST(s)) : 0; #ifdef LONGEST_MATCH_SLOW limit_base = limit; - if (best_len >= STD_MIN_MATCH && rolling_hash) { + if (best_len >= STD_MIN_MATCH) { /* We're continuing search (lazy evaluation). */ uint32_t i, hash; Pos pos; @@ -141,6 +135,8 @@ Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) { mbase_start -= match_offset; mbase_end -= match_offset; } +#else + early_exit = s->level < EARLY_EXIT_TRIGGER_LEVEL; #endif Assert((unsigned long)strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead"); for (;;) { @@ -217,7 +213,7 @@ Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) { #endif #ifdef LONGEST_MATCH_SLOW /* Look for a better string offset */ - if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart && rolling_hash)) { + if (UNLIKELY(len > STD_MIN_MATCH && match_start + len < strstart)) { Pos pos, next_pos; uint32_t i, hash; unsigned char *scan_endstr;