Skip to content

Commit

Permalink
fix: reduce the scope of map lock
Browse files Browse the repository at this point in the history
  • Loading branch information
will@2012 committed May 8, 2024
1 parent 18bfa80 commit b17d716
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions trie/triedb/pathdb/layertree.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ func (tree *layerTree) cap(root common.Hash, layers int) error {
if !ok {
return fmt.Errorf("triedb layer [%#x] is disk layer", root)
}
tree.lock.Lock()
defer tree.lock.Unlock()

// If full commit was requested, flatten the diffs and merge onto disk
if layers == 0 {
Expand All @@ -133,7 +131,9 @@ func (tree *layerTree) cap(root common.Hash, layers int) error {
return err
}
// Replace the entire layer tree with the flat base
tree.lock.Lock()
tree.layers = map[common.Hash]layer{base.rootHash(): base}
tree.lock.Unlock()
return nil
}
// Dive until we run out of layers or reach the persistent database
Expand Down Expand Up @@ -162,14 +162,20 @@ func (tree *layerTree) cap(root common.Hash, layers int) error {
diff.lock.Unlock()
return err
}
tree.lock.Lock()
tree.layers[base.rootHash()] = base
tree.lock.Unlock()

diff.parent = base

diff.lock.Unlock()

default:
panic(fmt.Sprintf("unknown data layer in triedb: %T", parent))
}

tree.lock.Lock()
defer tree.lock.Unlock()
// Remove any layer that is stale or links into a stale layer
children := make(map[common.Hash][]common.Hash)
for root, layer := range tree.layers {
Expand All @@ -196,18 +202,17 @@ func (tree *layerTree) cap(root common.Hash, layers int) error {

// bottom returns the bottom-most disk layer in this tree.
func (tree *layerTree) bottom() *diskLayer {
tree.lock.RLock()
defer tree.lock.RUnlock()

if len(tree.layers) == 0 {
if tree.len() == 0 {
return nil // Shouldn't happen, empty tree
}
// pick a random one as the entry point
var current layer
tree.lock.RLock()
for _, layer := range tree.layers {
current = layer
break
}
tree.lock.RUnlock()
for current.parentLayer() != nil {
current = current.parentLayer()
}
Expand Down

0 comments on commit b17d716

Please sign in to comment.