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

feat(lib/trie): clear fields when node is dirty #2297

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions internal/trie/node/dirty.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ func (b *Branch) IsDirty() bool {
// SetDirty sets the dirty status to the branch.
func (b *Branch) SetDirty(dirty bool) {
b.Dirty = dirty
if dirty {
// A node is marked dirty if its key or value is modified.
// This means its cached encoding and hash fields are no longer
// valid. To improve memory usage, we clear these fields.
b.Encoding = nil
b.HashDigest = nil
}
}

// IsDirty returns the dirty status of the leaf.
Expand All @@ -21,4 +28,11 @@ func (l *Leaf) IsDirty() bool {
// SetDirty sets the dirty status to the leaf.
func (l *Leaf) SetDirty(dirty bool) {
l.Dirty = dirty
if dirty {
// A node is marked dirty if its key or value is modified.
// This means its cached encoding and hash fields are no longer
// valid. To improve memory usage, we clear these fields.
l.Encoding = nil
l.HashDigest = nil
}
}
64 changes: 52 additions & 12 deletions internal/trie/node/dirty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,40 @@ func Test_Branch_SetDirty(t *testing.T) {
expected *Branch
}{
"not dirty to not dirty": {
branch: &Branch{},
expected: &Branch{},
branch: &Branch{
Encoding: []byte{1},
HashDigest: []byte{1},
},
expected: &Branch{
Encoding: []byte{1},
HashDigest: []byte{1},
},
},
"not dirty to dirty": {
branch: &Branch{},
branch: &Branch{
Encoding: []byte{1},
HashDigest: []byte{1},
},
dirty: true,
expected: &Branch{Dirty: true},
},
"dirty to not dirty": {
branch: &Branch{Dirty: true},
expected: &Branch{},
branch: &Branch{
Encoding: []byte{1},
HashDigest: []byte{1},
Dirty: true,
},
expected: &Branch{
Encoding: []byte{1},
HashDigest: []byte{1},
},
},
"dirty to dirty": {
branch: &Branch{Dirty: true},
branch: &Branch{
Encoding: []byte{1},
HashDigest: []byte{1},
Dirty: true,
},
dirty: true,
expected: &Branch{Dirty: true},
},
Expand Down Expand Up @@ -118,20 +138,40 @@ func Test_Leaf_SetDirty(t *testing.T) {
expected *Leaf
}{
"not dirty to not dirty": {
leaf: &Leaf{},
expected: &Leaf{},
leaf: &Leaf{
Encoding: []byte{1},
HashDigest: []byte{1},
},
expected: &Leaf{
Encoding: []byte{1},
HashDigest: []byte{1},
},
},
"not dirty to dirty": {
leaf: &Leaf{},
leaf: &Leaf{
Encoding: []byte{1},
HashDigest: []byte{1},
},
dirty: true,
expected: &Leaf{Dirty: true},
},
"dirty to not dirty": {
leaf: &Leaf{Dirty: true},
expected: &Leaf{},
leaf: &Leaf{
Encoding: []byte{1},
HashDigest: []byte{1},
Dirty: true,
},
expected: &Leaf{
Encoding: []byte{1},
HashDigest: []byte{1},
},
},
"dirty to dirty": {
leaf: &Leaf{Dirty: true},
leaf: &Leaf{
Encoding: []byte{1},
HashDigest: []byte{1},
Dirty: true,
},
dirty: true,
expected: &Leaf{Dirty: true},
},
Expand Down
1 change: 0 additions & 1 deletion internal/trie/node/leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,4 @@ func bytesToString(b []byte) (s string) {
default:
return fmt.Sprintf("0x%x...%x", b[:8], b[len(b)-8:])
}

}
3 changes: 1 addition & 2 deletions lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key,
// Move the current leaf parent as a child to the new branch.
childIndex := parentLeafKey[commonPrefixLength]
parentLeaf.Key = parentLeaf.Key[commonPrefixLength+1:]
parentLeaf.Dirty = true
parentLeaf.SetDirty(true)
newBranchParent.Children[childIndex] = parentLeaf
}

Expand Down Expand Up @@ -440,7 +440,6 @@ func (t *Trie) insertInBranch(parentBranch *node.Branch, key, value []byte) (new
oldParentIndex := parentBranch.Key[commonPrefixLength]
remainingOldParentKey := parentBranch.Key[commonPrefixLength+1:]

parentBranch.Dirty = true
parentBranch.Key = remainingOldParentKey
parentBranch.Generation = t.generation
newParentBranch.Children[oldParentIndex] = parentBranch
Expand Down