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

GODRIVER-3132 [master] Use dst buf pool for zstd encoding. #1686

Merged
merged 1 commit into from
Jun 26, 2024
Merged
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
21 changes: 19 additions & 2 deletions x/mongo/driver/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ type CompressionOpts struct {
// destination writer. It panics on any errors and should only be used at
// package initialization time.
func mustZstdNewWriter(lvl zstd.EncoderLevel) *zstd.Encoder {
enc, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(lvl))
enc, err := zstd.NewWriter(
nil,
zstd.WithWindowSize(8<<20), // Set window size to 8MB.
zstd.WithEncoderLevel(lvl),
)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -105,6 +109,13 @@ func (e *zlibEncoder) Encode(dst, src []byte) ([]byte, error) {
return dst, nil
}

var zstdBufPool = sync.Pool{
New: func() interface{} {
s := make([]byte, 0)
return &s
},
}

// CompressPayload takes a byte slice and compresses it according to the options passed
func CompressPayload(in []byte, opts CompressionOpts) ([]byte, error) {
switch opts.Compressor {
Expand All @@ -123,7 +134,13 @@ func CompressPayload(in []byte, opts CompressionOpts) ([]byte, error) {
if err != nil {
return nil, err
}
return encoder.EncodeAll(in, nil), nil
ptr := zstdBufPool.Get().(*[]byte)
b := encoder.EncodeAll(in, *ptr)
dst := make([]byte, len(b))
copy(dst, b)
*ptr = b[:0]
zstdBufPool.Put(ptr)
return dst, nil
default:
return nil, fmt.Errorf("unknown compressor ID %v", opts.Compressor)
}
Expand Down
Loading