From 5c4588667db388c9ba691363fe5956d0491333aa Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 3 Oct 2024 15:18:18 +0200 Subject: [PATCH] trie/trienode: minor optimization in nodeset merging --- trie/trienode/node.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/trie/trienode/node.go b/trie/trienode/node.go index d0f4239682..2bf01be1db 100644 --- a/trie/trienode/node.go +++ b/trie/trienode/node.go @@ -102,6 +102,8 @@ func (set *NodeSet) AddNode(path []byte, n *Node) { set.Nodes[string(path)] = n } +// MergeSet mergest set with other. It assumes that the sets are disjunct, so +// that it does not need to deduplicate data (count deletes, dedup leaves etc). func (set *NodeSet) MergeSet(other *NodeSet) error { if set.Owner != other.Owner { return fmt.Errorf("nodesets belong to different owner are not mergeable %x-%x", set.Owner, other.Owner) @@ -109,22 +111,12 @@ func (set *NodeSet) MergeSet(other *NodeSet) error { set.mu.Lock() defer set.mu.Unlock() for path, node := range other.Nodes { - prev, ok := set.Nodes[path] - if ok { - // overwrite happens, revoke the counter - if prev.IsDeleted() { - set.deletes -= 1 - } else { - set.updates -= 1 - } - } - if node.IsDeleted() { - set.deletes += 1 - } else { - set.updates += 1 - } set.Nodes[path] = node } + set.deletes += other.deletes + set.updates += other.updates + // Since we assume the sets are disjunct, we can safely append leaves + // like this without dedup. set.Leaves = append(set.Leaves, other.Leaves...) return nil }