Skip to content

Commit

Permalink
core/state/snapshot: switch quadratic cleanup to linear algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe authored and holiman committed Feb 4, 2020
1 parent 52694c7 commit 405bc5f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
9 changes: 0 additions & 9 deletions core/state/snapshot/difflayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,6 @@ func (dl *diffLayer) Root() common.Hash {
// Stale return whether this layer has become stale (was flattened across) or if
// it's still live.
func (dl *diffLayer) Stale() bool {
// If the parent is stale, mark this layer as stale too
if stale := dl.parent.Stale(); stale {
dl.lock.Lock()
defer dl.lock.Unlock()

dl.stale = true
return true
}
// Parent not stale, return only whether we are
dl.lock.RLock()
defer dl.lock.RUnlock()

Expand Down
29 changes: 15 additions & 14 deletions core/state/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,24 +228,25 @@ func (t *Tree) Cap(root common.Hash, layers int, memory uint64) error {
// Many layers requested to be retained, cap normally
t.cap(diff, layers, memory)
}
// Layers have been capped and paths invalidated, remove stales
// Remove any layer that is stale or links into a stale layer
children := make(map[common.Hash][]common.Hash)
for root, snap := range t.layers {
if snap.Stale() {
delete(t.layers, root)
if diff, ok := snap.(*diffLayer); ok {
parent := diff.parent.Root()
children[parent] = append(children[parent], root)
}
}
// Remove anything that links into a stale.
// TODO(karalabe): this is super suboptimal
for {
done := true
for root, snap := range t.layers {
if snap.Stale() {
delete(t.layers, root)
done = false
}
var remove func(root common.Hash)
remove = func(root common.Hash) {
delete(t.layers, root)
for _, child := range children[root] {
remove(child)
}
if done {
break
delete(children, root)
}
for root, snap := range t.layers {
if snap.Stale() {
remove(root)
}
}
return nil
Expand Down

0 comments on commit 405bc5f

Please sign in to comment.