Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve efficiency of the scan for dirty nodes #941

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions go/database/mpt/forest.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,7 @@ func (s *Forest) Flush() error {
// Get snapshot of set of dirty Node IDs.
ids := make([]NodeId, 0, 1<<16)
s.nodeCache.ForEach(func(id NodeId, node *shared.Shared[Node]) {
handle := node.GetViewHandle()
dirty := handle.Get().IsDirty()
handle.Release()
if dirty {
if node.GetUnprotected().IsDirty() {
ids = append(ids, id)
}
})
Expand All @@ -553,6 +550,10 @@ func (s *Forest) flushDirtyIds(ids []NodeId) error {
if present {
handle := node.GetWriteHandle()
node := handle.Get()
if !node.IsDirty() {
handle.Release()
continue
}
err := s.flushNode(id, node)
if err == nil {
node.MarkClean()
Expand Down Expand Up @@ -910,7 +911,7 @@ func (s *Forest) createAccount() (NodeReference, shared.WriteHandle[Node], error
instance, present := s.addToCache(&ref, shared.MakeShared[Node](node))
if present {
write := instance.GetWriteHandle()
*write.Get().(*AccountNode) = *node
write.Get().Reset()
write.Release()
}
return ref, instance.GetWriteHandle(), err
Expand All @@ -926,7 +927,7 @@ func (s *Forest) createBranch() (NodeReference, shared.WriteHandle[Node], error)
instance, present := s.addToCache(&ref, shared.MakeShared[Node](node))
if present {
write := instance.GetWriteHandle()
*write.Get().(*BranchNode) = *node
write.Get().Reset()
write.Release()
}
return ref, instance.GetWriteHandle(), err
Expand All @@ -942,7 +943,7 @@ func (s *Forest) createExtension() (NodeReference, shared.WriteHandle[Node], err
instance, present := s.addToCache(&ref, shared.MakeShared[Node](node))
if present {
write := instance.GetWriteHandle()
*write.Get().(*ExtensionNode) = *node
write.Get().Reset()
write.Release()
}
return ref, instance.GetWriteHandle(), err
Expand All @@ -958,7 +959,7 @@ func (s *Forest) createValue() (NodeReference, shared.WriteHandle[Node], error)
instance, present := s.addToCache(&ref, shared.MakeShared[Node](node))
if present {
write := instance.GetWriteHandle()
*write.Get().(*ValueNode) = *node
write.Get().Reset()
write.Release()
}
return ref, instance.GetWriteHandle(), err
Expand Down
24 changes: 14 additions & 10 deletions go/database/mpt/node_flusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package mpt
import (
"errors"
"slices"
"sync"
"time"

"github.com/Fantom-foundation/Carmen/go/database/mpt/shared"
Expand Down Expand Up @@ -72,20 +73,23 @@ func (f *nodeFlusher) Stop() error {
return errors.Join(f.errs...)
}

var dirtyIdListPool = sync.Pool{
New: func() interface{} {
list := make([]NodeId, 0, 1_000_000)
return &list
},
}

func tryFlushDirtyNodes(cache NodeCache, sink NodeSink) error {
// Collect a list of dirty nodes to be flushed.
dirtyIds := make([]NodeId, 0, 1_000_000)
dirtyIdsPtr := dirtyIdListPool.Get().(*[]NodeId)
defer dirtyIdListPool.Put(dirtyIdsPtr)
dirtyIds := *(dirtyIdsPtr)
dirtyIds = dirtyIds[:0]
cache.ForEach(func(id NodeId, node *shared.Shared[Node]) {
handle, success := node.TryGetViewHandle()
if !success {
return
}
dirty := handle.Get().IsDirty()
handle.Release()
if !dirty {
return
if node.GetUnprotected().IsDirty() {
dirtyIds = append(dirtyIds, id)
}
dirtyIds = append(dirtyIds, id)
})

// The IDs are sorted to increase the chance of sequential
Expand Down
47 changes: 34 additions & 13 deletions go/database/mpt/node_flusher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestNodeFlusher_ErrorsAreCollected(t *testing.T) {
sink := NewMockNodeSink(ctrl)

id := ValueId(1)
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: false}})
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusDirty}})

done := make(chan struct{})
counter := 0
Expand Down Expand Up @@ -115,10 +115,10 @@ func TestNodeFlusher_FlushesOnlyDirtyNodes(t *testing.T) {
sink := NewMockNodeSink(ctrl)

nodes := map[NodeId]*shared.Shared[Node]{
ValueId(1): shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: true}}),
ValueId(2): shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: false}}),
ValueId(3): shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: false}}),
ValueId(4): shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: true}}),
ValueId(1): shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusClean}}),
ValueId(2): shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusDirty}}),
ValueId(3): shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusDirty}}),
ValueId(4): shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusClean}}),
}

