Zstd re-use
This commit is contained in:
@@ -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)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user