-
Notifications
You must be signed in to change notification settings - Fork 3
/
compress.go
272 lines (236 loc) · 7.79 KB
/
compress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright 2022 Klaus Post.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package compress
import (
"fmt"
"io"
"connectrpc.com/connect"
"github.com/klauspost/compress/gzip"
"github.com/klauspost/compress/s2"
"github.com/klauspost/compress/zstd"
)
// Level provides 3 predefined compression levels.
type Level int
const (
// LevelFastest will choose the least cpu intensive cpu method.
LevelFastest Level = iota
// LevelBalanced provides balanced compression.
// Typical cpu usage will be around 200% of the fastest setting.
LevelBalanced
// LevelSmallest will use the strongest and most resource intensive
// compression method.
// This is generally not recommended.
LevelSmallest
)
const (
// Gzip provides faster compression methods than the standard library
// built-in to go-connect.
// Expected performance is ~200MB/s on JSON streams.
// Size reduction is ~85% on JSON stream.
Gzip = "gzip"
// Zstandard uses Zstandard compression,
// but with limited window sizes.
// Generally Zstandard compresses better and is faster than gzip.
// Expected performance is ~300MB/s on JSON streams.
// Size ~35% smaller than gzip on JSON stream.
Zstandard = "zstd"
// Snappy uses Google snappy format.
// Expected performance is ~550MB/s on JSON streams.
// Size ~50% bigger than gzip on JSON stream.
Snappy = "snappy"
// S2 provides better compression than Snappy at similar or better speeds.
// Expected performance is ~750MB/s on JSON streams.
// Size ~2% bigger than gzip on JSON stream.
S2 = "s2"
)
// Opts provides options
type Opts uint32
func (o Opts) contains(x Opts) bool {
return o&x == x
}
// maxLimitedWindow is the window limit.
const maxLimitedWindow = 64 << 10
const (
// OptStatelessGzip will force gzip compression to be stateless.
// Since each Write call will compress input this will affect compression ratio
// and should only be used when Write calls are controlled.
// Typically, this means a buffer should be inserted on the writer.
// See https://github.com/klauspost/compress#stateless-compression
// Compression level will be ignored.
OptStatelessGzip Opts = 1 << iota
// OptAllowMultithreadedCompression will allow some compression modes to use multiple goroutines.
OptAllowMultithreadedCompression
// OptSmallWindow will limit the compression window to 64KB.
// This will reduce memory usage of running operations,
// but also make compression worse.
OptSmallWindow
// internal snappy option
optSnappy
)
type compressorOption struct {
connect.ClientOption
connect.HandlerOption
}
// WithAll returns the client and handler option for all compression methods.
// Order of preference is S2, Snappy, Zstandard, Gzip.
func WithAll(level Level, options ...Opts) connect.Option {
var opts []connect.Option
for _, name := range []string{Gzip, Zstandard, Snappy, S2} {
opts = append(opts, WithNew(name, level, options...))
}
return connect.WithOptions(opts...)
}
// WithNew returns client and handler options for a single compression method.
// Name must be one of the predefined in this package.
func WithNew(name string, level Level, options ...Opts) connect.Option {
var o Opts
for _, opt := range options {
o = o | opt
}
var d func() connect.Decompressor
var c func() connect.Compressor
switch name {
case Gzip:
d, c = gzComp(level, o)
case Zstandard:
d, c = zstdComp(level, o)
case Snappy:
o |= optSnappy
d, c = s2Comp(level, o)
case S2:
d, c = s2Comp(level, o)
default:
panic(fmt.Errorf("unknown compression name: %s", name))
}
return &compressorOption{
ClientOption: connect.WithAcceptCompression(name, d, c),
HandlerOption: connect.WithCompression(name, d, c),
}
}
func gzComp(level Level, o Opts) (d func() connect.Decompressor, c func() connect.Compressor) {
return func() connect.Decompressor {
return &gzip.Reader{}
}, func() connect.Compressor {
if o.contains(OptStatelessGzip) {
gz, _ := gzip.NewWriterLevel(io.Discard, gzip.StatelessCompression)
return gz
}
switch level {
case LevelFastest:
gz, _ := gzip.NewWriterLevel(io.Discard, 1)
return gz
case LevelBalanced:
gz, _ := gzip.NewWriterLevel(io.Discard, 5)
return gz
case LevelSmallest:
gz, _ := gzip.NewWriterLevel(io.Discard, 9)
return gz
}
return gzip.NewWriter(io.Discard)
}
}
func zstdComp(level Level, o Opts) (d func() connect.Decompressor, c func() connect.Compressor) {
copts := []zstd.EOption{zstd.WithLowerEncoderMem(true)}
dopts := []zstd.DOption{zstd.WithDecoderLowmem(true), zstd.WithDecoderConcurrency(1)}
if o.contains(OptSmallWindow) {
dopts = append(dopts, zstd.WithDecoderMaxWindow(64<<10))
}
if o.contains(OptAllowMultithreadedCompression) {
// No need to go over board here.
copts = append(copts, zstd.WithEncoderConcurrency(4))
} else {
copts = append(copts, zstd.WithEncoderConcurrency(1))
}
switch level {
case LevelFastest:
copts = append(copts, zstd.WithEncoderLevel(zstd.SpeedFastest))
if o.contains(OptSmallWindow) {
copts = append(copts, zstd.WithWindowSize(64<<10))
} else {
copts = append(copts, zstd.WithWindowSize(1<<20))
}
case LevelBalanced:
copts = append(copts, zstd.WithEncoderLevel(zstd.SpeedDefault))
if o.contains(OptSmallWindow) {
copts = append(copts, zstd.WithWindowSize(64<<10))
} else {
copts = append(copts, zstd.WithWindowSize(1<<20))
}
case LevelSmallest:
copts = append(copts, zstd.WithEncoderLevel(zstd.SpeedBestCompression))
if o.contains(OptSmallWindow) {
copts = append(copts, zstd.WithWindowSize(64<<10))
} else {
copts = append(copts, zstd.WithWindowSize(4<<20))
}
}
return func() connect.Decompressor {
zs, _ := zstd.NewReader(nil, dopts...)
return &zstdWrapper{ReadCloser: zs.IOReadCloser(), dec: zs}
}, func() connect.Compressor {
zs, _ := zstd.NewWriter(nil, copts...)
return zs
}
}
type zstdWrapper struct {
io.ReadCloser
dec *zstd.Decoder
}
func (z *zstdWrapper) Reset(reader io.Reader) error {
return z.dec.Reset(reader)
}
func s2Comp(level Level, o Opts) (d func() connect.Decompressor, c func() connect.Compressor) {
var wopts []s2.WriterOption
var ropts []s2.ReaderOption
if o.contains(optSnappy) {
wopts = append(wopts, s2.WriterSnappyCompat())
ropts = append(ropts, s2.ReaderMaxBlockSize(maxLimitedWindow), s2.ReaderAllocBlock(maxLimitedWindow))
} else if o.contains(OptSmallWindow) {
wopts = append(wopts, s2.WriterBlockSize(maxLimitedWindow))
ropts = append(ropts, s2.ReaderMaxBlockSize(maxLimitedWindow), s2.ReaderAllocBlock(maxLimitedWindow))
}
if !o.contains(OptAllowMultithreadedCompression) {
wopts = append(wopts, s2.WriterConcurrency(1))
}
switch level {
case LevelFastest:
case LevelBalanced:
wopts = append(wopts, s2.WriterBetterCompression())
case LevelSmallest:
wopts = append(wopts, s2.WriterBestCompression())
if !o.contains(OptSmallWindow) && !o.contains(optSnappy) {
wopts = append(wopts, s2.WriterBlockSize(4<<20))
}
}
return func() connect.Decompressor {
dec := s2.NewReader(nil, ropts...)
return &s2rWrapper{dec: dec}
}, func() connect.Compressor {
return s2.NewWriter(nil, wopts...)
}
}
type s2rWrapper struct {
dec *s2.Reader
}
func (s *s2rWrapper) Read(p []byte) (n int, err error) {
return s.dec.Read(p)
}
func (s *s2rWrapper) Close() error {
s.dec.Reset(nil)
return nil
}
func (s *s2rWrapper) Reset(reader io.Reader) error {
s.dec.Reset(reader)
return nil
}