-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
the lock in that once per decode/encode is expensive, so just do it once at init Signed-off-by: bo <[email protected]>
- Loading branch information
Showing
1 changed file
with
2 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |