From 8e8031c1078971bae116096e6fd4e45cfe2aeaa9 Mon Sep 17 00:00:00 2001 From: MariusVanDerWijden Date: Thu, 26 Feb 2026 17:50:39 +0100 Subject: [PATCH] trie: parallel commit --- trie/committer.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/trie/committer.go b/trie/committer.go index 2a2142e0ff..11799be74b 100644 --- a/trie/committer.go +++ b/trie/committer.go @@ -88,8 +88,7 @@ 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) { var ( - wg sync.WaitGroup - nodesMu sync.Mutex + index = make([]int, 0, 16) ) for i := 0; i < 16; i++ { child := n.Children[i] @@ -102,28 +101,35 @@ func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) { if _, ok := child.(hashNode); ok { continue } + index = append(index, i) + } + if !parallel { // 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) + for _, i := range index { + n.Children[i] = c.commit(append(path, byte(i)), n.Children[i], false) + } + } else { + var ( + wg sync.WaitGroup + nodesMu sync.Mutex + ) + wg.Add(len(index)) + for _, i := range index { 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) + n.Children[index] = childCommitter.commit(p, n.Children[i], false) nodesMu.Lock() c.nodes.MergeDisjoint(childSet) nodesMu.Unlock() }(i) } - } - if parallel { wg.Wait() } }