Skip to content

Commit

Permalink
respond to review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
lizthegrey committed Oct 15, 2021
1 parent c9e4131 commit 60e10ba
Showing 3 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion compress.go
Original file line number Diff line number Diff line change
@@ -187,7 +187,7 @@ func compress(cc CompressionCodec, level int, data []byte) ([]byte, error) {
}
return buf.Bytes(), nil
case CompressionZSTD:
return zstdCompress(level, nil, data)
return zstdCompress(ZstdEncoderParams{level}, nil, data)
default:
return nil, PacketEncodingError{fmt.Sprintf("unsupported compression codec (%d)", cc)}
}
2 changes: 1 addition & 1 deletion decompress.go
Original file line number Diff line number Diff line change
@@ -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)}
}
39 changes: 26 additions & 13 deletions zstd.go
Original file line number Diff line number Diff line change
@@ -6,32 +6,45 @@ import (
"github.com/klauspost/compress/zstd"
)

var zstdEncMap sync.Map
type ZstdEncoderParams struct {
Level int
}
type ZstdDecoderParams struct {
}

var (
zstdDec, _ = zstd.NewReader(nil)
)
var zstdEncMap, zstdDecMap sync.Map

func getEncoder(level int) *zstd.Encoder {
if ret, ok := zstdEncMap.Load(level); ok {
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 level != CompressionLevelDefault {
encoderLevel = zstd.EncoderLevelFromZstd(level)
if params.Level != CompressionLevelDefault {
encoderLevel = zstd.EncoderLevelFromZstd(params.Level)
}
zstdEnc, _ := zstd.NewWriter(nil, zstd.WithZeroFrames(true),
zstd.WithEncoderLevel(encoderLevel))
zstdEncMap.Store(level, zstdEnc)
zstdEncMap.Store(params, zstdEnc)
return zstdEnc
}

func zstdDecompress(dst, src []byte) ([]byte, error) {
return zstdDec.DecodeAll(src, dst)
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(params ZstdDecoderParams, dst, src []byte) ([]byte, error) {
return getDecoder(params).DecodeAll(src, dst)
}

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

0 comments on commit 60e10ba

Please sign in to comment.