trie, triedb/pathdb: prealloc capacity for map and slice (#29986)

pull/30013/head
maskpp 3 months ago committed by GitHub
parent b78d2352ef
commit 115d154392
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      trie/secure_trie.go
  2. 2
      trie/trienode/node.go
  3. 14
      triedb/pathdb/history.go

@ -224,7 +224,7 @@ func (t *StateTrie) GetKey(shaKey []byte) []byte {
func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) { func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
// Write all the pre-images to the actual disk database // Write all the pre-images to the actual disk database
if len(t.getSecKeyCache()) > 0 { if len(t.getSecKeyCache()) > 0 {
preimages := make(map[common.Hash][]byte) preimages := make(map[common.Hash][]byte, len(t.secKeyCache))
for hk, key := range t.secKeyCache { for hk, key := range t.secKeyCache {
preimages[common.BytesToHash([]byte(hk))] = key preimages[common.BytesToHash([]byte(hk))] = key
} }

@ -186,7 +186,7 @@ func (set *MergedNodeSet) Merge(other *NodeSet) error {
// Flatten returns a two-dimensional map for internal nodes. // Flatten returns a two-dimensional map for internal nodes.
func (set *MergedNodeSet) Flatten() map[common.Hash]map[string]*Node { func (set *MergedNodeSet) Flatten() map[common.Hash]map[string]*Node {
nodes := make(map[common.Hash]map[string]*Node) nodes := make(map[common.Hash]map[string]*Node, len(set.Sets))
for owner, set := range set.Sets { for owner, set := range set.Sets {
nodes[owner] = set.Nodes nodes[owner] = set.Nodes
} }

@ -384,10 +384,11 @@ func (r *decoder) readAccount(pos int) (accountIndex, []byte, error) {
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 last common.Hash
list []common.Hash count = int(accIndex.storageSlots)
storage = make(map[common.Hash][]byte) list = make([]common.Hash, 0, count)
storage = make(map[common.Hash][]byte, count)
) )
for j := 0; j < int(accIndex.storageSlots); j++ { for j := 0; j < count; j++ {
var ( var (
index slotIndex index slotIndex
start = (accIndex.storageOffset + uint32(j)) * uint32(slotIndexSize) start = (accIndex.storageOffset + uint32(j)) * 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 {

Loading…
Cancel
Save