From 925413c487e32135762c21a542da6d5d53a9e022 Mon Sep 17 00:00:00 2001 From: BZO95 Date: Tue, 20 Jan 2026 21:29:39 +0800 Subject: [PATCH] core/state/snapshot: optimize lazy list generation via double-checked locking --- core/state/snapshot/difflayer.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/state/snapshot/difflayer.go b/core/state/snapshot/difflayer.go index 1286ded7e1..9873422b8a 100644 --- a/core/state/snapshot/difflayer.go +++ b/core/state/snapshot/difflayer.go @@ -431,6 +431,10 @@ func (dl *diffLayer) AccountList() []common.Hash { dl.lock.Lock() defer dl.lock.Unlock() + // Double check after acquiring the write lock + if dl.accountList != nil { + return dl.accountList + } dl.accountList = slices.SortedFunc(maps.Keys(dl.accountData), common.Hash.Cmp) dl.memory += uint64(len(dl.accountList) * common.HashLength) return dl.accountList @@ -463,6 +467,10 @@ func (dl *diffLayer) StorageList(accountHash common.Hash) []common.Hash { dl.lock.Lock() defer dl.lock.Unlock() + // Double check after acquiring the write lock + if list, exist := dl.storageList[accountHash]; exist { + return list + } storageList := slices.SortedFunc(maps.Keys(dl.storageData[accountHash]), common.Hash.Cmp) dl.storageList[accountHash] = storageList dl.memory += uint64(len(storageList)*common.HashLength + common.HashLength)