From ec596e06a58f3c4c4bac0262ebfc57539c3e5fd1 Mon Sep 17 00:00:00 2001 From: maskpp Date: Mon, 16 Sep 2024 16:56:02 +0800 Subject: [PATCH] core, trie: prealloc capacity for maps (#30437) - preallocate capacity for map - avoid `reinject` adding empty value - use `maps.Copy` --- core/stateless/witness.go | 4 +--- core/txpool/blobpool/blobpool.go | 6 ++++-- core/types/transaction.go | 2 +- trie/trie.go | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/stateless/witness.go b/core/stateless/witness.go index 7622c5eb61..0f2d6cfe07 100644 --- a/core/stateless/witness.go +++ b/core/stateless/witness.go @@ -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 diff --git a/core/txpool/blobpool/blobpool.go b/core/txpool/blobpool/blobpool.go index aa76ef774d..685c017cbd 100644 --- a/core/txpool/blobpool/blobpool.go +++ b/core/txpool/blobpool/blobpool.go @@ -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])) @@ -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]) { diff --git a/core/types/transaction.go b/core/types/transaction.go index 4ac9187bdb..6c8759ee69 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -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{}{} } diff --git a/trie/trie.go b/trie/trie.go index f44e10b918..885b6b7962 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -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{}{} }