trie: parallel commit

This commit is contained in:
MariusVanDerWijden 2026-02-26 17:50:39 +01:00
parent be92f5487e
commit 8e8031c107

View file

@ -88,8 +88,7 @@ func (c *committer) commit(path []byte, n node, parallel bool) node {
// commitChildren commits the children of the given fullnode // commitChildren commits the children of the given fullnode
func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) { func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) {
var ( var (
wg sync.WaitGroup index = make([]int, 0, 16)
nodesMu sync.Mutex
) )
for i := 0; i < 16; i++ { for i := 0; i < 16; i++ {
child := n.Children[i] child := n.Children[i]
@ -102,28 +101,35 @@ func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) {
if _, ok := child.(hashNode); ok { if _, ok := child.(hashNode); ok {
continue continue
} }
index = append(index, i)
}
if !parallel {
// Commit the child recursively and store the "hashed" value. // Commit the child recursively and store the "hashed" value.
// Note the returned node can be some embedded nodes, so it's // Note the returned node can be some embedded nodes, so it's
// possible the type is not hashNode. // possible the type is not hashNode.
if !parallel { for _, i := range index {
n.Children[i] = c.commit(append(path, byte(i)), child, false) n.Children[i] = c.commit(append(path, byte(i)), n.Children[i], false)
} else { }
wg.Add(1) } else {
var (
wg sync.WaitGroup
nodesMu sync.Mutex
)
wg.Add(len(index))
for _, i := range index {
go func(index int) { go func(index int) {
defer wg.Done() defer wg.Done()
p := append(path, byte(index)) p := append(path, byte(index))
childSet := trienode.NewNodeSet(c.nodes.Owner) childSet := trienode.NewNodeSet(c.nodes.Owner)
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, n.Children[i], false)
nodesMu.Lock() nodesMu.Lock()
c.nodes.MergeDisjoint(childSet) c.nodes.MergeDisjoint(childSet)
nodesMu.Unlock() nodesMu.Unlock()
}(i) }(i)
} }
}
if parallel {
wg.Wait() wg.Wait()
} }
} }