From 8b5fe2768a393d55a70a78700af3c7709e5dfc29 Mon Sep 17 00:00:00 2001 From: dboehm-avalabs Date: Mon, 10 Apr 2023 09:58:28 -0400 Subject: [PATCH 1/3] Adjust Logic In History No reason to do a search through the history entries if we know the start root entry is before the end root entry. This will be the case most of time since having the same root in the history multiple times should be pretty rare in normal usage. --- x/merkledb/history.go | 48 ++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/x/merkledb/history.go b/x/merkledb/history.go index fbaeca3bd8e3..9b721e30368a 100644 --- a/x/merkledb/history.go +++ b/x/merkledb/history.go @@ -96,28 +96,34 @@ func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start, end []b return nil, ErrRootIDNotPresent } - // [lastStartRootChange] is the latest appearance of [startRoot] - // which came before [lastEndRootChange]. - var lastStartRootChange *changeSummaryAndIndex - th.history.DescendLessOrEqual( - lastEndRootChange, - func(item *changeSummaryAndIndex) bool { - if item == lastEndRootChange { - return true // Skip first iteration - } - if item.rootID == startRoot { - lastStartRootChange = item - return false - } - return true - }, - ) - - // There's no change resulting in [startRoot] before the latest change resulting in [endRoot]. - if lastStartRootChange == nil { + // [lastStartRootChange] is the last appearance of [startRoot] + startRootChanges, ok := th.lastChanges[startRoot] + if !ok { return nil, ErrStartRootNotFound } + // lastStartRootChange is after the lastEndRootChange, but that is just the latest appearance of start root + // there may be an earlier entry, so attempt to find an entry that comes before lastEndRootChange + if startRootChanges.index > lastEndRootChange.index { + th.history.DescendLessOrEqual( + lastEndRootChange, + func(item *changeSummaryAndIndex) bool { + if item == lastEndRootChange { + return true // Skip first iteration + } + if item.rootID == startRoot { + startRootChanges = item + return false + } + return true + }, + ) + // There's no change resulting in [startRoot] before the latest change resulting in [endRoot]. + if startRootChanges.index > lastEndRootChange.index { + return nil, ErrStartRootNotFound + } + } + // Keep changes sorted so the largest can be removed in order to stay within the maxLength limit. sortedKeys := btree.NewG( 2, @@ -138,9 +144,9 @@ func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start, end []b // For each change after [lastStartRootChange] up to and including // [lastEndRootChange], record the change in [combinedChanges]. th.history.AscendGreaterOrEqual( - lastStartRootChange, + startRootChanges, func(item *changeSummaryAndIndex) bool { - if item == lastStartRootChange { + if item == startRootChanges { // Start from the first change after [lastStartRootChange]. return true } From 3a6252b25187eda9b6def107f5d8bcaec5616120 Mon Sep 17 00:00:00 2001 From: dboehm-avalabs Date: Mon, 10 Apr 2023 11:32:52 -0400 Subject: [PATCH 2/3] Update history.go --- x/merkledb/history.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x/merkledb/history.go b/x/merkledb/history.go index 9b721e30368a..7bccea6c2e32 100644 --- a/x/merkledb/history.go +++ b/x/merkledb/history.go @@ -96,13 +96,13 @@ func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start, end []b return nil, ErrRootIDNotPresent } - // [lastStartRootChange] is the last appearance of [startRoot] + // [startRootChanges] is the last appearance of [startRoot] startRootChanges, ok := th.lastChanges[startRoot] if !ok { return nil, ErrStartRootNotFound } - // lastStartRootChange is after the lastEndRootChange, but that is just the latest appearance of start root + // startRootChanges is after the lastEndRootChange, but that is just the latest appearance of start root // there may be an earlier entry, so attempt to find an entry that comes before lastEndRootChange if startRootChanges.index > lastEndRootChange.index { th.history.DescendLessOrEqual( @@ -141,13 +141,13 @@ func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start, end []b // Only the key-value pairs with the greatest [maxLength] keys will be kept. combinedChanges := newChangeSummary(maxLength) - // For each change after [lastStartRootChange] up to and including + // For each change after [startRootChanges] up to and including // [lastEndRootChange], record the change in [combinedChanges]. th.history.AscendGreaterOrEqual( startRootChanges, func(item *changeSummaryAndIndex) bool { if item == startRootChanges { - // Start from the first change after [lastStartRootChange]. + // Start from the first change after [startRootChanges]. return true } if item.index > lastEndRootChange.index { From e13e126803336dc6ce2b5193b8d5f7316e859dff Mon Sep 17 00:00:00 2001 From: David Boehm <91908103+dboehm-avalabs@users.noreply.github.com> Date: Tue, 11 Apr 2023 06:36:49 -0400 Subject: [PATCH 3/3] Update x/merkledb/history.go Co-authored-by: Darioush Jalali --- x/merkledb/history.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/merkledb/history.go b/x/merkledb/history.go index 7bccea6c2e32..e58dedbd3e19 100644 --- a/x/merkledb/history.go +++ b/x/merkledb/history.go @@ -102,7 +102,7 @@ func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start, end []b return nil, ErrStartRootNotFound } - // startRootChanges is after the lastEndRootChange, but that is just the latest appearance of start root + // startRootChanges is after the lastEndRootChange, but that is just the latest appearance of start root // there may be an earlier entry, so attempt to find an entry that comes before lastEndRootChange if startRootChanges.index > lastEndRootChange.index { th.history.DescendLessOrEqual(