From bee1fac1301dcc222eb8e1a0e79010e680274752 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Fri, 12 Nov 2021 13:19:53 +0000 Subject: [PATCH] Do not copy when not needed --- lib/trie/node.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/trie/node.go b/lib/trie/node.go index 8fcaa0c715..efe88c2df9 100644 --- a/lib/trie/node.go +++ b/lib/trie/node.go @@ -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 } @@ -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 } @@ -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 } @@ -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 }