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

core/hpke: reduce memory usage from zstd #4650

Merged
merged 2 commits into from Oct 31, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions pkg/hpke/url.go
Expand Up @@ -145,22 +145,29 @@ func withoutHPKEParams(values url.Values) url.Values {
return filtered
}

var zstdEncoder, _ = zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.SpeedBestCompression))

func encodeQueryStringV1(values url.Values) []byte {
return []byte(values.Encode())
}

const zstdWindowSize = 8 << 10 // 8kiB

var zstdEncoder, _ = zstd.NewWriter(nil,
zstd.WithEncoderLevel(zstd.SpeedDefault),
zstd.WithWindowSize(zstdWindowSize),
)

func encodeQueryStringV2(values url.Values) []byte {
return zstdEncoder.EncodeAll([]byte(values.Encode()), nil)
}

var zstdDecoder, _ = zstd.NewReader(nil)

func decodeQueryStringV1(raw []byte) (url.Values, error) {
return url.ParseQuery(string(raw))
}

var zstdDecoder, _ = zstd.NewReader(nil,
zstd.WithDecoderLowmem(true),
)

func decodeQueryStringV2(raw []byte) (url.Values, error) {
bs, err := zstdDecoder.DecodeAll(raw, nil)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/hpke/url_test.go
Expand Up @@ -77,3 +77,16 @@ func TestEncryptURLValues(t *testing.T) {
assert.Less(t, len(encrypted.Encode()), 1024)
})
}

func BenchmarkZSTD(b *testing.B) {
payload := url.Values{
"a": {strings.Repeat("b", 128)},
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
bs := encodeQueryStringV2(payload)
_, _ = decodeQueryStringV2(bs)
}
}