Skip to content

Commit

Permalink
Do not copy when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Nov 12, 2021
1 parent 63e8db9 commit bee1fac
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lib/trie/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ func (b *branch) encodeAndHash() (encoding, hash []byte, err error) {
b.encoding = make([]byte, len(bufferBytes))
encoding = make([]byte, len(bufferBytes))
copy(b.encoding, bufferBytes)
copy(encoding, bufferBytes)
encoding = b.encoding // no need to copy

if buffer.Len() < 32 {
b.hash = make([]byte, len(bufferBytes))
hash = make([]byte, len(bufferBytes))
copy(b.hash, bufferBytes)
copy(hash, bufferBytes)
hash = b.hash // no need to copy
return encoding, hash, nil
}

Expand All @@ -259,7 +259,7 @@ func (b *branch) encodeAndHash() (encoding, hash []byte, err error) {
return nil, nil, err
}
b.hash = hashArray[:]
hash = hashArray[:]
hash = b.hash // no need to copy

return encoding, hash, nil
}
Expand All @@ -281,15 +281,13 @@ func (l *leaf) encodeAndHash() (encoding, hash []byte, err error) {
bufferBytes := buffer.Bytes()

l.encoding = make([]byte, len(bufferBytes))
encoding = make([]byte, len(bufferBytes))
copy(l.encoding, bufferBytes)
copy(encoding, bufferBytes)
encoding = l.encoding // no need to copy

if len(bufferBytes) < 32 {
l.hash = make([]byte, len(bufferBytes))
hash = make([]byte, len(bufferBytes))
copy(l.hash, bufferBytes)
copy(hash, bufferBytes)
hash = l.hash
return encoding, hash, nil
}

Expand All @@ -300,7 +298,7 @@ func (l *leaf) encodeAndHash() (encoding, hash []byte, err error) {
}

l.hash = hashArray[:]
hash = hashArray[:]
hash = l.hash // no need to copy

return encoding, hash, nil
}
Expand Down

0 comments on commit bee1fac

Please sign in to comment.