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

[zstd] pass level param through to compress/zstd encoder #2045

Merged
merged 2 commits into from
Oct 15, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func compress(cc CompressionCodec, level int, data []byte) ([]byte, error) {
}
return buf.Bytes(), nil
case CompressionZSTD:
return zstdCompress(nil, data)
return zstdCompress(ZstdEncoderParams{level}, nil, data)
default:
return nil, PacketEncodingError{fmt.Sprintf("unsupported compression codec (%d)", cc)}
}
Expand Down
2 changes: 1 addition & 1 deletion decompress.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func decompress(cc CompressionCodec, data []byte) ([]byte, error) {

return io.ReadAll(reader)
case CompressionZSTD:
return zstdDecompress(nil, data)
return zstdDecompress(ZstdDecoderParams{}, nil, data)
default:
return nil, PacketDecodingError{fmt.Sprintf("invalid compression specified (%d)", cc)}
}
Expand Down
48 changes: 40 additions & 8 deletions zstd.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
package sarama

import (
"sync"

"github.com/klauspost/compress/zstd"
)

var (
zstdDec, _ = zstd.NewReader(nil)
zstdEnc, _ = zstd.NewWriter(nil, zstd.WithZeroFrames(true))
)
type ZstdEncoderParams struct {
Level int
}
type ZstdDecoderParams struct {
}

var zstdEncMap, zstdDecMap sync.Map

func getEncoder(params ZstdEncoderParams) *zstd.Encoder {
if ret, ok := zstdEncMap.Load(params); ok {
return ret.(*zstd.Encoder)
}
// It's possible to race and create multiple new writers.
// Only one will survive GC after use.
encoderLevel := zstd.SpeedDefault
if params.Level != CompressionLevelDefault {
encoderLevel = zstd.EncoderLevelFromZstd(params.Level)
}
zstdEnc, _ := zstd.NewWriter(nil, zstd.WithZeroFrames(true),
zstd.WithEncoderLevel(encoderLevel))
zstdEncMap.Store(params, zstdEnc)
return zstdEnc
}

func getDecoder(params ZstdDecoderParams) *zstd.Decoder {
if ret, ok := zstdDecMap.Load(params); ok {
return ret.(*zstd.Decoder)
}
// It's possible to race and create multiple new readers.
// Only one will survive GC after use.
zstdDec, _ := zstd.NewReader(nil)
zstdDecMap.Store(params, zstdDec)
return zstdDec
}

func zstdDecompress(dst, src []byte) ([]byte, error) {
return zstdDec.DecodeAll(src, dst)
func zstdDecompress(params ZstdDecoderParams, dst, src []byte) ([]byte, error) {
return getDecoder(params).DecodeAll(src, dst)
}

func zstdCompress(dst, src []byte) ([]byte, error) {
return zstdEnc.EncodeAll(src, dst), nil
func zstdCompress(params ZstdEncoderParams, dst, src []byte) ([]byte, error) {
return getEncoder(params).EncodeAll(src, dst), nil
}