Skip to content

Commit 1162828

Browse files
authored
feat(lib/trie): clear fields when node is dirty (#2297)
1 parent dc62695 commit 1162828

File tree

4 files changed

+67
-15
lines changed

4 files changed

+67
-15
lines changed

internal/trie/node/dirty.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ func (b *Branch) IsDirty() bool {
1111
// SetDirty sets the dirty status to the branch.
1212
func (b *Branch) SetDirty(dirty bool) {
1313
b.Dirty = dirty
14+
if dirty {
15+
// A node is marked dirty if its key or value is modified.
16+
// This means its cached encoding and hash fields are no longer
17+
// valid. To improve memory usage, we clear these fields.
18+
b.Encoding = nil
19+
b.HashDigest = nil
20+
}
1421
}
1522

1623
// IsDirty returns the dirty status of the leaf.
@@ -21,4 +28,11 @@ func (l *Leaf) IsDirty() bool {
2128
// SetDirty sets the dirty status to the leaf.
2229
func (l *Leaf) SetDirty(dirty bool) {
2330
l.Dirty = dirty
31+
if dirty {
32+
// A node is marked dirty if its key or value is modified.
33+
// This means its cached encoding and hash fields are no longer
34+
// valid. To improve memory usage, we clear these fields.
35+
l.Encoding = nil
36+
l.HashDigest = nil
37+
}
2438
}

internal/trie/node/dirty_test.go

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,40 @@ func Test_Branch_SetDirty(t *testing.T) {
4848
expected *Branch
4949
}{
5050
"not dirty to not dirty": {
51-
branch: &Branch{},
52-
expected: &Branch{},
51+
branch: &Branch{
52+
Encoding: []byte{1},
53+
HashDigest: []byte{1},
54+
},
55+
expected: &Branch{
56+
Encoding: []byte{1},
57+
HashDigest: []byte{1},
58+
},
5359
},
5460
"not dirty to dirty": {
55-
branch: &Branch{},
61+
branch: &Branch{
62+
Encoding: []byte{1},
63+
HashDigest: []byte{1},
64+
},
5665
dirty: true,
5766
expected: &Branch{Dirty: true},
5867
},
5968
"dirty to not dirty": {
60-
branch: &Branch{Dirty: true},
61-
expected: &Branch{},
69+
branch: &Branch{
70+
Encoding: []byte{1},
71+
HashDigest: []byte{1},
72+
Dirty: true,
73+
},
74+
expected: &Branch{
75+
Encoding: []byte{1},
76+
HashDigest: []byte{1},
77+
},
6278
},
6379
"dirty to dirty": {
64-
branch: &Branch{Dirty: true},
80+
branch: &Branch{
81+
Encoding: []byte{1},
82+
HashDigest: []byte{1},
83+
Dirty: true,
84+
},
6585
dirty: true,
6686
expected: &Branch{Dirty: true},
6787
},
@@ -118,20 +138,40 @@ func Test_Leaf_SetDirty(t *testing.T) {
118138
expected *Leaf
119139
}{
120140
"not dirty to not dirty": {
121-
leaf: &Leaf{},
122-
expected: &Leaf{},
141+
leaf: &Leaf{
142+
Encoding: []byte{1},
143+
HashDigest: []byte{1},
144+
},
145+
expected: &Leaf{
146+
Encoding: []byte{1},
147+
HashDigest: []byte{1},
148+
},
123149
},
124150
"not dirty to dirty": {
125-
leaf: &Leaf{},
151+
leaf: &Leaf{
152+
Encoding: []byte{1},
153+
HashDigest: []byte{1},
154+
},
126155
dirty: true,
127156
expected: &Leaf{Dirty: true},
128157
},
129158
"dirty to not dirty": {
130-
leaf: &Leaf{Dirty: true},
131-
expected: &Leaf{},
159+
leaf: &Leaf{
160+
Encoding: []byte{1},
161+
HashDigest: []byte{1},
162+
Dirty: true,
163+
},
164+
expected: &Leaf{
165+
Encoding: []byte{1},
166+
HashDigest: []byte{1},
167+
},
132168
},
133169
"dirty to dirty": {
134-
leaf: &Leaf{Dirty: true},
170+
leaf: &Leaf{
171+
Encoding: []byte{1},
172+
HashDigest: []byte{1},
173+
Dirty: true,
174+
},
135175
dirty: true,
136176
expected: &Leaf{Dirty: true},
137177
},

internal/trie/node/leaf.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,4 @@ func bytesToString(b []byte) (s string) {
6868
default:
6969
return fmt.Sprintf("0x%x...%x", b[:8], b[len(b)-8:])
7070
}
71-
7271
}

lib/trie/trie.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ func (t *Trie) insertInLeaf(parentLeaf *node.Leaf, key,
366366
// Move the current leaf parent as a child to the new branch.
367367
childIndex := parentLeafKey[commonPrefixLength]
368368
parentLeaf.Key = parentLeaf.Key[commonPrefixLength+1:]
369-
parentLeaf.Dirty = true
369+
parentLeaf.SetDirty(true)
370370
newBranchParent.Children[childIndex] = parentLeaf
371371
}
372372

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

443-
parentBranch.Dirty = true
444443
parentBranch.Key = remainingOldParentKey
445444
parentBranch.Generation = t.generation
446445
newParentBranch.Children[oldParentIndex] = parentBranch

0 commit comments

Comments
 (0)