Zstd re-use
This commit is contained in:
@@ -1,31 +1,20 @@
|
||||
package decompress
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/klauspost/compress/zstd"
|
||||
)
|
||||
|
||||
type Zstd struct {
|
||||
pool sync.Pool
|
||||
rdr *zstd.Decoder
|
||||
}
|
||||
|
||||
func NewZstd() *Zstd {
|
||||
return &Zstd{
|
||||
pool: sync.Pool{
|
||||
New: func() any {
|
||||
rdr, _ := zstd.NewReader(nil, zstd.WithDecoderLowmem(true), zstd.WithDecoderConcurrency(1))
|
||||
return rdr
|
||||
},
|
||||
},
|
||||
func NewZstd() Zstd {
|
||||
rdr, _ := zstd.NewReader(nil, zstd.WithDecoderLowmem(true))
|
||||
return Zstd{
|
||||
rdr: rdr,
|
||||
}
|
||||
}
|
||||
|
||||
func (z *Zstd) Decompress(data []byte) ([]byte, error) {
|
||||
rdr := z.pool.Get().(*zstd.Decoder)
|
||||
defer func() {
|
||||
rdr.Reset(nil)
|
||||
z.pool.Put(rdr)
|
||||
}()
|
||||
return rdr.DecodeAll(data, nil)
|
||||
func (z Zstd) Decompress(data []byte) ([]byte, error) {
|
||||
return z.rdr.DecodeAll(data, nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user