Skip to content

Commit

Permalink
feat: zstd performance improvements
Browse files Browse the repository at this point in the history
the lock in that once per decode/encode is expensive, so just do it once
at init

Signed-off-by: bo <bo.blanton@gmail.com>
  • Loading branch information
wyndhblb committed Apr 28, 2021
1 parent 7285d6c commit 76988ca
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions zstd.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
package sarama

import (
"sync"

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

var (
zstdDec *zstd.Decoder
zstdEnc *zstd.Encoder

zstdEncOnce, zstdDecOnce sync.Once
)
var zstdDec, _ = zstd.NewReader(nil)
var zstdEnc, _ = zstd.NewWriter(nil, zstd.WithZeroFrames(true))

func zstdDecompress(dst, src []byte) ([]byte, error) {
zstdDecOnce.Do(func() {
zstdDec, _ = zstd.NewReader(nil)
})
return zstdDec.DecodeAll(src, dst)
}

func zstdCompress(dst, src []byte) ([]byte, error) {
zstdEncOnce.Do(func() {
zstdEnc, _ = zstd.NewWriter(nil, zstd.WithZeroFrames(true))
})
return zstdEnc.EncodeAll(src, dst), nil
}

0 comments on commit 76988ca

Please sign in to comment.