diff --git a/huff0/compress.go b/huff0/compress.go index f51a542203..22bee74777 100644 --- a/huff0/compress.go +++ b/huff0/compress.go @@ -35,6 +35,7 @@ func compress(in []byte, s *Scratch, compressor func(src []byte) ([]byte, error) if s.Reuse == ReusePolicyNone { s.prevTable = s.prevTable[:0] } + // Create histogram, if none was provided. maxCount := s.maxCount var canReuse = false @@ -338,6 +339,10 @@ type cTableEntry struct { const huffNodesMask = huffNodesLen - 1 func (s *Scratch) buildCTable() error { + if cap(s.cTable) < maxSymbolValue+1 { + s.cTable = make([]cTableEntry, 0, maxSymbolValue+1) + } + s.huffSort() s.cTable = s.cTable[:s.symbolLen] diff --git a/huff0/huff0.go b/huff0/huff0.go index 2f2df1688b..a9a93aa76d 100644 --- a/huff0/huff0.go +++ b/huff0/huff0.go @@ -122,22 +122,16 @@ func (s *Scratch) prepare(in []byte) (*Scratch, error) { s.Out = make([]byte, 0, len(in)) } s.Out = s.Out[:0] - if cap(s.cTable) < maxSymbolValue+1 { - s.cTable = make([]cTableEntry, 0, maxSymbolValue+1) - } - s.cTable = s.cTable[:0] - if cap(s.prevTable) < maxSymbolValue+1 { - s.prevTable = make([]cTableEntry, 0, maxSymbolValue+1) - } + s.OutTable = nil s.OutData = nil if cap(s.nodes) < huffNodesLen+1 { s.nodes = make([]nodeElt, 0, huffNodesLen+1) } + s.nodes = s.nodes[:0] if s.fse == nil { s.fse = &fse.Scratch{} } - s.nodes = s.nodes[:0] s.br.init(in) return s, nil