]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Avoid hashing same memory location twice by truncating overlapping byte ranges,
authorMika Lindqvist <postmaster@raasu.org>
Tue, 14 Feb 2017 09:40:52 +0000 (11:40 +0200)
committerHans Kristian Rosbach <hk-git@circlestorm.org>
Thu, 16 Feb 2017 10:19:57 +0000 (11:19 +0100)
it's speed optimization as the inner code also checks that previous hash value
is not same as new hash value. Essentially those two checks together makes the
compression a little more efficient as it can remember matches further apart.
As far as I remember from my tests, the secondary path was triggered only twice
in very long uncompressed file, but the gain in compression rate was still noticeable.

deflate_medium.c

index 82301d1c4472be6e855837745469265942f927cf..046449fb9b2159ae1221a75fc6570016a169573d 100644 (file)
@@ -68,17 +68,18 @@ static void insert_match(deflate_state *s, struct match match) {
             }
         }
 #else
-        if (likely(match.match_length == 1)) {
-            match.strstart++;
-            match.match_length = 0;
-        }else{
-            match.strstart++;
-            match.match_length--;
+        match.strstart++;
+        match.match_length--;
+        if (match.match_length > 0) {
             if (match.strstart >= match.orgstart) {
-                insert_string(s, match.strstart, match.match_length);
+                if (match.strstart + match.match_length - 1 >= match.orgstart) {
+                    insert_string(s, match.strstart, match.match_length);
+                } else {
+                    insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
+                }
+                match.strstart += match.match_length;
+                match.match_length = 0;
             }
-            match.strstart += match.match_length;
-            match.match_length = 0;
         }
 #endif
         return;
@@ -102,7 +103,11 @@ static void insert_match(deflate_state *s, struct match match) {
         } while (--match.match_length != 0);
 #else
         if (likely(match.strstart >= match.orgstart)) {
-            insert_string(s, match.strstart, match.match_length);
+            if (likely(match.strstart + match.match_length - 1 >= match.orgstart)) {
+                insert_string(s, match.strstart, match.match_length);
+            } else {
+                insert_string(s, match.strstart, match.orgstart - match.strstart + 1);
+            }
         }
         match.strstart += match.match_length;
         match.match_length = 0;
@@ -111,7 +116,7 @@ static void insert_match(deflate_state *s, struct match match) {
         match.strstart += match.match_length;
         match.match_length = 0;
         s->ins_h = s->window[match.strstart];
-        if (match.strstart >= 1)
+        if (match.strstart >= (MIN_MATCH - 2))
 #ifndef NOT_TWEAK_COMPILER
             insert_string(s, match.strstart + 2 - MIN_MATCH, MIN_MATCH - 2);
 #else