Skip to content

Commit 051ae5e

Browse files
committed
Use sync.Pool to cache zstd ctx
1 parent 0bea7cd commit 051ae5e

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

pulsar/internal/compression/zstd_cgo.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,23 @@
2525
package compression
2626

2727
import (
28+
"sync"
29+
2830
"github.com/DataDog/zstd"
2931
log "github.com/sirupsen/logrus"
3032
)
3133

3234
type zstdCGoProvider struct {
33-
ctx zstd.Ctx
35+
ctxPool sync.Pool
3436
level Level
3537
zstdLevel int
3638
}
3739

3840
func newCGoZStdProvider(level Level) Provider {
3941
z := &zstdCGoProvider{
40-
ctx: zstd.NewCtx(),
42+
ctxPool: sync.Pool{New: func() any {
43+
return zstd.NewCtx()
44+
}},
4145
}
4246

4347
switch level {
@@ -61,7 +65,9 @@ func (z *zstdCGoProvider) CompressMaxSize(originalSize int) int {
6165
}
6266

6367
func (z *zstdCGoProvider) Compress(dst, src []byte) []byte {
64-
out, err := z.ctx.CompressLevel(dst, src, z.zstdLevel)
68+
ctx := z.ctxPool.Get().(zstd.Ctx)
69+
defer z.ctxPool.Put(ctx)
70+
out, err := ctx.CompressLevel(dst, src, z.zstdLevel)
6571
if err != nil {
6672
log.WithError(err).Fatal("Failed to compress")
6773
}
@@ -70,7 +76,9 @@ func (z *zstdCGoProvider) Compress(dst, src []byte) []byte {
7076
}
7177

7278
func (z *zstdCGoProvider) Decompress(dst, src []byte, originalSize int) ([]byte, error) {
73-
return z.ctx.Decompress(dst, src)
79+
ctx := z.ctxPool.Get().(zstd.Ctx)
80+
defer z.ctxPool.Put(ctx)
81+
return ctx.Decompress(dst, src)
7482
}
7583

7684
func (z *zstdCGoProvider) Close() error {

0 commit comments

Comments
 (0)