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

New nodes map with capacity #29704

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ func (s *StateDB) fastDeleteStorage(addrHash common.Hash, root common.Hash) (com

var (
size common.StorageSize
nodes = trienode.NewNodeSet(addrHash)
nodes = trienode.NewNodeSet(addrHash, 0)
slots = make(map[common.Hash][]byte)
)
stack := trie.NewStackTrie(func(path []byte, hash common.Hash, blob []byte) {
Expand Down Expand Up @@ -989,7 +989,7 @@ func (s *StateDB) slowDeleteStorage(addr common.Address, addrHash common.Hash, r
}
var (
size common.StorageSize
nodes = trienode.NewNodeSet(addrHash)
nodes = trienode.NewNodeSet(addrHash, 0)
slots = make(map[common.Hash][]byte)
)
for it.Next(true) {
Expand Down
4 changes: 2 additions & 2 deletions trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error)
if len(paths) == 0 {
return types.EmptyRootHash, nil, nil // case (a)
}
nodes := trienode.NewNodeSet(t.owner)
nodes := trienode.NewNodeSet(t.owner, len(paths))
for _, path := range paths {
nodes.AddNode([]byte(path), trienode.NewDeleted())
}
Expand All @@ -640,7 +640,7 @@ func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error)
t.root = hashedNode
return rootHash, nil, nil
}
nodes := trienode.NewNodeSet(t.owner)
nodes := trienode.NewNodeSet(t.owner, len(t.tracer.deletedNodes()))
for _, path := range t.tracer.deletedNodes() {
nodes.AddNode([]byte(path), trienode.NewDeleted())
}
Expand Down
6 changes: 3 additions & 3 deletions trie/trienode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ type NodeSet struct {

// NewNodeSet initializes a node set. The owner is zero for the account trie and
// the owning account address hash for storage tries.
func NewNodeSet(owner common.Hash) *NodeSet {
func NewNodeSet(owner common.Hash, capacity int) *NodeSet {
return &NodeSet{
Owner: owner,
Nodes: make(map[string]*Node),
Nodes: make(map[string]*Node, capacity),
}
}

Expand Down Expand Up @@ -186,7 +186,7 @@ func (set *MergedNodeSet) Merge(other *NodeSet) error {

// Flatten returns a two-dimensional map for internal nodes.
func (set *MergedNodeSet) Flatten() map[common.Hash]map[string]*Node {
nodes := make(map[common.Hash]map[string]*Node)
nodes := make(map[common.Hash]map[string]*Node, len(set.Sets))
for owner, set := range set.Sets {
nodes[owner] = set.Nodes
}
Expand Down
6 changes: 3 additions & 3 deletions trie/trienode/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func BenchmarkMerge(b *testing.B) {
}

func benchmarkMerge(b *testing.B, count int) {
x := NewNodeSet(common.Hash{})
y := NewNodeSet(common.Hash{})
x := NewNodeSet(common.Hash{}, count)
y := NewNodeSet(common.Hash{}, count)
addNode := func(s *NodeSet) {
path := make([]byte, 4)
rand.Read(path)
Expand All @@ -52,7 +52,7 @@ func benchmarkMerge(b *testing.B, count int) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Store set x into a backup
z := NewNodeSet(common.Hash{})
z := NewNodeSet(common.Hash{}, len(x.Nodes))
z.Merge(common.Hash{}, x.Nodes)
// Merge y into x
x.Merge(common.Hash{}, y.Nodes)
Expand Down
2 changes: 1 addition & 1 deletion trie/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (t *VerkleTrie) Commit(_ bool) (common.Hash, *trienode.NodeSet, error) {
if err != nil {
return common.Hash{}, nil, fmt.Errorf("serializing tree nodes: %s", err)
}
nodeset := trienode.NewNodeSet(common.Hash{})
nodeset := trienode.NewNodeSet(common.Hash{}, len(nodes))
for _, node := range nodes {
// hash parameter is not used in pathdb
nodeset.AddNode(node.Path, trienode.New(common.Hash{}, node.SerializedBytes))
Expand Down
2 changes: 1 addition & 1 deletion triedb/pathdb/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (h *testHasher) Delete(key []byte) error {
func (h *testHasher) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error) {
var (
nodes = make(map[common.Hash][]byte)
set = trienode.NewNodeSet(h.owner)
set = trienode.NewNodeSet(h.owner, len(h.dirties))
)
for hash, val := range h.cleans {
nodes[hash] = val
Expand Down