Skip to content

Commit ec596e0

Browse files
authored
core, trie: prealloc capacity for maps (#30437)
- preallocate capacity for map - avoid `reinject` adding empty value - use `maps.Copy`
1 parent 0342496 commit ec596e0

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

core/stateless/witness.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ func (w *Witness) AddState(nodes map[string]struct{}) {
101101
w.lock.Lock()
102102
defer w.lock.Unlock()
103103

104-
for node := range nodes {
105-
w.State[node] = struct{}{}
106-
}
104+
maps.Copy(w.State, nodes)
107105
}
108106

109107
// Copy deep-copies the witness object. Witness.Block isn't deep-copied as it

core/txpool/blobpool/blobpool.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ func (p *BlobPool) reorg(oldHead, newHead *types.Header) (map[common.Address][]*
940940
}
941941
// Generate the set of transactions per address to pull back into the pool,
942942
// also updating the rest along the way
943-
reinject := make(map[common.Address][]*types.Transaction)
943+
reinject := make(map[common.Address][]*types.Transaction, len(transactors))
944944
for addr := range transactors {
945945
// Generate the set that was lost to reinject into the pool
946946
lost := make([]*types.Transaction, 0, len(discarded[addr]))
@@ -949,7 +949,9 @@ func (p *BlobPool) reorg(oldHead, newHead *types.Header) (map[common.Address][]*
949949
lost = append(lost, tx)
950950
}
951951
}
952-
reinject[addr] = lost
952+
if len(lost) > 0 {
953+
reinject[addr] = lost
954+
}
953955

954956
// Update the set that was already reincluded to track the blocks in limbo
955957
for _, tx := range types.TxDifference(included[addr], discarded[addr]) {

core/types/transaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ func (s Transactions) EncodeIndex(i int, w *bytes.Buffer) {
560560
func TxDifference(a, b Transactions) Transactions {
561561
keep := make(Transactions, 0, len(a))
562562

563-
remove := make(map[common.Hash]struct{})
563+
remove := make(map[common.Hash]struct{}, b.Len())
564564
for _, tx := range b {
565565
remove[tx.Hash()] = struct{}{}
566566
}

trie/trie.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ func (t *Trie) Witness() map[string]struct{} {
666666
if len(t.tracer.accessList) == 0 {
667667
return nil
668668
}
669-
witness := make(map[string]struct{})
669+
witness := make(map[string]struct{}, len(t.tracer.accessList))
670670
for _, node := range t.tracer.accessList {
671671
witness[string(node)] = struct{}{}
672672
}

0 commit comments

Comments
 (0)