trie: small optimization of delete in fullNode case (#22979)

When deleting in fullNode, and the new child node nn is not nil, there is no need
to check the number of non-nil entries in the node. This is because the fullNode 
must've contained at least two children before deletion, so there must be another
child node other than nn.

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
Evolution404 2021-06-20 21:59:00 +08:00 committed by GitHub
parent 7b6c8363da
commit 732a6a3666
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -405,6 +405,14 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
n.flags = t.newFlag() n.flags = t.newFlag()
n.Children[key[0]] = nn n.Children[key[0]] = nn
// Because n is a full node, it must've contained at least two children
// before the delete operation. If the new child value is non-nil, n still
// has at least two children after the deletion, and cannot be reduced to
// a short node.
if nn != nil {
return true, n, nil
}
// Reduction:
// Check how many non-nil entries are left after deleting and // Check how many non-nil entries are left after deleting and
// reduce the full node to a short node if only one entry is // reduce the full node to a short node if only one entry is
// left. Since n must've contained at least two children // left. Since n must've contained at least two children