mirror of https://github.com/golang/go.git
archive/zip: pool flate readers
Similar to the flate Writer pools already used, this adds pooling for flate Readers. compress/flate allows re-using of Readers, see https://codereview.appspot.com/97140043/ In a real-world scenario when reading ~ 500 small files from a ZIP archive this gives a speedup of 1.5x-2x. Fixes #14289 Change-Id: I2d98ad983e95ab7d97e06fd0145f619b4f47caa4 Reviewed-on: https://go-review.googlesource.com/19416 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
2f41edf120
commit
e155bf0ce8
|
|
@ -64,6 +64,44 @@ func (w *pooledFlateWriter) Close() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var flateReaderPool sync.Pool
|
||||||
|
|
||||||
|
func newFlateReader(r io.Reader) io.ReadCloser {
|
||||||
|
fr, ok := flateReaderPool.Get().(io.ReadCloser)
|
||||||
|
if ok {
|
||||||
|
fr.(flate.Resetter).Reset(r, nil)
|
||||||
|
} else {
|
||||||
|
fr = flate.NewReader(r)
|
||||||
|
}
|
||||||
|
return &pooledFlateReader{fr: fr}
|
||||||
|
}
|
||||||
|
|
||||||
|
type pooledFlateReader struct {
|
||||||
|
mu sync.Mutex // guards Close and Read
|
||||||
|
fr io.ReadCloser
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *pooledFlateReader) Read(p []byte) (n int, err error) {
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
if r.fr == nil {
|
||||||
|
return 0, errors.New("Read after Close")
|
||||||
|
}
|
||||||
|
return r.fr.Read(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *pooledFlateReader) Close() error {
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
var err error
|
||||||
|
if r.fr != nil {
|
||||||
|
err = r.fr.Close()
|
||||||
|
flateReaderPool.Put(r.fr)
|
||||||
|
r.fr = nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
mu sync.RWMutex // guards compressor and decompressor maps
|
mu sync.RWMutex // guards compressor and decompressor maps
|
||||||
|
|
||||||
|
|
@ -74,7 +112,7 @@ var (
|
||||||
|
|
||||||
decompressors = map[uint16]Decompressor{
|
decompressors = map[uint16]Decompressor{
|
||||||
Store: ioutil.NopCloser,
|
Store: ioutil.NopCloser,
|
||||||
Deflate: flate.NewReader,
|
Deflate: newFlateReader,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue