Skip to content

Commit

Permalink
core, trie: prealloc capacity for maps (ethereum#30437)
Browse files Browse the repository at this point in the history
- preallocate capacity for map
- avoid `reinject` adding empty value
- use `maps.Copy`
  • Loading branch information
mask-pp authored and zfy0701 committed Dec 3, 2024
1 parent d4fa96e commit db992a8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
4 changes: 1 addition & 3 deletions core/stateless/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ func (w *Witness) AddState(nodes map[string]struct{}) {
w.lock.Lock()
defer w.lock.Unlock()

for node := range nodes {
w.State[node] = struct{}{}
}
maps.Copy(w.State, nodes)
}

// Copy deep-copies the witness object. Witness.Block isn't deep-copied as it
Expand Down
6 changes: 4 additions & 2 deletions core/txpool/blobpool/blobpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ func (p *BlobPool) reorg(oldHead, newHead *types.Header) (map[common.Address][]*
}
// Generate the set of transactions per address to pull back into the pool,
// also updating the rest along the way
reinject := make(map[common.Address][]*types.Transaction)
reinject := make(map[common.Address][]*types.Transaction, len(transactors))
for addr := range transactors {
// Generate the set that was lost to reinject into the pool
lost := make([]*types.Transaction, 0, len(discarded[addr]))
Expand All @@ -949,7 +949,9 @@ func (p *BlobPool) reorg(oldHead, newHead *types.Header) (map[common.Address][]*
lost = append(lost, tx)
}
}
reinject[addr] = lost
if len(lost) > 0 {
reinject[addr] = lost
}

// Update the set that was already reincluded to track the blocks in limbo
for _, tx := range types.TxDifference(included[addr], discarded[addr]) {
Expand Down
2 changes: 1 addition & 1 deletion core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ func (s Transactions) EncodeIndex(i int, w *bytes.Buffer) {
func TxDifference(a, b Transactions) Transactions {
keep := make(Transactions, 0, len(a))

remove := make(map[common.Hash]struct{})
remove := make(map[common.Hash]struct{}, b.Len())
for _, tx := range b {
remove[tx.Hash()] = struct{}{}
}
Expand Down
2 changes: 1 addition & 1 deletion trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ func (t *Trie) Witness() map[string]struct{} {
if len(t.tracer.accessList) == 0 {
return nil
}
witness := make(map[string]struct{})
witness := make(map[string]struct{}, len(t.tracer.accessList))
for _, node := range t.tracer.accessList {
witness[string(node)] = struct{}{}
}
Expand Down

0 comments on commit db992a8

Please sign in to comment.