Skip to content

Commit 9b09c0f

Browse files
holimankaralabe
authored andcommitted
* trie: utilize callbacks instead of amassing lists in ref/unref (#20529)
* trie/tests: add benchmarks and update trie tests * trie: update benchmark tests * trie: utilize callbacks instead of amassing lists of hashes in database ref/unref * trie: replace remaining non-callback based accesses
1 parent 770316d commit 9b09c0f

File tree

2 files changed

+337
-31
lines changed

2 files changed

+337
-31
lines changed

trie/database.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -180,35 +180,31 @@ func (n *cachedNode) obj(hash common.Hash) node {
180180
return expandNode(hash[:], n.node)
181181
}
182182

183-
// childs returns all the tracked children of this node, both the implicit ones
184-
// from inside the node as well as the explicit ones from outside the node.
185-
func (n *cachedNode) childs() []common.Hash {
186-
children := make([]common.Hash, 0, 16)
183+
// forChilds invokes the callback for all the tracked children of this node,
184+
// both the implicit ones from inside the node as well as the explicit ones
185+
//from outside the node.
186+
func (n *cachedNode) forChilds(onChild func(hash common.Hash)) {
187187
for child := range n.children {
188-
children = append(children, child)
188+
onChild(child)
189189
}
190190
if _, ok := n.node.(rawNode); !ok {
191-
gatherChildren(n.node, &children)
191+
forGatherChildren(n.node, onChild)
192192
}
193-
return children
194193
}
195194

196-
// gatherChildren traverses the node hierarchy of a collapsed storage node and
197-
// retrieves all the hashnode children.
198-
func gatherChildren(n node, children *[]common.Hash) {
195+
// forGatherChildren traverses the node hierarchy of a collapsed storage node and
196+
// invokes the callback for all the hashnode children.
197+
func forGatherChildren(n node, onChild func(hash common.Hash)) {
199198
switch n := n.(type) {
200199
case *rawShortNode:
201-
gatherChildren(n.Val, children)
202-
200+
forGatherChildren(n.Val, onChild)
203201
case rawFullNode:
204202
for i := 0; i < 16; i++ {
205-
gatherChildren(n[i], children)
203+
forGatherChildren(n[i], onChild)
206204
}
207205
case hashNode:
208-
*children = append(*children, common.BytesToHash(n))
209-
206+
onChild(common.BytesToHash(n))
210207
case valueNode, nil:
211-
212208
default:
213209
panic(fmt.Sprintf("unknown node type: %T", n))
214210
}
@@ -334,11 +330,11 @@ func (db *Database) insert(hash common.Hash, blob []byte, node node) {
334330
size: uint16(len(blob)),
335331
flushPrev: db.newest,
336332
}
337-
for _, child := range entry.childs() {
333+
entry.forChilds(func(child common.Hash) {
338334
if c := db.dirties[child]; c != nil {
339335
c.parents++
340336
}
341-
}
337+
})
342338
db.dirties[hash] = entry
343339

344340
// Update the flush-list endpoints
@@ -570,9 +566,9 @@ func (db *Database) dereference(child common.Hash, parent common.Hash) {
570566
db.dirties[node.flushNext].flushPrev = node.flushPrev
571567
}
572568
// Dereference all children and delete the node
573-
for _, hash := range node.childs() {
569+
node.forChilds(func(hash common.Hash) {
574570
db.dereference(hash, child)
575-
}
571+
})
576572
delete(db.dirties, child)
577573
db.dirtiesSize -= common.StorageSize(common.HashLength + int(node.size))
578574
if node.children != nil {
@@ -766,10 +762,14 @@ func (db *Database) commit(hash common.Hash, batch ethdb.Batch, uncacher *cleane
766762
if !ok {
767763
return nil
768764
}
769-
for _, child := range node.childs() {
770-
if err := db.commit(child, batch, uncacher); err != nil {
771-
return err
765+
var err error
766+
node.forChilds(func(child common.Hash) {
767+
if err == nil {
768+
err = db.commit(child, batch, uncacher)
772769
}
770+
})
771+
if err != nil {
772+
return err
773773
}
774774
if err := batch.Put(hash[:], node.rlp()); err != nil {
775775
return err

0 commit comments

Comments
 (0)