From: Mika Lindqvist Date: Tue, 14 Feb 2017 09:40:52 +0000 (+0200) Subject: Avoid hashing same memory location twice by truncating overlapping byte ranges, X-Git-Tag: 1.9.9-b1~682 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=53204685c0165028e6a33f13e22fdda3c70a218c;p=thirdparty%2Fzlib-ng.git Avoid hashing same memory location twice by truncating overlapping byte ranges, 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. --- diff --git a/deflate_medium.c b/deflate_medium.c index 82301d1c4..046449fb9 100644 --- a/deflate_medium.c +++ b/deflate_medium.c @@ -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