Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reuse buffer #453

Merged
merged 2 commits into from Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions encoding.go
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
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
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
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
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
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