Skip to content

Commit

Permalink
Skip creating encoders when not needed (#238)
Browse files Browse the repository at this point in the history
* Skip creating encoders when not needed
* Set default if encoder hasn't been initialized.

Only EncodeAll uses the channel of encoders, so only allocate it when needed there.

Fixes #235

Reported by @jpchen-lightstep
  • Loading branch information
klauspost committed Feb 28, 2020
1 parent 98b287b commit 022415c
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions zstd/encoder.go
Expand Up @@ -71,15 +71,14 @@ func NewWriter(w io.Writer, opts ...EOption) (*Encoder, error) {
}
if w != nil {
e.Reset(w)
} else {
e.init.Do(func() {
e.initialize()
})
}
return &e, nil
}

func (e *Encoder) initialize() {
if e.o.concurrent == 0 {
e.o.setDefault()
}
e.encoders = make(chan encoder, e.o.concurrent)
for i := 0; i < e.o.concurrent; i++ {
e.encoders <- e.o.encoder()
Expand All @@ -89,9 +88,6 @@ func (e *Encoder) initialize() {
// Reset will re-initialize the writer and new writes will encode to the supplied writer
// as a new, independent stream.
func (e *Encoder) Reset(w io.Writer) {
e.init.Do(func() {
e.initialize()
})
s := &e.state
s.wg.Wait()
s.wWg.Wait()
Expand Down Expand Up @@ -422,10 +418,7 @@ func (e *Encoder) EncodeAll(src, dst []byte) []byte {
}
return dst
}
e.init.Do(func() {
e.o.setDefault()
e.initialize()
})
e.init.Do(e.initialize)
enc := <-e.encoders
defer func() {
// Release encoder reference to last block.
Expand Down

0 comments on commit 022415c

Please sign in to comment.