Skip to content

Commit

Permalink
Move some compression only initialization to the compressor.
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Apr 2, 2018
1 parent 30b4e9b commit 5fb1f31
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
5 changes: 5 additions & 0 deletions huff0/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand Down
10 changes: 2 additions & 8 deletions huff0/huff0.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5fb1f31

Please sign in to comment.