From 3bd5c37c3b958211ae1f69298d9a6ebe35bb102d Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 23 May 2025 11:36:48 +0800 Subject: [PATCH] core/state: update stats before lock Signed-off-by: jsvisa --- core/state/snapshot/snapshot.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/core/state/snapshot/snapshot.go b/core/state/snapshot/snapshot.go index ec6dba3fa6..fe5381ef69 100644 --- a/core/state/snapshot/snapshot.go +++ b/core/state/snapshot/snapshot.go @@ -388,17 +388,8 @@ func (t *Tree) Cap(root common.Hash, layers int) error { if !ok { return fmt.Errorf("snapshot [%#x] is disk layer", root) } - // If the generator is still running, use a more aggressive cap - diff.origin.lock.RLock() - if diff.origin.genMarker != nil && layers > 8 { - layers = 8 - } - diff.origin.lock.RUnlock() - // Run the internal capping and discard all stale layers - t.lock.Lock() - - defer t.lock.Unlock() + // Update the cache stats var stats fastcache.Stats diff.origin.cache.UpdateStats(&stats) snapshotCacheGetGauge.Update(int64(stats.GetCalls)) @@ -409,6 +400,17 @@ func (t *Tree) Cap(root common.Hash, layers int) error { snapshotCacheEntriesGauge.Update(int64(stats.EntriesCount)) snapshotCacheCollisionGauge.Update(int64(stats.Collisions)) + // If the generator is still running, use a more aggressive cap + diff.origin.lock.RLock() + if diff.origin.genMarker != nil && layers > 8 { + layers = 8 + } + diff.origin.lock.RUnlock() + + // Run the internal capping and discard all stale layers + t.lock.Lock() + defer t.lock.Unlock() + // Flattening the bottom-most diff layer requires special casing since there's // no child to rewire to the grandparent. In that case we can fake a temporary // child for the capping and then remove it.