triedb/pathdb: add nil guards for metrics in diskLayer.node

Add nil checks before metric recording calls in diskLayer.node(). When
metrics collection is disabled, this eliminates per-lookup method call
overhead on the hot node retrieval path.
This commit is contained in:
CPerezz 2026-03-16 23:59:04 +01:00
parent 98b13f342f
commit 10c60af37a
No known key found for this signature in database
GPG key ID: 62045F34B97177DD

View file

@ -126,24 +126,38 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co
if buffer != nil { if buffer != nil {
n, found := buffer.node(owner, path) n, found := buffer.node(owner, path)
if found { if found {
dirtyNodeHitMeter.Mark(1) if dirtyNodeHitMeter != nil {
dirtyNodeReadMeter.Mark(int64(len(n.Blob))) dirtyNodeHitMeter.Mark(1)
dirtyNodeHitDepthHist.Update(int64(depth)) }
if dirtyNodeReadMeter != nil {
dirtyNodeReadMeter.Mark(int64(len(n.Blob)))
}
if dirtyNodeHitDepthHist != nil {
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
} }
} }
} }
dirtyNodeMissMeter.Mark(1) if dirtyNodeMissMeter != nil {
dirtyNodeMissMeter.Mark(1)
}
// Try to retrieve the trie node from the clean memory cache // Try to retrieve the trie node from the clean memory cache
key := nodeCacheKey(owner, path) key := nodeCacheKey(owner, path)
if dl.nodes != nil { if dl.nodes != nil {
if blob := dl.nodes.Get(nil, key); len(blob) > 0 { if blob := dl.nodes.Get(nil, key); len(blob) > 0 {
cleanNodeHitMeter.Mark(1) if cleanNodeHitMeter != nil {
cleanNodeReadMeter.Mark(int64(len(blob))) cleanNodeHitMeter.Mark(1)
}
if cleanNodeReadMeter != nil {
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) if cleanNodeMissMeter != nil {
cleanNodeMissMeter.Mark(1)
}
} }
// Try to retrieve the trie node from the disk. // Try to retrieve the trie node from the disk.
var blob []byte var blob []byte
@ -159,7 +173,9 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co
// database. // database.
if dl.nodes != nil && len(blob) > 0 { if dl.nodes != nil && len(blob) > 0 {
dl.nodes.Set(key, blob) dl.nodes.Set(key, blob)
cleanNodeWriteMeter.Mark(int64(len(blob))) if cleanNodeWriteMeter != nil {
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
} }