-
My code uses To demonstrate the issue I'm going to use the import (
"io"
"testing"
"github.com/klauspost/compress/zstd"
)
var data = []byte{1, 2, 3}
func newZstdWriter() (*zstd.Encoder, error) {
return zstd.NewWriter(
io.Discard,
zstd.WithEncoderLevel(zstd.SpeedBetterCompression),
zstd.WithEncoderConcurrency(16), // we implicitly get this concurrency level if we run on 16 core CPU
)
}
func BenchmarkMemWithFlush(b *testing.B) {
for i := 0; i < b.N; i++ {
w, err := newZstdWriter()
if err != nil {
b.Fatal(err)
}
for j := 0; j < 16; j++ {
w.Reset(io.Discard)
if _, err := w.Write(data); err != nil {
b.Fatal(err)
}
if err := w.Flush(); err != nil {
b.Fatal(err)
}
if err := w.Close(); err != nil {
b.Fatal(err)
}
}
}
}
func BenchmarkMemWithoutFlush(b *testing.B) {
for i := 0; i < b.N; i++ {
w, err := newZstdWriter()
if err != nil {
b.Fatal(err)
}
for j := 0; j < 16; j++ {
w.Reset(io.Discard)
if _, err := w.Write(data); err != nil {
b.Fatal(err)
}
if err := w.Close(); err != nil {
b.Fatal(err)
}
}
}
} Running the benchmark locally I get the following result:
Note that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
If you just intend to use it for a stream, use |
Beta Was this translation helpful? Give feedback.
#1007 should eliminate the extra allocs on small encodes.