Skip to content

Commit

Permalink
trie, triedb/pathdb: prealloc capacity for map and slice (#29986)
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp authored Jun 17, 2024
1 parent b78d235 commit 115d154
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion trie/secure_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (t *StateTrie) GetKey(shaKey []byte) []byte {
func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
// Write all the pre-images to the actual disk database
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 {
preimages[common.BytesToHash([]byte(hk))] = key
}
Expand Down
2 changes: 1 addition & 1 deletion trie/trienode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (set *MergedNodeSet) Merge(other *NodeSet) error {

// Flatten returns a two-dimensional map for internal nodes.
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 {
nodes[owner] = set.Nodes
}
Expand Down
14 changes: 8 additions & 6 deletions triedb/pathdb/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
var (
last common.Hash
list []common.Hash
storage = make(map[common.Hash][]byte)
count = int(accIndex.storageSlots)
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 (
index slotIndex
start = (accIndex.storageOffset + uint32(j)) * uint32(slotIndexSize)
Expand Down Expand Up @@ -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.
func (h *history) decode(accountData, storageData, accountIndexes, storageIndexes []byte) error {
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)
accountList []common.Address
accountList = make([]common.Address, 0, count)
storageList = make(map[common.Address][]common.Hash)

r = &decoder{
Expand All @@ -445,7 +447,7 @@ func (h *history) decode(accountData, storageData, accountIndexes, storageIndexe
if err := r.verify(); err != nil {
return err
}
for i := 0; i < len(accountIndexes)/accountIndexSize; i++ {
for i := 0; i < count; i++ {
// Resolve account first
accIndex, accData, err := r.readAccount(i)
if err != nil {
Expand Down

0 comments on commit 115d154

Please sign in to comment.