triedb/pathdb: return nodeLoc by value to avoid heap allocation (#33819)

This commit is contained in:
sashass1315 2026-02-11 16:14:43 +02:00 committed by GitHub
parent 3011d83e6f
commit 919b238c82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 9 additions and 9 deletions

View file

@ -45,7 +45,7 @@ type layer interface {
// Note: // Note:
// - the returned node is not a copy, please don't modify it. // - the returned node is not a copy, please don't modify it.
// - no error will be returned if the requested node is not found in database. // - no error will be returned if the requested node is not found in database.
node(owner common.Hash, path []byte, depth int) ([]byte, common.Hash, *nodeLoc, error) node(owner common.Hash, path []byte, depth int) ([]byte, common.Hash, nodeLoc, error)
// account directly retrieves the account RLP associated with a particular // account directly retrieves the account RLP associated with a particular
// hash in the slim data format. An error will be returned if the read // hash in the slim data format. An error will be returned if the read

View file

@ -79,7 +79,7 @@ func (dl *diffLayer) parentLayer() layer {
// node implements the layer interface, retrieving the trie node blob with the // node implements the layer interface, retrieving the trie node blob with the
// provided node information. No error will be returned if the node is not found. // provided node information. No error will be returned if the node is not found.
func (dl *diffLayer) node(owner common.Hash, path []byte, depth int) ([]byte, common.Hash, *nodeLoc, error) { func (dl *diffLayer) node(owner common.Hash, path []byte, depth int) ([]byte, common.Hash, nodeLoc, error) {
// Hold the lock, ensure the parent won't be changed during the // Hold the lock, ensure the parent won't be changed during the
// state accessing. // state accessing.
dl.lock.RLock() dl.lock.RLock()
@ -91,7 +91,7 @@ func (dl *diffLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co
dirtyNodeHitMeter.Mark(1) dirtyNodeHitMeter.Mark(1)
dirtyNodeHitDepthHist.Update(int64(depth)) dirtyNodeHitDepthHist.Update(int64(depth))
dirtyNodeReadMeter.Mark(int64(len(n.Blob))) dirtyNodeReadMeter.Mark(int64(len(n.Blob)))
return n.Blob, n.Hash, &nodeLoc{loc: locDiffLayer, depth: depth}, nil return n.Blob, n.Hash, nodeLoc{loc: locDiffLayer, depth: depth}, nil
} }
// Trie node unknown to this layer, resolve from parent // Trie node unknown to this layer, resolve from parent
return dl.parent.node(owner, path, depth+1) return dl.parent.node(owner, path, depth+1)

View file

@ -112,12 +112,12 @@ func (dl *diskLayer) markStale() {
// node implements the layer interface, retrieving the trie node with the // node implements the layer interface, retrieving the trie node with the
// provided node info. No error will be returned if the node is not found. // provided node info. No error will be returned if the node is not found.
func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, common.Hash, *nodeLoc, error) { func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, common.Hash, nodeLoc, error) {
dl.lock.RLock() dl.lock.RLock()
defer dl.lock.RUnlock() defer dl.lock.RUnlock()
if dl.stale { if dl.stale {
return nil, common.Hash{}, nil, errSnapshotStale return nil, common.Hash{}, nodeLoc{}, errSnapshotStale
} }
// Try to retrieve the trie node from the not-yet-written node buffer first // Try to retrieve the trie node from the not-yet-written node buffer first
// (both the live one and the frozen one). Note the buffer is lock free since // (both the live one and the frozen one). Note the buffer is lock free since
@ -129,7 +129,7 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co
dirtyNodeHitMeter.Mark(1) dirtyNodeHitMeter.Mark(1)
dirtyNodeReadMeter.Mark(int64(len(n.Blob))) dirtyNodeReadMeter.Mark(int64(len(n.Blob)))
dirtyNodeHitDepthHist.Update(int64(depth)) dirtyNodeHitDepthHist.Update(int64(depth))
return n.Blob, n.Hash, &nodeLoc{loc: locDirtyCache, depth: depth}, nil return n.Blob, n.Hash, nodeLoc{loc: locDirtyCache, depth: depth}, nil
} }
} }
} }
@ -141,7 +141,7 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co
if blob := dl.nodes.Get(nil, key); len(blob) > 0 { if blob := dl.nodes.Get(nil, key); len(blob) > 0 {
cleanNodeHitMeter.Mark(1) cleanNodeHitMeter.Mark(1)
cleanNodeReadMeter.Mark(int64(len(blob))) cleanNodeReadMeter.Mark(int64(len(blob)))
return blob, crypto.Keccak256Hash(blob), &nodeLoc{loc: locCleanCache, depth: depth}, nil return blob, crypto.Keccak256Hash(blob), nodeLoc{loc: locCleanCache, depth: depth}, nil
} }
cleanNodeMissMeter.Mark(1) cleanNodeMissMeter.Mark(1)
} }
@ -161,7 +161,7 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co
dl.nodes.Set(key, blob) dl.nodes.Set(key, blob)
cleanNodeWriteMeter.Mark(int64(len(blob))) cleanNodeWriteMeter.Mark(int64(len(blob)))
} }
return blob, crypto.Keccak256Hash(blob), &nodeLoc{loc: locDiskLayer, depth: depth}, nil return blob, crypto.Keccak256Hash(blob), nodeLoc{loc: locDiskLayer, depth: depth}, nil
} }
// account directly retrieves the account RLP associated with a particular // account directly retrieves the account RLP associated with a particular

View file

@ -47,7 +47,7 @@ type nodeLoc struct {
} }
// string returns the string representation of node location. // string returns the string representation of node location.
func (loc *nodeLoc) string() string { func (loc nodeLoc) string() string {
return fmt.Sprintf("loc: %s, depth: %d", loc.loc, loc.depth) return fmt.Sprintf("loc: %s, depth: %d", loc.loc, loc.depth)
} }