From 7f6538681aad36d22f2c10e1ace5ed5a1a61b47c Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Fri, 28 Feb 2020 13:24:14 +0100 Subject: [PATCH 1/2] Skip creating encoders when not needed Only EncodeAll uses the channel of encoders, so only allocate it when needed there. Fixes #235 Reported by @jpchen-lightstep --- zstd/encoder.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/zstd/encoder.go b/zstd/encoder.go index 4032fb9fc8..935f414f0e 100644 --- a/zstd/encoder.go +++ b/zstd/encoder.go @@ -71,10 +71,6 @@ 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 } @@ -89,9 +85,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() @@ -423,7 +416,6 @@ func (e *Encoder) EncodeAll(src, dst []byte) []byte { return dst } e.init.Do(func() { - e.o.setDefault() e.initialize() }) enc := <-e.encoders From 88988480cc6fb85eb0b6459f7e230488c65c5271 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Fri, 28 Feb 2020 13:35:53 +0100 Subject: [PATCH 2/2] Set default if encoder hasn't been initialized. --- zstd/encoder.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/zstd/encoder.go b/zstd/encoder.go index 935f414f0e..67d45efb98 100644 --- a/zstd/encoder.go +++ b/zstd/encoder.go @@ -76,6 +76,9 @@ func NewWriter(w io.Writer, opts ...EOption) (*Encoder, error) { } 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() @@ -415,9 +418,7 @@ func (e *Encoder) EncodeAll(src, dst []byte) []byte { } return dst } - e.init.Do(func() { - e.initialize() - }) + e.init.Do(e.initialize) enc := <-e.encoders defer func() { // Release encoder reference to last block.