MCPcopy
hub / github.com/klauspost/compress / writeBlockHuff

Method writeBlockHuff

flate/huffman_bit_writer.go:987–1174  ·  view source on GitHub ↗

writeBlockHuff encodes a block of bytes as either Huffman encoded literals or uncompressed bytes if the results only gains very little from compression.

(eof bool, input []byte, sync bool)

Source from the content-addressed store, hash-verified

985// Huffman encoded literals or uncompressed bytes if the
986// results only gains very little from compression.
987func (w *huffmanBitWriter) writeBlockHuff(eof bool, input []byte, sync bool) {
988 if w.err != nil {
989 return
990 }
991
992 // Clear histogram
993 for i := range w.literalFreq[:] {
994 w.literalFreq[i] = 0
995 }
996 if !w.lastHuffMan {
997 for i := range w.offsetFreq[:] {
998 w.offsetFreq[i] = 0
999 }
1000 }
1001
1002 const numLiterals = endBlockMarker + 1
1003 const numOffsets = 1
1004
1005 // Add everything as literals
1006 // We have to estimate the header size.
1007 // Assume header is around 70 bytes:
1008 // https://stackoverflow.com/a/25454430
1009 const guessHeaderSizeBits = 70 * 8
1010 histogram(input, w.literalFreq[:numLiterals])
1011 ssize, storable := w.storedSize(input)
1012 if storable && len(input) > 1024 {
1013 // Quick check for incompressible content.
1014 abs := float64(0)
1015 avg := float64(len(input)) / 256
1016 max := float64(len(input) * 2)
1017 for _, v := range w.literalFreq[:256] {
1018 diff := float64(v) - avg
1019 abs += diff * diff
1020 if abs > max {
1021 break
1022 }
1023 }
1024 if abs < max {
1025 if debugDeflate {
1026 fmt.Println("stored", abs, "<", max)
1027 }
1028 // No chance we can compress this...
1029 w.writeStoredHeader(len(input), eof)
1030 w.writeBytes(input)
1031 return
1032 }
1033 }
1034 w.literalFreq[endBlockMarker] = 1
1035 w.tmpLitEncoding.generate(w.literalFreq[:numLiterals], 15)
1036 estBits := w.tmpLitEncoding.canReuseBits(w.literalFreq[:numLiterals])
1037 if estBits < math.MaxInt32 {
1038 estBits += w.lastHeader
1039 if w.lastHeader == 0 {
1040 estBits += guessHeaderSizeBits
1041 }
1042 estBits += estBits >> w.logNewTablePenalty
1043 }
1044

Callers 6

testBlockHuffFunction · 0.80
testWriterEOFFunction · 0.80
writeBlockSkipMethod · 0.80
storeHuffMethod · 0.80
storeFastMethod · 0.80
StatelessDeflateFunction · 0.80

Calls 15

storedSizeMethod · 0.95
writeStoredHeaderMethod · 0.95
writeBytesMethod · 0.95
writeCodeMethod · 0.95
generateCodegenMethod · 0.95
codegensMethod · 0.95
writeDynamicHeaderMethod · 0.95
headerSizeMethod · 0.95
writeOutBitsMethod · 0.95
Store64Function · 0.92
histogramFunction · 0.85
canReuseBitsMethod · 0.80

Tested by 2

testBlockHuffFunction · 0.64
testWriterEOFFunction · 0.64