Skip to content

Commit

Permalink
Reduce lock contentions in ompt/mpt.Flush()
Browse files Browse the repository at this point in the history
  • Loading branch information
mksong76 committed Aug 22, 2023
1 parent 7e521e2 commit a7de989
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
3 changes: 2 additions & 1 deletion common/trie/ompt/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ func (n *branch) flush(m *mpt, nibs []byte) error {
return err
}
}
lock.Migrate()
if err := n.nodeBase.flushBaseInLock(m, nibs); err != nil {
return err
}
lock.Migrate()
n.state = stateFlushed
return nil
}

Expand Down
6 changes: 4 additions & 2 deletions common/trie/ompt/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (n *extension) freeze() {
}

func (n *extension) flush(m *mpt, nibs []byte) error {
n.mutex.Lock()
defer n.mutex.Unlock()
lock := n.rlock()
defer lock.Unlock()
if n.state == stateFlushed {
return nil
}
Expand All @@ -90,6 +90,8 @@ func (n *extension) flush(m *mpt, nibs []byte) error {
if err := n.nodeBase.flushBaseInLock(m, nil); err != nil {
return err
}
lock.Migrate()
n.state = stateFlushed
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion common/trie/ompt/leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ func (n *leaf) flush(m *mpt, nibs []byte) error {
if err := n.value.Flush(); err != nil {
return err
}
lock.Migrate()
if err := n.nodeBase.flushBaseInLock(m, nil); err != nil {
return err
}
lock.Migrate()
n.state = stateFlushed
return nil
}

Expand Down
15 changes: 8 additions & 7 deletions common/trie/ompt/mpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ func (m *mpt) Get(k []byte) (trie.Object, error) {
}

func (m *mpt) Hash() []byte {
m.mutex.Lock()
defer m.mutex.Unlock()
m.mutex.RLock()
defer m.mutex.RUnlock()
if m.root != nil {
return m.root.getLink(true)
} else {
Expand All @@ -183,8 +183,8 @@ func (m *mpt) Hash() []byte {
}

func (m *mpt) Flush() error {
m.mutex.Lock()
defer m.mutex.Unlock()
m.mutex.RLock()
defer m.mutex.RUnlock()
if m.root != nil {
// Before flush node data to Database, We need to make sure that it
// builds required data for dumping data.
Expand Down Expand Up @@ -415,12 +415,13 @@ func (m *mpt) Iterator() trie.IteratorForObject {
}

func (m *mpt) Filter(prefix []byte) trie.IteratorForObject {
m.mutex.Lock()
defer m.mutex.Unlock()
lock := RLock(&m.mutex)
defer lock.Unlock()

root := m.root
if root != nil {
if n, err := root.realize(m); err == nil {
if n, err := root.realize(m); err == nil && n != root {
lock.Migrate()
root = n
m.root = n
}
Expand Down
1 change: 0 additions & 1 deletion common/trie/ompt/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func (n *nodeBase) flushBaseInLock(m *mpt, nibs []byte) error {
}
m.cache.Put(nibs, n.hashValue, n.serialized)
}
n.state = stateFlushed
return nil
}

Expand Down

0 comments on commit a7de989

Please sign in to comment.