Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve memory usage of zstd encoder by using our own pool management #2375

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions zstd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,49 @@ import (
"github.com/klauspost/compress/zstd"
)

// zstdMaxBufferedEncoders maximum number of not-in-use zstd encoders
// If the pool of encoders is exhausted then new encoders will be created on the fly
const zstdMaxBufferedEncoders = 1

type ZstdEncoderParams struct {
Level int
}
type ZstdDecoderParams struct {
}

var zstdEncMap, zstdDecMap sync.Map
var zstdDecMap sync.Map

var zstdAvailableEncoders sync.Map

func getEncoder(params ZstdEncoderParams) *zstd.Encoder {
if ret, ok := zstdEncMap.Load(params); ok {
return ret.(*zstd.Encoder)
func getZstdEncoderChannel(params ZstdEncoderParams) chan *zstd.Encoder {
if c, ok := zstdAvailableEncoders.Load(params); ok {
return c.(chan *zstd.Encoder)
}
// It's possible to race and create multiple new writers.
// Only one will survive GC after use.
encoderLevel := zstd.SpeedDefault
if params.Level != CompressionLevelDefault {
encoderLevel = zstd.EncoderLevelFromZstd(params.Level)
c, _ := zstdAvailableEncoders.LoadOrStore(params, make(chan *zstd.Encoder, zstdMaxBufferedEncoders))
return c.(chan *zstd.Encoder)
}

func getZstdEncoder(params ZstdEncoderParams) *zstd.Encoder {
select {
case enc := <-getZstdEncoderChannel(params):
return enc
default:
encoderLevel := zstd.SpeedDefault
if params.Level != CompressionLevelDefault {
encoderLevel = zstd.EncoderLevelFromZstd(params.Level)
}
zstdEnc, _ := zstd.NewWriter(nil, zstd.WithZeroFrames(true),
zstd.WithEncoderLevel(encoderLevel),
zstd.WithEncoderConcurrency(1))
return zstdEnc
}
}

func releaseEncoder(params ZstdEncoderParams, enc *zstd.Encoder) {
select {
case getZstdEncoderChannel(params) <- enc:
default:
}
zstdEnc, _ := zstd.NewWriter(nil, zstd.WithZeroFrames(true),
zstd.WithEncoderLevel(encoderLevel))
zstdEncMap.Store(params, zstdEnc)
return zstdEnc
}

func getDecoder(params ZstdDecoderParams) *zstd.Decoder {
Expand All @@ -46,5 +67,8 @@ func zstdDecompress(params ZstdDecoderParams, dst, src []byte) ([]byte, error) {
}

func zstdCompress(params ZstdEncoderParams, dst, src []byte) ([]byte, error) {
return getEncoder(params).EncodeAll(src, dst), nil
enc := getZstdEncoder(params)
out := enc.EncodeAll(src, dst)
releaseEncoder(params, enc)
return out, nil
}
29 changes: 29 additions & 0 deletions zstd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sarama

import (
"runtime"
"testing"
)

func BenchmarkZstdMemoryConsumption(b *testing.B) {
params := ZstdEncoderParams{Level: 9}
buf := make([]byte, 1024*1024)
for i := 0; i < len(buf); i++ {
buf[i] = byte((i / 256) + (i * 257))
}

cpus := 96

gomaxprocsBackup := runtime.GOMAXPROCS(cpus)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for j := 0; j < 2*cpus; j++ {
_, _ = zstdCompress(params, nil, buf)
}
// drain the buffered encoder
getZstdEncoder(params)
// previously this would be achieved with
// zstdEncMap.Delete(params)
}
runtime.GOMAXPROCS(gomaxprocsBackup)
}