Skip to content

Commit

Permalink
Tweak header estimates.
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Jan 15, 2020
1 parent 36b6ed4 commit e48bab1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 10 additions & 8 deletions flate/huffman_bit_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,12 @@ func (w *huffmanBitWriter) writeDynamicHeader(numLiterals int, numOffsets int, n

i := 0
for {
var codeWord int = int(w.codegen[i])
var codeWord = uint32(w.codegen[i])
i++
if codeWord == badCode {
break
}
w.writeCode(w.codegenEncoding.codes[uint32(codeWord)])
w.writeCode(w.codegenEncoding.codes[codeWord])

switch codeWord {
case 16:
Expand Down Expand Up @@ -609,7 +609,8 @@ func (w *huffmanBitWriter) writeBlockDynamic(tokens *tokens, eof bool, input []b
var size int
// Check if we should reuse.
if w.lastHeader > 0 {
// Estimate size for using a new table
// Estimate size for using a new table.
// Use the previous header size as the best estimate.
newSize := w.lastHeader + tokens.EstimatedBits()
newSize += newSize >> w.logReusePenalty

Expand Down Expand Up @@ -807,22 +808,23 @@ func (w *huffmanBitWriter) writeBlockHuff(eof bool, input []byte, sync bool) {
}

// Add everything as literals
const guessHeaderSizeBits = 400
// We have to estimate the header size.
// Assume header is around 70 bytes:
// https://stackoverflow.com/a/25454430
const guessHeaderSizeBits = 70 * 8
estBits := histogramSize(input, w.literalFreq[:], !eof && !sync) + 15 + guessHeaderSizeBits

// Store bytes, if we don't get a reasonable improvement.
ssize, storable := w.storedSize(input)
if storable && ssize < (estBits+estBits>>4) {
if storable && ssize < estBits {
w.writeStoredHeader(len(input), eof)
w.writeBytes(input)
return
}

if w.lastHeader > 0 {
size := w.dynamicReuseSize(w.literalEncoding, huffOffset) + w.lastHeader
if w.logReusePenalty > 0 {
estBits += estBits >> w.logReusePenalty
}
estBits += estBits >> w.logReusePenalty

if estBits < size {
// We owe an EOB
Expand Down
4 changes: 3 additions & 1 deletion flate/huffman_bit_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func TestBlockHuff(t *testing.T) {
if strings.HasSuffix(in, ".in") {
out = in[:len(in)-len(".in")] + ".golden"
}
testBlockHuff(t, in, out)
t.Run(in, func(t *testing.T) {
testBlockHuff(t, in, out)
})
}
}

Expand Down

0 comments on commit e48bab1

Please sign in to comment.