From a0326e4f86e43fa14eac0060fc5770cccdcc89b8 Mon Sep 17 00:00:00 2001 From: Hai | RISE <150876604+hai-rise@users.noreply.github.com> Date: Sat, 7 Dec 2024 03:35:30 +0700 Subject: [PATCH] perf: more `FxHashMap`s for `SenderId` key (#13188) --- crates/transaction-pool/src/pool/mod.rs | 10 +++------- crates/transaction-pool/src/pool/pending.rs | 9 +++++---- crates/transaction-pool/src/pool/txpool.rs | 6 +++--- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/crates/transaction-pool/src/pool/mod.rs b/crates/transaction-pool/src/pool/mod.rs index d93b4a14d8..89c4d6d346 100644 --- a/crates/transaction-pool/src/pool/mod.rs +++ b/crates/transaction-pool/src/pool/mod.rs @@ -89,12 +89,8 @@ use reth_execution_types::ChangedAccount; use alloy_eips::eip4844::BlobTransactionSidecar; use reth_primitives::RecoveredTx; -use std::{ - collections::{HashMap, HashSet}, - fmt, - sync::Arc, - time::Instant, -}; +use rustc_hash::FxHashMap; +use std::{collections::HashSet, fmt, sync::Arc, time::Instant}; use tokio::sync::mpsc; use tracing::{debug, trace, warn}; mod events; @@ -216,7 +212,7 @@ where fn changed_senders( &self, accs: impl Iterator, - ) -> HashMap { + ) -> FxHashMap { let mut identifiers = self.identifiers.write(); accs.into_iter() .map(|acc| { diff --git a/crates/transaction-pool/src/pool/pending.rs b/crates/transaction-pool/src/pool/pending.rs index 89e673aad9..27706bd175 100644 --- a/crates/transaction-pool/src/pool/pending.rs +++ b/crates/transaction-pool/src/pool/pending.rs @@ -6,9 +6,10 @@ use crate::{ }, Priority, SubPoolLimit, TransactionOrdering, ValidPoolTransaction, }; +use rustc_hash::FxHashMap; use std::{ cmp::Ordering, - collections::{hash_map::Entry, BTreeMap, HashMap}, + collections::{hash_map::Entry, BTreeMap}, ops::Bound::Unbounded, sync::Arc, }; @@ -36,10 +37,10 @@ pub struct PendingPool { by_id: BTreeMap>, /// The highest nonce transactions for each sender - like the `independent` set, but the /// highest instead of lowest nonce. - highest_nonces: HashMap>, + highest_nonces: FxHashMap>, /// Independent transactions that can be included directly and don't require other /// transactions. - independent_transactions: HashMap>, + independent_transactions: FxHashMap>, /// Keeps track of the size of this pool. /// /// See also [`PoolTransaction::size`](crate::traits::PoolTransaction::size). @@ -523,7 +524,7 @@ impl PendingPool { /// Returns a reference to the independent transactions in the pool #[cfg(test)] - pub(crate) const fn independent(&self) -> &HashMap> { + pub(crate) const fn independent(&self) -> &FxHashMap> { &self.independent_transactions } diff --git a/crates/transaction-pool/src/pool/txpool.rs b/crates/transaction-pool/src/pool/txpool.rs index 1b330543cf..11212e0aa3 100644 --- a/crates/transaction-pool/src/pool/txpool.rs +++ b/crates/transaction-pool/src/pool/txpool.rs @@ -460,7 +460,7 @@ impl TxPool { /// Updates the transactions for the changed senders. pub(crate) fn update_accounts( &mut self, - changed_senders: HashMap, + changed_senders: FxHashMap, ) -> UpdateOutcome { // track changed accounts self.sender_info.extend(changed_senders.clone()); @@ -481,7 +481,7 @@ impl TxPool { &mut self, block_info: BlockInfo, mined_transactions: Vec, - changed_senders: HashMap, + changed_senders: FxHashMap, update_kind: PoolUpdateKind, ) -> OnNewCanonicalStateOutcome { // update block info @@ -1180,7 +1180,7 @@ impl AllTransactions { /// that got transaction included in the block. pub(crate) fn update( &mut self, - changed_accounts: HashMap, + changed_accounts: FxHashMap, ) -> Vec { // pre-allocate a few updates let mut updates = Vec::with_capacity(64);