Skip to content

Commit 7c180f8

Browse files
authored
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.
1 parent 2055196 commit 7c180f8

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

internal/ethapi/api.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,23 @@ func NewTxPoolAPI(b Backend) *TxPoolAPI {
186186

187187
// Content returns the transactions contained within the transaction pool.
188188
func (api *TxPoolAPI) Content() map[string]map[string]map[string]*RPCTransaction {
189+
pending, queue := api.b.TxPoolContent()
189190
content := map[string]map[string]map[string]*RPCTransaction{
190-
"pending": make(map[string]map[string]*RPCTransaction),
191-
"queued": make(map[string]map[string]*RPCTransaction),
191+
"pending": make(map[string]map[string]*RPCTransaction, len(pending)),
192+
"queued": make(map[string]map[string]*RPCTransaction, len(queue)),
192193
}
193-
pending, queue := api.b.TxPoolContent()
194194
curHeader := api.b.CurrentHeader()
195195
// Flatten the pending transactions
196196
for account, txs := range pending {
197-
dump := make(map[string]*RPCTransaction)
197+
dump := make(map[string]*RPCTransaction, len(txs))
198198
for _, tx := range txs {
199199
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, api.b.ChainConfig())
200200
}
201201
content["pending"][account.Hex()] = dump
202202
}
203203
// Flatten the queued transactions
204204
for account, txs := range queue {
205-
dump := make(map[string]*RPCTransaction)
205+
dump := make(map[string]*RPCTransaction, len(txs))
206206
for _, tx := range txs {
207207
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, api.b.ChainConfig())
208208
}
@@ -246,11 +246,11 @@ func (api *TxPoolAPI) Status() map[string]hexutil.Uint {
246246
// Inspect retrieves the content of the transaction pool and flattens it into an
247247
// easily inspectable list.
248248
func (api *TxPoolAPI) Inspect() map[string]map[string]map[string]string {
249+
pending, queue := api.b.TxPoolContent()
249250
content := map[string]map[string]map[string]string{
250-
"pending": make(map[string]map[string]string),
251-
"queued": make(map[string]map[string]string),
251+
"pending": make(map[string]map[string]string, len(pending)),
252+
"queued": make(map[string]map[string]string, len(queue)),
252253
}
253-
pending, queue := api.b.TxPoolContent()
254254

255255
// Define a formatter to flatten a transaction into a string
256256
format := func(tx *types.Transaction) string {
@@ -261,15 +261,15 @@ func (api *TxPoolAPI) Inspect() map[string]map[string]map[string]string {
261261
}
262262
// Flatten the pending transactions
263263
for account, txs := range pending {
264-
dump := make(map[string]string)
264+
dump := make(map[string]string, len(txs))
265265
for _, tx := range txs {
266266
dump[fmt.Sprintf("%d", tx.Nonce())] = format(tx)
267267
}
268268
content["pending"][account.Hex()] = dump
269269
}
270270
// Flatten the queued transactions
271271
for account, txs := range queue {
272-
dump := make(map[string]string)
272+
dump := make(map[string]string, len(txs))
273273
for _, tx := range txs {
274274
dump[fmt.Sprintf("%d", tx.Nonce())] = format(tx)
275275
}

0 commit comments

Comments
 (0)