From bfcbb638e741964baab783caaca28d0214692db7 Mon Sep 17 00:00:00 2001
From: cwbhhjl <cwbhhjl@gmail.com>
Date: Wed, 23 Mar 2022 17:43:21 +0800
Subject: [PATCH] Merge PR: iavl optimzie when delta disable (#1724)

* calcHeightAndSize opt

* if delta off, don't use map of saveNodes

* usafe string to slice

* test GetNode

* Revert "test GetNode"

This reverts commit ae5ee27bcfca7e130bb357f83a34cc2beb9bffc5.

* if produceDelta is false, we don't need to update tree.deltas

* don't need to generate empty slice

* Revert "don't need to generate empty slice"

This reverts commit 73cd0df5a5597c11141328f13a6d878e04e1af91.

Co-authored-by: chengzhinei <chengxue5yuan@163.com>
Co-authored-by: xiangjianmeng <805442788@qq.com>
---
 libs/iavl/mutable_tree_oec.go | 14 ++++++++++----
 libs/iavl/node.go             |  6 ++++--
 libs/iavl/nodedb_oec.go       | 10 +++++++---
 3 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/libs/iavl/mutable_tree_oec.go b/libs/iavl/mutable_tree_oec.go
index f7410ccf43..091b195f47 100644
--- a/libs/iavl/mutable_tree_oec.go
+++ b/libs/iavl/mutable_tree_oec.go
@@ -53,8 +53,10 @@ func (tree *MutableTree) SaveVersionAsync(version int64, useDeltas bool) ([]byte
 	if tree.root != nil {
 		if useDeltas {
 			tree.updateBranchWithDelta(tree.root)
-		} else {
+		} else if produceDelta {
 			tree.ndb.updateBranchConcurrency(tree.root, tree.savedNodes)
+		} else {
+			tree.ndb.updateBranchConcurrency(tree.root, nil)
 		}
 
 		// generate state delta
@@ -82,7 +84,9 @@ func (tree *MutableTree) SaveVersionAsync(version int64, useDeltas bool) ([]byte
 	tree.ImmutableTree = tree.ImmutableTree.clone()
 	tree.lastSaved = tree.ImmutableTree.clone()
 	tree.orphans = []*Node{}
-	tree.savedNodes = map[string]*Node{}
+	for k := range tree.savedNodes {
+		delete(tree.savedNodes, k)
+	}
 
 	rootHash := tree.lastSaved.Hash()
 	tree.setHeightOrphansItem(version, rootHash)
@@ -278,8 +282,10 @@ func (tree *MutableTree) addOrphansOptimized(orphans []*Node) {
 			if node.persisted && EnablePruningHistoryState {
 				k := string(node.hash)
 				tree.commitOrphans[k] = node.version
-				commitOrp := &CommitOrphansImp{Key: k, CommitValue: node.version}
-				tree.deltas.CommitOrphansDelta = append(tree.deltas.CommitOrphansDelta, commitOrp)
+				if produceDelta {
+					commitOrp := &CommitOrphansImp{Key: k, CommitValue: node.version}
+					tree.deltas.CommitOrphansDelta = append(tree.deltas.CommitOrphansDelta, commitOrp)
+				}
 			}
 		}
 
diff --git a/libs/iavl/node.go b/libs/iavl/node.go
index c71a5202f5..f213295acb 100644
--- a/libs/iavl/node.go
+++ b/libs/iavl/node.go
@@ -560,8 +560,10 @@ func (node *Node) getRightNode(t *ImmutableTree) *Node {
 
 // NOTE: mutates height and size
 func (node *Node) calcHeightAndSize(t *ImmutableTree) {
-	node.height = maxInt8(node.getLeftNode(t).height, node.getRightNode(t).height) + 1
-	node.size = node.getLeftNode(t).size + node.getRightNode(t).size
+	left := node.getLeftNode(t)
+	right := node.getRightNode(t)
+	node.height = maxInt8(left.height, right.height) + 1
+	node.size = left.size + right.size
 }
 
 func (node *Node) calcBalance(t *ImmutableTree) int {
diff --git a/libs/iavl/nodedb_oec.go b/libs/iavl/nodedb_oec.go
index 82596e5d96..4f78647e37 100644
--- a/libs/iavl/nodedb_oec.go
+++ b/libs/iavl/nodedb_oec.go
@@ -395,7 +395,9 @@ func (ndb *nodeDB) updateBranchConcurrency(node *Node, savedNodes map[string]*No
 					ndb.saveNodeToPrePersistCache(n)
 					n.leftNode = nil
 					n.rightNode = nil
-					savedNodes[string(n.hash)] = n
+					if savedNodes != nil {
+						savedNodes[string(n.hash)] = n
+					}
 				}
 			}
 		}(wg, needNilNodeNum, savedNodes, ndb, nodeCh)
@@ -420,7 +422,9 @@ func (ndb *nodeDB) updateBranchConcurrency(node *Node, savedNodes map[string]*No
 	node.rightNode = nil
 
 	// TODO: handle magic number
-	savedNodes[string(node.hash)] = node
+	if savedNodes != nil {
+		savedNodes[string(node.hash)] = node
+	}
 
 	return node.hash
 }
@@ -488,7 +492,7 @@ func (ndb *nodeDB) saveCommitOrphans(batch dbm.Batch, version int64, orphans map
 	toVersion := ndb.getPreviousVersion(version)
 	for hash, fromVersion := range orphans {
 		ndb.log(IavlDebug, "SAVEORPHAN", "from", fromVersion, "to", toVersion, "hash", amino.BytesHexStringer(amino.StrToBytes(hash)))
-		ndb.saveOrphan(batch, []byte(hash), fromVersion, toVersion)
+		ndb.saveOrphan(batch, amino.StrToBytes(hash), fromVersion, toVersion)
 	}
 }