mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
trie: polish committer
This commit is contained in:
parent
2a7466d38c
commit
4bc5c5c374
1 changed files with 26 additions and 52 deletions
|
|
@ -87,33 +87,9 @@ func (c *committer) commit(path []byte, n node, parallel bool) node {
|
|||
|
||||
// commitChildren commits the children of the given fullnode
|
||||
func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) {
|
||||
if !parallel {
|
||||
// Sequential path: process children one by one
|
||||
for i := 0; i < 16; i++ {
|
||||
child := n.Children[i]
|
||||
if child == nil {
|
||||
continue
|
||||
}
|
||||
// If it's the hashed child, save the hash value directly.
|
||||
// Note: it's impossible that the child in range [0, 15]
|
||||
// is a valueNode. Values are only stored in Children[16]
|
||||
// (the 17th slot) when a key terminates at a branch node.
|
||||
// Children[0-15] are structural slots for hex digit routing.
|
||||
if _, ok := child.(hashNode); ok {
|
||||
continue
|
||||
}
|
||||
// Commit the child recursively and store the "hashed" value.
|
||||
// Note the returned node can be some embedded nodes, so it's
|
||||
// possible the type is not hashNode.
|
||||
n.Children[i] = c.commit(append(path, byte(i)), child, false)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Parallel path: collect all child NodeSets first, then merge without lock contention
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
nodeset = make([]*trienode.NodeSet, 16)
|
||||
subsets = make([]*trienode.NodeSet, 16)
|
||||
)
|
||||
for i := 0; i < 16; i++ {
|
||||
child := n.Children[i]
|
||||
|
|
@ -122,39 +98,37 @@ func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) {
|
|||
}
|
||||
// If it's the hashed child, save the hash value directly.
|
||||
// Note: it's impossible that the child in range [0, 15]
|
||||
// is a valueNode. Values are only stored in Children[16]
|
||||
// (the 17th slot) when a key terminates at a branch node.
|
||||
// Children[0-15] are structural slots for hex digit routing.
|
||||
// is a valueNode.
|
||||
if _, ok := child.(hashNode); ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// Commit the child recursively and store the "hashed" value.
|
||||
// Note the returned node can be some embedded nodes, so it's
|
||||
// possible the type is not hashNode.
|
||||
if !parallel {
|
||||
n.Children[i] = c.commit(append(path, byte(i)), child, false)
|
||||
} else {
|
||||
wg.Add(1)
|
||||
go func(index int, child node) {
|
||||
go func(index int) {
|
||||
defer wg.Done()
|
||||
|
||||
p := append(path, byte(index))
|
||||
childSet := trienode.NewNodeSet(c.nodes.Owner)
|
||||
childCommitter := newCommitter(childSet, c.tracer, c.collectLeaf)
|
||||
n.Children[index] = childCommitter.commit(p, child, false)
|
||||
|
||||
// Store the nodeset at the child's index. No mutex needed since
|
||||
// each goroutine writes to a unique index.
|
||||
nodeset[index] = childSet
|
||||
}(i, child)
|
||||
subset := trienode.NewNodeSet(c.nodes.Owner)
|
||||
subCom := newCommitter(subset, c.tracer, c.collectLeaf)
|
||||
n.Children[index] = subCom.commit(append(path, byte(index)), child, false)
|
||||
subsets[index] = subset
|
||||
}(i)
|
||||
}
|
||||
|
||||
// Wait for all goroutines to complete
|
||||
}
|
||||
if parallel {
|
||||
wg.Wait()
|
||||
|
||||
// Now merge all results sequentially without any lock contention
|
||||
// This is safe because all goroutines have completed
|
||||
for _, set := range nodeset {
|
||||
for _, set := range subsets {
|
||||
if set == nil {
|
||||
continue
|
||||
}
|
||||
c.nodes.MergeDisjoint(set)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// store hashes the node n and adds it to the modified nodeset. If leaf collection
|
||||
|
|
|
|||
Loading…
Reference in a new issue