Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zstd: Reuse zip decoders #514

Merged
merged 2 commits into from
Mar 6, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
zstd: Reuse zip decoders
Now that decoding is stateless we can pool decoders.
  • Loading branch information
klauspost committed Mar 6, 2022
commit cec7f383bba62eab02946c298af86d9d407fb0f9
16 changes: 7 additions & 9 deletions zstd/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 20,7 @@ const ZipMethodPKWare = 20

var zipReaderPool sync.Pool

// newZipReader cannot be used since we would leak goroutines...
// newZipReader creates a pooled zip decompressor.
func newZipReader(r io.Reader) io.ReadCloser {
dec, ok := zipReaderPool.Get().(*Decoder)
if ok {
Expand All @@ -47,7 47,11 @@ func (r *pooledZipReader) Read(p []byte) (n int, err error) {
return 0, errors.New("Read after Close")
}
dec, err := r.dec.Read(p)

if err == io.EOF {
err = r.dec.Reset(nil)
zipReaderPool.Put(r.dec)
r.dec = nil
}
return dec, err
}

Expand Down Expand Up @@ -112,11 116,5 @@ func ZipCompressor(opts ...EOption) func(w io.Writer) (io.WriteCloser, error) {
// ZipDecompressor returns a decompressor that can be registered with zip libraries.
// See ZipCompressor for example.
func ZipDecompressor() func(r io.Reader) io.ReadCloser {
return func(r io.Reader) io.ReadCloser {
d, err := NewReader(r, WithDecoderConcurrency(1), WithDecoderLowmem(true))
if err != nil {
panic(err)
}
return d.IOReadCloser()
}
return newZipReader
}