Skip to content

Commit

Permalink
Reuse buffer from pool when encoding elements
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Oct 2, 2023
1 parent 2775ff5 commit 836eb70
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
26 changes: 23 additions & 3 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"math"
"strings"
"sync"

"github.com/fxamacker/cbor/v2"
)
Expand Down Expand Up @@ -220,6 +221,23 @@ type Array struct {
mutableElementIndex map[ValueID]uint64
}

var bufferPool = sync.Pool{
New: func() interface{} {
e := new(bytes.Buffer)
e.Grow(int(maxThreshold))
return e
},
}

func getBuffer() *bytes.Buffer {
return bufferPool.Get().(*bytes.Buffer)
}

func putBuffer(e *bytes.Buffer) {
e.Reset()
bufferPool.Put(e)
}

var _ Value = &Array{}
var _ mutableValueNotifier = &Array{}

Expand Down Expand Up @@ -775,9 +793,11 @@ func (a *ArrayDataSlab) Encode(enc *Encoder) error {

inlinedTypes := newInlinedExtraData()

// TODO: maybe use a buffer pool
var elementBuf bytes.Buffer
elementEnc := NewEncoder(&elementBuf, enc.encMode)
// Get a buffer from a pool to encode elements.
elementBuf := getBuffer()
defer putBuffer(elementBuf)

elementEnc := NewEncoder(elementBuf, enc.encMode)

err := a.encodeElements(elementEnc, inlinedTypes)
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package atree

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
Expand Down Expand Up @@ -2683,9 +2682,11 @@ func (m *MapDataSlab) Encode(enc *Encoder) error {

inlinedTypes := newInlinedExtraData()

// TODO: maybe use a buffer pool
var buf bytes.Buffer
elemEnc := NewEncoder(&buf, enc.encMode)
// Get a buffer from a pool to encode elements.
elementBuf := getBuffer()
defer putBuffer(elementBuf)

elemEnc := NewEncoder(elementBuf, enc.encMode)

err := m.encodeElements(elemEnc, inlinedTypes)
if err != nil {
Expand Down Expand Up @@ -2763,7 +2764,7 @@ func (m *MapDataSlab) Encode(enc *Encoder) error {
}

// Encode elements
err = enc.CBOR.EncodeRawBytes(buf.Bytes())
err = enc.CBOR.EncodeRawBytes(elementBuf.Bytes())
if err != nil {
return NewEncodingError(err)
}
Expand Down

0 comments on commit 836eb70

Please sign in to comment.