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 package decompress
import ( import (
"sync"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
) )
type Zstd struct { type Zstd struct {
pool sync.Pool rdr *zstd.Decoder
} }
func NewZstd() *Zstd { func NewZstd() Zstd {
return &Zstd{ rdr, _ := zstd.NewReader(nil, zstd.WithDecoderLowmem(true))
pool: sync.Pool{ return Zstd{
New: func() any { rdr: rdr,
rdr, _ := zstd.NewReader(nil, zstd.WithDecoderLowmem(true), zstd.WithDecoderConcurrency(1))
return rdr
},
},
} }
} }
func (z *Zstd) Decompress(data []byte) ([]byte, error) { func (z Zstd) Decompress(data []byte) ([]byte, error) {
rdr := z.pool.Get().(*zstd.Decoder) return z.rdr.DecodeAll(data, nil)
defer func() {
rdr.Reset(nil)
z.pool.Put(rdr)
}()
return rdr.DecodeAll(data, nil)
} }