upgrade newFastIterator

This commit is contained in:
maskpp 2024-05-04 10:48:51 +08:00
parent 86a1f0c394
commit 517694b2a2

View file

@ -77,13 +77,21 @@ func newFastIterator(tree *Tree, root common.Hash, account common.Hash, seek com
if snap == nil { if snap == nil {
return nil, fmt.Errorf("unknown snapshot: %x", root) return nil, fmt.Errorf("unknown snapshot: %x", root)
} }
// Get all the ancestor snaps.
snaps := make([]snapshot, 0, 128)
for current := snap.(snapshot); current != nil; current = current.Parent() {
snaps = append(snaps, current)
}
fi := &fastIterator{ fi := &fastIterator{
tree: tree, tree: tree,
root: root, root: root,
account: accountIterator, account: accountIterator,
iterators: make([]*weightedIterator, 0, len(snaps)),
} }
current := snap.(snapshot)
for depth := 0; current != nil; depth++ { for depth, current := range snaps {
if accountIterator { if accountIterator {
fi.iterators = append(fi.iterators, &weightedIterator{ fi.iterators = append(fi.iterators, &weightedIterator{
it: current.AccountIterator(seek), it: current.AccountIterator(seek),
@ -103,7 +111,6 @@ func newFastIterator(tree *Tree, root common.Hash, account common.Hash, seek com
break break
} }
} }
current = current.Parent()
} }
fi.init() fi.init()
return fi, nil return fi, nil