Skip to content

Commit

Permalink
Merge pull request #124 from eos175/master
Browse files Browse the repository at this point in the history
add buffer pool to encoder for improved performance
  • Loading branch information
AmarnathCJD authored Jul 30, 2024
2 parents d0c3a93 + eccf79f commit 5b08ff7
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions internal/encoding/tl/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@ import (
"bytes"
"fmt"
"reflect"
"sync"

"github.com/pkg/errors"
)

var bufferPool = sync.Pool{
New: func() any {
return bytes.NewBuffer(make([]byte, 8*1024)) // 8kb
},
}

func Marshal(v any) ([]byte, error) {
buf := bytes.NewBuffer(nil)
buf := bufferPool.Get().(*bytes.Buffer)
defer bufferPool.Put(buf)
buf.Reset()

encoder := NewEncoder(buf)
encoder.encodeValue(reflect.ValueOf(v))
if err := encoder.CheckErr(); err != nil {
return nil, err
}

return buf.Bytes(), nil
return bytes.Clone(buf.Bytes()), nil
}

func (c *Encoder) encodeValue(value reflect.Value) {
Expand Down

0 comments on commit 5b08ff7

Please sign in to comment.