mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 12:46:44 +00:00
simplify by omitting mutex
This commit is contained in:
parent
13a581e28a
commit
ae97dfa234
1 changed files with 14 additions and 16 deletions
|
|
@ -111,14 +111,10 @@ func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parallel path: collect all child NodeSets first, then merge without lock contention
|
// Parallel path: collect all child NodeSets first, then merge without lock contention
|
||||||
type childResult struct {
|
var (
|
||||||
index int
|
wg sync.WaitGroup
|
||||||
set *trienode.NodeSet
|
nodeset = make([]*trienode.NodeSet, 16)
|
||||||
}
|
)
|
||||||
results := make([]childResult, 0, 16)
|
|
||||||
resultsMu := sync.Mutex{}
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
|
||||||
for i := 0; i < 16; i++ {
|
for i := 0; i < 16; i++ {
|
||||||
child := n.Children[i]
|
child := n.Children[i]
|
||||||
if child == nil {
|
if child == nil {
|
||||||
|
|
@ -134,7 +130,7 @@ func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(index int) {
|
go func(index int, child node) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
p := append(path, byte(index))
|
p := append(path, byte(index))
|
||||||
|
|
@ -142,11 +138,10 @@ func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) {
|
||||||
childCommitter := newCommitter(childSet, c.tracer, c.collectLeaf)
|
childCommitter := newCommitter(childSet, c.tracer, c.collectLeaf)
|
||||||
n.Children[index] = childCommitter.commit(p, child, false)
|
n.Children[index] = childCommitter.commit(p, child, false)
|
||||||
|
|
||||||
// Collect results without merging yet - only lock to append to slice
|
// Store the nodeset at the child's index. No mutex needed since
|
||||||
resultsMu.Lock()
|
// each goroutine writes to a unique index.
|
||||||
results = append(results, childResult{index: index, set: childSet})
|
nodeset[index] = childSet
|
||||||
resultsMu.Unlock()
|
}(i, child)
|
||||||
}(i)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for all goroutines to complete
|
// Wait for all goroutines to complete
|
||||||
|
|
@ -154,8 +149,11 @@ func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) {
|
||||||
|
|
||||||
// Now merge all results sequentially without any lock contention
|
// Now merge all results sequentially without any lock contention
|
||||||
// This is safe because all goroutines have completed
|
// This is safe because all goroutines have completed
|
||||||
for _, result := range results {
|
for _, set := range nodeset {
|
||||||
c.nodes.MergeDisjoint(result.set)
|
if set == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
c.nodes.MergeDisjoint(set)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue