Skip to content

Commit b10dc34

Browse files
authored
GODRIVER-3132 Use dst buf pool for zstd encoding. (#1577)
1 parent d43da19 commit b10dc34

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

x/mongo/driver/compression.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ type CompressionOpts struct {
3030
// destination writer. It panics on any errors and should only be used at
3131
// package initialization time.
3232
func mustZstdNewWriter(lvl zstd.EncoderLevel) *zstd.Encoder {
33-
enc, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(lvl))
33+
enc, err := zstd.NewWriter(
34+
nil,
35+
zstd.WithWindowSize(8<<20), // Set window size to 8MB.
36+
zstd.WithEncoderLevel(lvl),
37+
)
3438
if err != nil {
3539
panic(err)
3640
}
@@ -105,6 +109,13 @@ func (e *zlibEncoder) Encode(dst, src []byte) ([]byte, error) {
105109
return dst, nil
106110
}
107111

112+
var zstdBufPool = sync.Pool{
113+
New: func() interface{} {
114+
s := make([]byte, 0)
115+
return &s
116+
},
117+
}
118+
108119
// CompressPayload takes a byte slice and compresses it according to the options passed
109120
func CompressPayload(in []byte, opts CompressionOpts) ([]byte, error) {
110121
switch opts.Compressor {
@@ -123,7 +134,13 @@ func CompressPayload(in []byte, opts CompressionOpts) ([]byte, error) {
123134
if err != nil {
124135
return nil, err
125136
}
126-
return encoder.EncodeAll(in, nil), nil
137+
ptr := zstdBufPool.Get().(*[]byte)
138+
b := encoder.EncodeAll(in, *ptr)
139+
dst := make([]byte, len(b))
140+
copy(dst, b)
141+
*ptr = b[:0]
142+
zstdBufPool.Put(ptr)
143+
return dst, nil
127144
default:
128145
return nil, fmt.Errorf("unknown compressor ID %v", opts.Compressor)
129146
}

0 commit comments

Comments
 (0)