Skip to content

Commit 2f3336a

Browse files
committed
core/state/snapshot: switch quadratic cleanup to linear algorithm
1 parent b5afdec commit 2f3336a

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

core/state/snapshot/difflayer.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,6 @@ func (dl *diffLayer) Root() common.Hash {
9898
// Stale return whether this layer has become stale (was flattened across) or if
9999
// it's still live.
100100
func (dl *diffLayer) Stale() bool {
101-
// If the parent is stale, mark this layer as stale too
102-
if stale := dl.parent.Stale(); stale {
103-
dl.lock.Lock()
104-
defer dl.lock.Unlock()
105-
106-
dl.stale = true
107-
return true
108-
}
109-
// Parent not stale, return only whether we are
110101
dl.lock.RLock()
111102
defer dl.lock.RUnlock()
112103

core/state/snapshot/snapshot.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -228,24 +228,25 @@ func (t *Tree) Cap(root common.Hash, layers int, memory uint64) error {
228228
// Many layers requested to be retained, cap normally
229229
t.cap(diff, layers, memory)
230230
}
231-
// Layers have been capped and paths invalidated, remove stales
231+
// Remove any layer that is stale or links into a stale layer
232+
children := make(map[common.Hash][]common.Hash)
232233
for root, snap := range t.layers {
233-
if snap.Stale() {
234-
delete(t.layers, root)
234+
if diff, ok := snap.(*diffLayer); ok {
235+
parent := diff.parent.Root()
236+
children[parent] = append(children[parent], root)
235237
}
236238
}
237-
// Remove anything that links into a stale.
238-
// TODO(karalabe): this is super suboptimal
239-
for {
240-
done := true
241-
for root, snap := range t.layers {
242-
if snap.Stale() {
243-
delete(t.layers, root)
244-
done = false
245-
}
239+
var remove func(root common.Hash)
240+
remove = func(root common.Hash) {
241+
delete(t.layers, root)
242+
for _, child := range children[root] {
243+
remove(child)
246244
}
247-
if done {
248-
break
245+
delete(children, root)
246+
}
247+
for root, snap := range t.layers {
248+
if snap.Stale() {
249+
remove(root)
249250
}
250251
}
251252
return nil

0 commit comments

Comments
 (0)