internal/ethapi: prealloc map for the txpool api (#32110)

use `make(map, len(txpool))` to prealloc the map for the txpool content,
to avoid the map growing in the loop.
This commit is contained in:
nthumann 2025-07-01 08:07:33 +01:00 committed by GitHub
parent 2055196cea
commit 7c180f851c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -186,15 +186,15 @@ func NewTxPoolAPI(b Backend) *TxPoolAPI {
// Content returns the transactions contained within the transaction pool. // Content returns the transactions contained within the transaction pool.
func (api *TxPoolAPI) Content() map[string]map[string]map[string]*RPCTransaction { func (api *TxPoolAPI) Content() map[string]map[string]map[string]*RPCTransaction {
content := map[string]map[string]map[string]*RPCTransaction{
"pending": make(map[string]map[string]*RPCTransaction),
"queued": make(map[string]map[string]*RPCTransaction),
}
pending, queue := api.b.TxPoolContent() pending, queue := api.b.TxPoolContent()
content := map[string]map[string]map[string]*RPCTransaction{
"pending": make(map[string]map[string]*RPCTransaction, len(pending)),
"queued": make(map[string]map[string]*RPCTransaction, len(queue)),
}
curHeader := api.b.CurrentHeader() curHeader := api.b.CurrentHeader()
// Flatten the pending transactions // Flatten the pending transactions
for account, txs := range pending { for account, txs := range pending {
dump := make(map[string]*RPCTransaction) dump := make(map[string]*RPCTransaction, len(txs))
for _, tx := range txs { for _, tx := range txs {
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, api.b.ChainConfig()) dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, api.b.ChainConfig())
} }
@ -202,7 +202,7 @@ func (api *TxPoolAPI) Content() map[string]map[string]map[string]*RPCTransaction
} }
// Flatten the queued transactions // Flatten the queued transactions
for account, txs := range queue { for account, txs := range queue {
dump := make(map[string]*RPCTransaction) dump := make(map[string]*RPCTransaction, len(txs))
for _, tx := range txs { for _, tx := range txs {
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, api.b.ChainConfig()) dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, api.b.ChainConfig())
} }
@ -246,11 +246,11 @@ func (api *TxPoolAPI) Status() map[string]hexutil.Uint {
// Inspect retrieves the content of the transaction pool and flattens it into an // Inspect retrieves the content of the transaction pool and flattens it into an
// easily inspectable list. // easily inspectable list.
func (api *TxPoolAPI) Inspect() map[string]map[string]map[string]string { func (api *TxPoolAPI) Inspect() map[string]map[string]map[string]string {
content := map[string]map[string]map[string]string{
"pending": make(map[string]map[string]string),
"queued": make(map[string]map[string]string),
}
pending, queue := api.b.TxPoolContent() pending, queue := api.b.TxPoolContent()
content := map[string]map[string]map[string]string{
"pending": make(map[string]map[string]string, len(pending)),
"queued": make(map[string]map[string]string, len(queue)),
}
// Define a formatter to flatten a transaction into a string // Define a formatter to flatten a transaction into a string
format := func(tx *types.Transaction) string { format := func(tx *types.Transaction) string {
@ -261,7 +261,7 @@ func (api *TxPoolAPI) Inspect() map[string]map[string]map[string]string {
} }
// Flatten the pending transactions // Flatten the pending transactions
for account, txs := range pending { for account, txs := range pending {
dump := make(map[string]string) dump := make(map[string]string, len(txs))
for _, tx := range txs { for _, tx := range txs {
dump[fmt.Sprintf("%d", tx.Nonce())] = format(tx) dump[fmt.Sprintf("%d", tx.Nonce())] = format(tx)
} }
@ -269,7 +269,7 @@ func (api *TxPoolAPI) Inspect() map[string]map[string]map[string]string {
} }
// Flatten the queued transactions // Flatten the queued transactions
for account, txs := range queue { for account, txs := range queue {
dump := make(map[string]string) dump := make(map[string]string, len(txs))
for _, tx := range txs { for _, tx := range txs {
dump[fmt.Sprintf("%d", tx.Nonce())] = format(tx) dump[fmt.Sprintf("%d", tx.Nonce())] = format(tx)
} }