trie: simplify parallelism signalling

This commit is contained in:
Martin Holst Swende 2024-10-08 10:46:07 +02:00
parent dc9164d924
commit 3aa1cf71f7
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 7 additions and 9 deletions

View file

@ -31,26 +31,24 @@ type committer struct {
nodes *trienode.NodeSet nodes *trienode.NodeSet
tracer *tracer tracer *tracer
collectLeaf bool collectLeaf bool
parallel bool
} }
// newCommitter creates a new committer or picks one from the pool. // newCommitter creates a new committer or picks one from the pool.
func newCommitter(nodeset *trienode.NodeSet, tracer *tracer, collectLeaf bool, parallel bool) *committer { func newCommitter(nodeset *trienode.NodeSet, tracer *tracer, collectLeaf bool) *committer {
return &committer{ return &committer{
nodes: nodeset, nodes: nodeset,
tracer: tracer, tracer: tracer,
collectLeaf: collectLeaf, collectLeaf: collectLeaf,
parallel: parallel,
} }
} }
// Commit collapses a node down into a hash node. // Commit collapses a node down into a hash node.
func (c *committer) Commit(n node) hashNode { func (c *committer) Commit(n node, parallel bool) hashNode {
return c.commit(nil, n, true).(hashNode) return c.commit(nil, n, parallel).(hashNode)
} }
// commit collapses a node down into a hash node and returns it. // commit collapses a node down into a hash node and returns it.
func (c *committer) commit(path []byte, n node, topmost bool) node { func (c *committer) commit(path []byte, n node, parallel bool) node {
// if this path is clean, use available cached data // if this path is clean, use available cached data
hash, dirty := n.cache() hash, dirty := n.cache()
if hash != nil && !dirty { if hash != nil && !dirty {
@ -76,7 +74,7 @@ func (c *committer) commit(path []byte, n node, topmost bool) node {
} }
return collapsed return collapsed
case *fullNode: case *fullNode:
hashedKids := c.commitChildren(path, cn, topmost && c.parallel) hashedKids := c.commitChildren(path, cn, parallel)
collapsed := cn.copy() collapsed := cn.copy()
collapsed.Children = hashedKids collapsed.Children = hashedKids
@ -122,7 +120,7 @@ func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) [17]
go func(index int) { go func(index int) {
p := append(path, byte(index)) p := append(path, byte(index))
childSet := trienode.NewNodeSet(c.nodes.Owner) childSet := trienode.NewNodeSet(c.nodes.Owner)
childComitter := newCommitter(childSet, c.tracer, c.collectLeaf, false) childComitter := newCommitter(childSet, c.tracer, c.collectLeaf)
h := childComitter.commit(p, child, false) h := childComitter.commit(p, child, false)
children[index] = h children[index] = h
nodesMu.Lock() nodesMu.Lock()

View file

@ -648,7 +648,7 @@ func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
nodes.AddNode([]byte(path), trienode.NewDeleted()) nodes.AddNode([]byte(path), trienode.NewDeleted())
} }
// If the number of changes is below 100, we let one thread handle it // If the number of changes is below 100, we let one thread handle it
t.root = newCommitter(nodes, t.tracer, collectLeaf, t.uncommitted > 100).Commit(t.root) t.root = newCommitter(nodes, t.tracer, collectLeaf).Commit(t.root, t.uncommitted > 100)
t.uncommitted = 0 t.uncommitted = 0
return rootHash, nodes return rootHash, nodes
} }