Skip to content

Commit

Permalink
Merge PR: iavl optimzie when delta disable (#1724)
Browse files Browse the repository at this point in the history
* calcHeightAndSize opt

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

* usafe string to slice

* test GetNode

* Revert "test GetNode"

This reverts commit ae5ee27.

* 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 73cd0df.

Co-authored-by: chengzhinei <[email protected]>
Co-authored-by: xiangjianmeng <[email protected]>
  • Loading branch information
3 people authored Mar 23, 2022
1 parent fcddf37 commit bfcbb63
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
14 changes: 10 additions & 4 deletions libs/iavl/mutable_tree_oec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions libs/iavl/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions libs/iavl/nodedb_oec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit bfcbb63

Please sign in to comment.