trie: preallocate slice capacity (#33689)
Some checks are pending
/ Linux Build (push) Waiting to run
/ Linux Build (arm) (push) Waiting to run
/ Keeper Build (push) Waiting to run
/ Windows Build (push) Waiting to run
/ Docker Image (push) Waiting to run

This PR optimizes memory allocation in StateTrie.PrefetchAccount() and
StateTrie.PrefetchStorage() by preallocating slice capacity when the
final size is known.
This commit is contained in:
marukai67 2026-01-27 05:04:12 +01:00 committed by GitHub
parent c2595381bf
commit e250836973
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -134,9 +134,9 @@ func (t *StateTrie) GetAccountByHash(addrHash common.Hash) (*types.StateAccount,
// PrefetchAccount attempts to resolve specific accounts from the database // PrefetchAccount attempts to resolve specific accounts from the database
// to accelerate subsequent trie operations. // to accelerate subsequent trie operations.
func (t *StateTrie) PrefetchAccount(addresses []common.Address) error { func (t *StateTrie) PrefetchAccount(addresses []common.Address) error {
var keys [][]byte keys := make([][]byte, len(addresses))
for _, addr := range addresses { for i, addr := range addresses {
keys = append(keys, crypto.Keccak256(addr.Bytes())) keys[i] = crypto.Keccak256(addr.Bytes())
} }
return t.trie.Prefetch(keys) return t.trie.Prefetch(keys)
} }
@ -157,9 +157,9 @@ func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
// PrefetchStorage attempts to resolve specific storage slots from the database // PrefetchStorage attempts to resolve specific storage slots from the database
// to accelerate subsequent trie operations. // to accelerate subsequent trie operations.
func (t *StateTrie) PrefetchStorage(_ common.Address, keys [][]byte) error { func (t *StateTrie) PrefetchStorage(_ common.Address, keys [][]byte) error {
var keylist [][]byte keylist := make([][]byte, len(keys))
for _, key := range keys { for i, key := range keys {
keylist = append(keylist, crypto.Keccak256(key)) keylist[i] = crypto.Keccak256(key)
} }
return t.trie.Prefetch(keylist) return t.trie.Prefetch(keylist)
} }