triedb: opt using sync.Pool

This commit is contained in:
cuiweixie 2025-08-08 21:19:46 +08:00
parent c3ef6c77c2
commit be3291c015

View file

@ -161,6 +161,12 @@ func (l *lookup) storageTip(accountHash common.Hash, slotHash common.Hash, state
return common.Hash{} return common.Hash{}
} }
var listPool = sync.Pool{
New: func() any {
return make([]common.Hash, 0, 16)
},
}
// addLayer traverses the state data retained in the specified diff layer and // addLayer traverses the state data retained in the specified diff layer and
// integrates it into the lookup set. // integrates it into the lookup set.
// //
@ -182,7 +188,7 @@ func (l *lookup) addLayer(diff *diffLayer) {
for accountHash := range diff.states.accountData { for accountHash := range diff.states.accountData {
list, exists := l.accounts[accountHash] list, exists := l.accounts[accountHash]
if !exists { if !exists {
list = make([]common.Hash, 0, 16) // TODO(rjl493456442) use sync pool list = listPool.Get().([]common.Hash)
} }
list = append(list, state) list = append(list, state)
l.accounts[accountHash] = list l.accounts[accountHash] = list
@ -197,7 +203,7 @@ func (l *lookup) addLayer(diff *diffLayer) {
key := storageKey(accountHash, slotHash) key := storageKey(accountHash, slotHash)
list, exists := l.storages[key] list, exists := l.storages[key]
if !exists { if !exists {
list = make([]common.Hash, 0, 16) // TODO(rjl493456442) use sync pool list = listPool.Get().([]common.Hash)
} }
list = append(list, state) list = append(list, state)
l.storages[key] = list l.storages[key] = list
@ -251,6 +257,7 @@ func (l *lookup) removeLayer(diff *diffLayer) error {
if len(list) != 0 { if len(list) != 0 {
l.accounts[accountHash] = list l.accounts[accountHash] = list
} else { } else {
listPool.Put(list[:0])
delete(l.accounts, accountHash) delete(l.accounts, accountHash)
} }
} }
@ -268,6 +275,7 @@ func (l *lookup) removeLayer(diff *diffLayer) error {
if len(list) != 0 { if len(list) != 0 {
l.storages[key] = list l.storages[key] = list
} else { } else {
listPool.Put(list[:0])
delete(l.storages, key) delete(l.storages, key)
} }
} }