From b464d1503800051479d3b88ac9794db027bdab35 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Tue, 18 Jan 2022 21:54:26 +0100 Subject: [PATCH] flate: Increase level 7+8 speed and compression Keep expensive check for level 9. --- flate/deflate.go | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/flate/deflate.go b/flate/deflate.go index 1e9e41e315..bffa2f3323 100644 --- a/flate/deflate.go +++ b/flate/deflate.go @@ -73,8 +73,8 @@ var levels = []compressionLevel{ {0, 0, 0, 0, 0, 6}, // Levels 7-9 use increasingly more lazy matching // and increasingly stringent conditions for "good enough". - {6, 10, 12, 16, skipNever, 7}, - {10, 24, 32, 64, skipNever, 8}, + {8, 12, 16, 24, skipNever, 7}, + {16, 30, 40, 64, skipNever, 8}, {32, 258, 258, 1024, skipNever, 9}, } @@ -295,11 +295,38 @@ func (d *compressor) findMatch(pos int, prevHead int, lookahead int) (length, of } offset = 0 + cGain := 0 + if d.chain < 100 { + for i := prevHead; tries > 0; tries-- { + if wEnd == win[i+length] { + n := matchLen(win[i:i+minMatchLook], wPos) + if n > length { + length = n + offset = pos - i + ok = true + if n >= nice { + // The match is good enough that we don't try to find a better one. + break + } + wEnd = win[pos+n] + } + } + if i <= minIndex { + // hashPrev[i & windowMask] has already been overwritten, so stop now. + break + } + i = int(d.state.hashPrev[i&windowMask]) - d.state.hashOffset + if i < minIndex { + break + } + } + return + } + // Some like it higher (CSV), some like it lower (JSON) const baseCost = 6 // Base is 4 bytes at with an additional cost. // Matches must be better than this. - cGain := 0 for i := prevHead; tries > 0; tries-- { if wEnd == win[i+length] { n := matchLen(win[i:i+minMatchLook], wPos) @@ -392,7 +419,7 @@ func (d *compressor) deflateLazy() { if d.windowEnd-s.index < minMatchLength+maxMatchLength && !d.sync { return } - if d.windowEnd != s.index { + if d.windowEnd != s.index && d.chain > 100 { // Get literal huffman coder. if d.h == nil { d.h = newHuffmanEncoder(maxFlateBlockTokens) @@ -402,7 +429,6 @@ func (d *compressor) deflateLazy() { tmp[v]++ } d.h.generate(tmp[:], 15) - //s.estBitsPerByte = d.h.bitLength(tmp[:])/(d.windowEnd-s.index) + 1 } s.maxInsertIndex = d.windowEnd - (minMatchLength - 1)