trie: preallocate capacity for fields slice (#29614)

trie: Preallocate capacity for fields slice
This commit is contained in:
qcrao 2024-04-24 20:04:20 +08:00 committed by GitHub
parent 0d4c38865e
commit ac21f9bfb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -78,7 +78,7 @@ func NewNodeSet(owner common.Hash) *NodeSet {
// ForEachWithOrder iterates the nodes with the order from bottom to top, // ForEachWithOrder iterates the nodes with the order from bottom to top,
// right to left, nodes with the longest path will be iterated first. // right to left, nodes with the longest path will be iterated first.
func (set *NodeSet) ForEachWithOrder(callback func(path string, n *Node)) { func (set *NodeSet) ForEachWithOrder(callback func(path string, n *Node)) {
var paths []string paths := make([]string, 0, len(set.Nodes))
for path := range set.Nodes { for path := range set.Nodes {
paths = append(paths, path) paths = append(paths, path)
} }
@ -133,7 +133,7 @@ func (set *NodeSet) Size() (int, int) {
// Hashes returns the hashes of all updated nodes. TODO(rjl493456442) how can // Hashes returns the hashes of all updated nodes. TODO(rjl493456442) how can
// we get rid of it? // we get rid of it?
func (set *NodeSet) Hashes() []common.Hash { func (set *NodeSet) Hashes() []common.Hash {
var ret []common.Hash ret := make([]common.Hash, 0, len(set.Nodes))
for _, node := range set.Nodes { for _, node := range set.Nodes {
ret = append(ret, node.Hash) ret = append(ret, node.Hash)
} }