// All nodes are checked.
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestNodeFlusher_FlushedNodesAreMarkedClean(t *testing.T) {
sink := NewMockNodeSink(ctrl)

id := ValueId(1)
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: false}})
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusDirty}})
cache.EXPECT().ForEach(gomock.Any()).Do(func(f func(NodeId, *shared.Shared[Node])) {
f(id, node)
})
Expand All @@ -177,11 +177,13 @@ func TestNodeFlusher_NodesInUseAreIgnored(t *testing.T) {
sink := NewMockNodeSink(ctrl)

id := ValueId(1)
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: false}})
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusDirty}})
cache.EXPECT().ForEach(gomock.Any()).Do(func(f func(NodeId, *shared.Shared[Node])) {
f(id, node)
})

cache.EXPECT().Get(RefTo(id)).Return(node, true)

// There shall be no write events (which is the default, but spelled out explicitly here).
sink.EXPECT().Write(gomock.Any(), gomock.Any()).Times(0)

Expand All @@ -206,7 +208,7 @@ func TestNodeFlusher_NodesThatAreAccessedAfterBeingIdentifiedAsDirtyAreIgnored(t
sink := NewMockNodeSink(ctrl)

id := ValueId(1)
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: false}})
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusDirty}})
handle := shared.WriteHandle[Node]{}
cache.EXPECT().ForEach(gomock.Any()).Do(func(f func(NodeId, *shared.Shared[Node])) {
f(id, node)
Expand All @@ -232,7 +234,7 @@ func TestNodeFlusher_EvictedNodesAreIgnored(t *testing.T) {
sink := NewMockNodeSink(ctrl)

id := ValueId(1)
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: false}})
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusDirty}})
cache.EXPECT().ForEach(gomock.Any()).Do(func(f func(NodeId, *shared.Shared[Node])) {
f(id, node)
})
Expand All @@ -256,7 +258,7 @@ func TestNodeFlusher_NodesThatGetMarkedCleanByThirdPartyAreIgnored(t *testing.T)
sink := NewMockNodeSink(ctrl)

id := ValueId(1)
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: false}})
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusDirty}})
cache.EXPECT().ForEach(gomock.Any()).Do(func(f func(NodeId, *shared.Shared[Node])) {
f(id, node)
handle := node.GetWriteHandle()
Expand All @@ -282,7 +284,7 @@ func TestNodeFlusher_NodesWithDirtyHashesAreIgnored(t *testing.T) {
sink := NewMockNodeSink(ctrl)

id := ValueId(1)
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: false, hashStatus: hashStatusDirty}})
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusDirty, hashStatus: hashStatusDirty}})
cache.EXPECT().ForEach(gomock.Any()).Do(func(f func(NodeId, *shared.Shared[Node])) {
f(id, node)
})
Expand All @@ -305,7 +307,7 @@ func TestNodeFlusher_FlushErrorsArePropagated(t *testing.T) {
sink := NewMockNodeSink(ctrl)

id := ValueId(1)
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: false}})
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusDirty}})
cache.EXPECT().ForEach(gomock.Any()).Do(func(f func(NodeId, *shared.Shared[Node])) {
f(id, node)
})
Expand Down Expand Up @@ -333,7 +335,7 @@ func TestNodeFlusher_FlushErrorsAreAggregated(t *testing.T) {

id1 := ValueId(1)
id2 := ValueId(2)
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: false}})
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusDirty}})
cache.EXPECT().ForEach(gomock.Any()).Do(func(f func(NodeId, *shared.Shared[Node])) {
f(id1, node)
f(id2, node)
Expand Down Expand Up @@ -362,3 +364,22 @@ func checkThatNodeIsNotLocked(t *testing.T, node *shared.Shared[Node]) {
}
handle.Release()
}

func BenchmarkNodeFlusher_CollectDirtyNodes(b *testing.B) {
sizes := []int{1_000, 10_000, 100_000, 1_000_000, 10_000_000}
for _, size := range sizes {
b.Run(fmt.Sprintf("size=%d", size), func(b *testing.B) {
// Fill a cache with clean nodes.
cache := NewNodeCache(size)
for i := 0; i < size; i++ {
node := shared.MakeShared[Node](&ValueNode{nodeBase: nodeBase{clean: cleanStatusClean}})
ref := NewNodeReference(ValueId(uint64(i)))
cache.GetOrSet(&ref, node)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
tryFlushDirtyNodes(cache, nil)
}
})
}
}
Loading