]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Switch longest_match in deflate_slow based on whether or not rolling hash is being...
authorNathan Moinvaziri <nathan@nathanm.com>
Wed, 23 Jun 2021 00:22:52 +0000 (17:22 -0700)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Fri, 25 Jun 2021 18:09:14 +0000 (20:09 +0200)
Co-authored-by: Hans Kristian Rosbach <hk-git@circlestorm.org>
deflate.c
deflate_p.h
deflate_slow.c
match_tpl.h

index fa86df0c641b36e5f67e686e4b2edb43d5d04296..c66c1e17a58cf7b03dd7e6312b466e5fd66aeb81 100644 (file)
--- 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);
index 9b23c51ebad762cc6f9dddf02c7c15eac39fa1af..da667a0f9d50d888d4f4e215582141c3cbc73f84 100644 (file)
@@ -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
index 19bd09fb6ef72884f2ac687749dbc14b2a4bec33..b8beec9ccd28d2f7c628d5e9766aeadd1c6956d9 100644 (file)
@@ -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)) {
index 920f715ad07e0f952caf33fc12e11005338aacd2..e4e31e9819c1cc06b03092b6db61c13e0beae4f0 100644 (file)
@@ -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;