From 732a6a36664b62af0877d0e1c6ba7d0c7c154cfe Mon Sep 17 00:00:00 2001
From: Evolution404 <35091674+Evolution404@users.noreply.github.com>
Date: Sun, 20 Jun 2021 21:59:00 +0800
Subject: [PATCH] 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>
---
 trie/trie.go | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/trie/trie.go b/trie/trie.go
index 7ed235fa8..e492a532c 100644
--- a/trie/trie.go
+++ b/trie/trie.go
@@ -405,6 +405,14 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
 		n.flags = t.newFlag()
 		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
 		// reduce the full node to a short node if only one entry is
 		// left. Since n must've contained at least two children
-- 
GitLab