core/state/snapshot: optimize lazy list generation via double-checked locking

This commit is contained in:
BZO95 2026-01-20 21:29:39 +08:00
parent 2eb1ccc6c4
commit 925413c487

View file

@ -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)