@@ -1008,6 +1008,26 @@ func (w *huffmanBitWriter) writeBlockHuff(eof bool, input []byte, sync bool) {
1008
1008
// https://stackoverflow.com/a/25454430
1009
1009
const guessHeaderSizeBits = 70 * 8
1010
1010
histogram (input , w .literalFreq [:numLiterals ], fill )
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
+ // No chance we can compress this...
1026
+ w .writeStoredHeader (len (input ), eof )
1027
+ w .writeBytes (input )
1028
+ return
1029
+ }
1030
+ }
1011
1031
w .literalFreq [endBlockMarker ] = 1
1012
1032
w .tmpLitEncoding .generate (w .literalFreq [:numLiterals ], 15 )
1013
1033
if fill {
@@ -1025,7 +1045,6 @@ func (w *huffmanBitWriter) writeBlockHuff(eof bool, input []byte, sync bool) {
1025
1045
estBits += estBits >> w .logNewTablePenalty
1026
1046
1027
1047
// Store bytes, if we don't get a reasonable improvement.
1028
- ssize , storable := w .storedSize (input )
1029
1048
if storable && ssize <= estBits {
1030
1049
w .writeStoredHeader (len (input ), eof )
1031
1050
w .writeBytes (input )
0 commit comments