Zstd re-use

This commit is contained in:
Caleb Gardner
2025-04-16 18:02:19 -05:00
parent f991ddb1d4
commit f32cb520dc
+7 -18
View File
@@ -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)
}