@@ -30,7 +30,11 @@ type CompressionOpts struct {
30
30
// destination writer. It panics on any errors and should only be used at
31
31
// package initialization time.
32
32
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
+ )
34
38
if err != nil {
35
39
panic (err )
36
40
}
@@ -105,6 +109,13 @@ func (e *zlibEncoder) Encode(dst, src []byte) ([]byte, error) {
105
109
return dst , nil
106
110
}
107
111
112
+ var zstdBufPool = sync.Pool {
113
+ New : func () interface {} {
114
+ s := make ([]byte , 0 )
115
+ return & s
116
+ },
117
+ }
118
+
108
119
// CompressPayload takes a byte slice and compresses it according to the options passed
109
120
func CompressPayload (in []byte , opts CompressionOpts ) ([]byte , error ) {
110
121
switch opts .Compressor {
@@ -123,7 +134,13 @@ func CompressPayload(in []byte, opts CompressionOpts) ([]byte, error) {
123
134
if err != nil {
124
135
return nil , err
125
136
}
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
127
144
default :
128
145
return nil , fmt .Errorf ("unknown compressor ID %v" , opts .Compressor )
129
146
}
0 commit comments