prealloc capacity for map and slice

This commit is contained in:
maskpp 2024-06-13 02:04:09 +08:00
parent b6f2bbd417
commit 6c4b8eb9fd

View file

@ -383,12 +383,13 @@ func (r *decoder) readAccount(pos int) (accountIndex, []byte, error) {
// readStorage parses the storage slots from the byte stream with specified account. // readStorage parses the storage slots from the byte stream with specified account.
func (r *decoder) readStorage(accIndex accountIndex) ([]common.Hash, map[common.Hash][]byte, error) { func (r *decoder) readStorage(accIndex accountIndex) ([]common.Hash, map[common.Hash][]byte, error) {
var ( var (
last common.Hash count = int(accIndex.storageSlots)
list []common.Hash list = make([]common.Hash, 0, count)
storage = make(map[common.Hash][]byte) storage = make(map[common.Hash][]byte, count)
) )
for j := 0; j < int(accIndex.storageSlots); j++ { for j := 0; j < count; j++ {
var ( var (
last common.Hash
index slotIndex index slotIndex
start = (accIndex.storageOffset + uint32(j)) * uint32(slotIndexSize) start = (accIndex.storageOffset + uint32(j)) * uint32(slotIndexSize)
end = (accIndex.storageOffset + uint32(j+1)) * uint32(slotIndexSize) end = (accIndex.storageOffset + uint32(j+1)) * uint32(slotIndexSize)
@ -430,9 +431,10 @@ func (r *decoder) readStorage(accIndex accountIndex) ([]common.Hash, map[common.
// decode deserializes the account and storage data from the provided byte stream. // decode deserializes the account and storage data from the provided byte stream.
func (h *history) decode(accountData, storageData, accountIndexes, storageIndexes []byte) error { func (h *history) decode(accountData, storageData, accountIndexes, storageIndexes []byte) error {
var ( var (
accounts = make(map[common.Address][]byte) count = len(accountIndexes) / accountIndexSize
accounts = make(map[common.Address][]byte, count)
storages = make(map[common.Address]map[common.Hash][]byte) storages = make(map[common.Address]map[common.Hash][]byte)
accountList []common.Address accountList = make([]common.Address, 0, count)
storageList = make(map[common.Address][]common.Hash) storageList = make(map[common.Address][]common.Hash)
r = &decoder{ r = &decoder{
@ -445,7 +447,7 @@ func (h *history) decode(accountData, storageData, accountIndexes, storageIndexe
if err := r.verify(); err != nil { if err := r.verify(); err != nil {
return err return err
} }
for i := 0; i < len(accountIndexes)/accountIndexSize; i++ { for i := 0; i < count; i++ {
// Resolve account first // Resolve account first
accIndex, accData, err := r.readAccount(i) accIndex, accData, err := r.readAccount(i)
if err != nil { if err != nil {