Skip to content

Commit

Permalink
reuse buffer (cosmos#453)
Browse files Browse the repository at this point in the history
* reuse buffer

* copy bytes

(cherry picked from commit 0568d39)
  • Loading branch information
tac0turtle authored and lizhi committed Aug 9, 2022
1 parent d994480 commit 7ecd0af
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
19 changes: 16 additions & 3 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
"sync"
)

var bufPool = &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}

var varintPool = &sync.Pool{
New: func() interface{} {
return &[binary.MaxVarintLen64]byte{}
Expand Down Expand Up @@ -93,9 +99,16 @@ func encodeBytes(w io.Writer, bz []byte) error {

// encodeBytesSlice length-prefixes the byte slice and returns it.
func encodeBytesSlice(bz []byte) ([]byte, error) {
var buf bytes.Buffer
err := encodeBytes(&buf, bz)
return buf.Bytes(), err
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)

err := encodeBytes(buf, bz)

bytesCopy := make([]byte, buf.Len())
copy(bytesCopy, buf.Bytes())

return bytesCopy, err
}

// encodeBytesSize returns the byte size of the given slice including length-prefixing.
Expand Down
13 changes: 9 additions & 4 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,18 @@ func (i *Importer) Add(exportNode *ExportNode) error {
return err
}

var buf bytes.Buffer
err = node.writeBytes(&buf)
if err != nil {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)

if err = node.writeBytes(buf); err != nil {
return err
}

if err = i.batch.Set(i.tree.ndb.nodeKey(node.hash), buf.Bytes()); err != nil {
bytesCopy := make([]byte, buf.Len())
copy(bytesCopy, buf.Bytes())

if err = i.batch.Set(i.tree.ndb.nodeKey(node.hash), bytesCopy); err != nil {
return err
}

Expand Down
10 changes: 8 additions & 2 deletions proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ func (pin ProofInnerNode) stringIndented(indent string) string {

func (pin ProofInnerNode) Hash(childHash []byte) []byte {
hasher := sha256.New()
buf := new(bytes.Buffer)

buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)

err := encodeVarint(buf, int64(pin.Height))
if err == nil {
Expand Down Expand Up @@ -145,7 +148,10 @@ func (pln ProofLeafNode) stringIndented(indent string) string {

func (pln ProofLeafNode) Hash() []byte {
hasher := sha256.New()
buf := new(bytes.Buffer)

buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)

err := encodeVarint(buf, 0)
if err == nil {
Expand Down
2 changes: 0 additions & 2 deletions proof_ics23_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ func GetKey(allkeys [][]byte, loc Where) []byte {
return allkeys[len(allkeys)-1]
}
// select a random index between 1 and allkeys-2
// nolint:gosec
idx := rand.Int()%(len(allkeys)-2) + 1
return allkeys[idx]
}
Expand Down Expand Up @@ -181,7 +180,6 @@ func BuildTree(size int) (itree *ImmutableTree, keys [][]byte, err error) {
for i := 0; i < size; i++ {
key := make([]byte, 4)
// create random 4 byte key
// nolint:gosec
rand.Read(key)
value := "value_for_key:" + string(key)
tree.Set(key, []byte(value))
Expand Down
1 change: 0 additions & 1 deletion testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func randBytes(length int) []byte {
key := make([]byte, length)
// math.rand.Read always returns err=nil
// we do not need cryptographic randomness for this test:
//nolint:gosec
mrand.Read(key)
return key
}
Expand Down
2 changes: 1 addition & 1 deletion tree_dotgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func WriteDOTGraph(w io.Writer, tree *ImmutableTree, paths []PathToLeaf) {
}
shortHash := graphNode.Hash[:7]

graphNode.Label = mkLabel(fmt.Sprintf("%s", node.key), 16, "sans-serif")
graphNode.Label = mkLabel(string(node.key), 16, "sans-serif")
graphNode.Label += mkLabel(shortHash, 10, "monospace")
graphNode.Label += mkLabel(fmt.Sprintf("version=%d", node.version), 10, "monospace")

Expand Down

0 comments on commit 7ecd0af

Please sign in to comment.