Skip to content

Commit

Permalink
Merge pull request #382 from fxamacker/fxamacker/return-encoder-buffe…
Browse files Browse the repository at this point in the history
…r-to-pool

Return buffer to pool when using Encoder
  • Loading branch information
fxamacker authored Jan 2, 2023
2 parents f57ecbe + 52c6053 commit 9c7f31b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
29 changes: 26 additions & 3 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package cbor

import (
"bytes"
"io/ioutil"
"io"
"reflect"
"testing"
)
Expand Down Expand Up @@ -415,6 +415,29 @@ func BenchmarkMarshalCanonical(b *testing.B) {
}
}

// BenchmarkNewEncoderEncode benchmarks NewEncoder() and Encode().
func BenchmarkNewEncoderEncode(b *testing.B) {
for _, bm := range encodeBenchmarks {
for _, v := range bm.values {
name := "Go " + reflect.TypeOf(v).String() + " to CBOR " + bm.name
if reflect.TypeOf(v).Kind() == reflect.Struct {
name = "Go " + reflect.TypeOf(v).Kind().String() + " to CBOR " + bm.name
}
b.Run(name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
encoder := NewEncoder(io.Discard)
if err := encoder.Encode(v); err != nil {
b.Fatal("Encode:", err)
}
}
})
}
}
}

// BenchmarkEncode benchmarks Encode(). It reuses same Encoder to exclude NewEncoder()
// from the benchmark.
func BenchmarkEncode(b *testing.B) {
for _, bm := range encodeBenchmarks {
for _, v := range bm.values {
Expand All @@ -423,7 +446,7 @@ func BenchmarkEncode(b *testing.B) {
name = "Go " + reflect.TypeOf(v).Kind().String() + " to CBOR " + bm.name
}
b.Run(name, func(b *testing.B) {
encoder := NewEncoder(ioutil.Discard)
encoder := NewEncoder(io.Discard)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := encoder.Encode(v); err != nil {
Expand All @@ -437,7 +460,7 @@ func BenchmarkEncode(b *testing.B) {

func BenchmarkEncodeStream(b *testing.B) {
for i := 0; i < b.N; i++ {
encoder := NewEncoder(ioutil.Discard)
encoder := NewEncoder(io.Discard)
for i := 0; i < rounds; i++ {
for _, bm := range encodeBenchmarks {
for _, v := range bm.values {
Expand Down
2 changes: 1 addition & 1 deletion encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func (em *encMode) Marshal(v interface{}) ([]byte, error) {

// NewEncoder returns a new encoder that writes to w using em EncMode.
func (em *encMode) NewEncoder(w io.Writer) *Encoder {
return &Encoder{w: w, em: em, e: getEncoderBuffer()}
return &Encoder{w: w, em: em}
}

type encoderBuffer struct {
Expand Down
10 changes: 6 additions & 4 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ func (dec *Decoder) overwriteBuf(newBuf []byte) {
type Encoder struct {
w io.Writer
em *encMode
e *encoderBuffer
indefTypes []cborType
}

Expand All @@ -157,11 +156,14 @@ func (enc *Encoder) Encode(v interface{}) error {
}
}

err := encode(enc.e, enc.em, reflect.ValueOf(v))
buf := getEncoderBuffer()

err := encode(buf, enc.em, reflect.ValueOf(v))
if err == nil {
_, err = enc.e.WriteTo(enc.w)
_, err = enc.w.Write(buf.Bytes())
}
enc.e.Reset()

putEncoderBuffer(buf)
return err
}

Expand Down

0 comments on commit 9c7f31b

Please sign in to comment.