Skip to content

Commit a84c7df

Browse files
committed
Switch hot EVM and mempool HashMaps to FxHashMap for faster hashing
1 parent 45a02ce commit a84c7df

File tree

6 files changed

+27
-23
lines changed

6 files changed

+27
-23
lines changed

crates/blockchain/mempool.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::{
2-
collections::{BTreeMap, HashMap, VecDeque},
2+
collections::{BTreeMap, VecDeque},
33
sync::RwLock,
44
};
55

6+
use rustc_hash::FxHashMap;
7+
68
use crate::{
79
constants::{
810
TX_ACCESS_LIST_ADDRESS_GAS, TX_ACCESS_LIST_STORAGE_KEY_GAS, TX_CREATE_GAS_COST,
@@ -22,8 +24,8 @@ use tracing::warn;
2224
#[derive(Debug, Default)]
2325
struct MempoolInner {
2426
broadcast_pool: HashSet<H256>,
25-
transaction_pool: HashMap<H256, MempoolTransaction>,
26-
blobs_bundle_pool: HashMap<H256, BlobsBundle>,
27+
transaction_pool: FxHashMap<H256, MempoolTransaction>,
28+
blobs_bundle_pool: FxHashMap<H256, BlobsBundle>,
2729
txs_by_sender_nonce: BTreeMap<(H160, u64), H256>,
2830
txs_order: VecDeque<H256>,
2931
max_mempool_size: usize,
@@ -35,7 +37,7 @@ impl MempoolInner {
3537
fn new(max_mempool_size: usize) -> Self {
3638
MempoolInner {
3739
txs_order: VecDeque::with_capacity(max_mempool_size * 2),
38-
transaction_pool: HashMap::with_capacity(max_mempool_size),
40+
transaction_pool: FxHashMap::with_capacity_and_hasher(max_mempool_size, Default::default()),
3941
max_mempool_size,
4042
mempool_prune_threshold: max_mempool_size + max_mempool_size / 2,
4143
..Default::default()
@@ -191,7 +193,7 @@ impl Mempool {
191193
pub fn filter_transactions(
192194
&self,
193195
filter: &PendingTxFilter,
194-
) -> Result<HashMap<Address, Vec<MempoolTransaction>>, StoreError> {
196+
) -> Result<FxHashMap<Address, Vec<MempoolTransaction>>, StoreError> {
195197
let filter_tx = |tx: &Transaction| -> bool {
196198
// Filter by tx type
197199
let is_blob_tx = matches!(tx, Transaction::EIP4844Transaction(_));
@@ -229,9 +231,9 @@ impl Mempool {
229231
/// Gets all the transactions in the mempool
230232
pub fn get_all_txs_by_sender(
231233
&self,
232-
) -> Result<HashMap<Address, Vec<MempoolTransaction>>, StoreError> {
233-
let mut txs_by_sender: HashMap<Address, Vec<MempoolTransaction>> =
234-
HashMap::with_capacity(128);
234+
) -> Result<FxHashMap<Address, Vec<MempoolTransaction>>, StoreError> {
235+
let mut txs_by_sender: FxHashMap<Address, Vec<MempoolTransaction>> =
236+
FxHashMap::with_capacity_and_hasher(128, Default::default());
235237
let tx_pool = &self.read()?.transaction_pool;
236238

237239
for (_, tx) in tx_pool.iter() {
@@ -250,9 +252,9 @@ impl Mempool {
250252
pub fn filter_transactions_with_filter_fn(
251253
&self,
252254
filter: &dyn Fn(&Transaction) -> bool,
253-
) -> Result<HashMap<Address, Vec<MempoolTransaction>>, StoreError> {
254-
let mut txs_by_sender: HashMap<Address, Vec<MempoolTransaction>> =
255-
HashMap::with_capacity(128);
255+
) -> Result<FxHashMap<Address, Vec<MempoolTransaction>>, StoreError> {
256+
let mut txs_by_sender: FxHashMap<Address, Vec<MempoolTransaction>> =
257+
FxHashMap::with_capacity_and_hasher(128, Default::default());
256258
let tx_pool = &self.read()?.transaction_pool;
257259

258260
for (_, tx) in tx_pool.iter() {

crates/blockchain/payload.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::{
22
cmp::{Ordering, max},
3-
collections::HashMap,
43
ops::Div,
54
sync::Arc,
65
time::{Duration, Instant},
76
};
87

8+
use rustc_hash::FxHashMap;
9+
910
use ethrex_common::{
1011
Address, Bloom, Bytes, H256, U256,
1112
constants::{DEFAULT_OMMERS_HASH, DEFAULT_REQUESTS_HASH, GAS_PER_BLOB, MAX_RLP_BLOCK_SIZE},
@@ -764,7 +765,7 @@ pub struct TransactionQueue {
764765
// The first transaction for each account along with its tip, sorted by highest tip
765766
heads: Vec<HeadTransaction>,
766767
// The remaining txs grouped by account and sorted by nonce
767-
txs: HashMap<Address, Vec<MempoolTransaction>>,
768+
txs: FxHashMap<Address, Vec<MempoolTransaction>>,
768769
// Base Fee stored for tip calculations
769770
base_fee: Option<u64>,
770771
}
@@ -792,7 +793,7 @@ impl From<HeadTransaction> for Transaction {
792793
impl TransactionQueue {
793794
/// Creates a new TransactionQueue from a set of transactions grouped by sender and sorted by nonce
794795
fn new(
795-
mut txs: HashMap<Address, Vec<MempoolTransaction>>,
796+
mut txs: FxHashMap<Address, Vec<MempoolTransaction>>,
796797
base_fee: Option<u64>,
797798
) -> Result<Self, ChainError> {
798799
let mut heads = Vec::with_capacity(100);

crates/vm/levm/src/call_frame.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use bytes::Bytes;
1010
use ethrex_common::types::block_access_list::BlockAccessListCheckpoint;
1111
use ethrex_common::{Address, U256};
1212
use ethrex_common::{H256, types::Code};
13-
use std::{collections::HashMap, fmt, hint::assert_unchecked};
13+
use rustc_hash::FxHashMap;
14+
use std::{fmt, hint::assert_unchecked};
1415

1516
/// [`u64`]s that make up a [`U256`]
1617
const U64_PER_U256: usize = U256::MAX.0.len();
@@ -273,8 +274,8 @@ pub struct CallFrame {
273274

274275
#[derive(Debug, Clone, Eq, PartialEq, Default)]
275276
pub struct CallFrameBackup {
276-
pub original_accounts_info: HashMap<Address, LevmAccount>,
277-
pub original_account_storage_slots: HashMap<Address, HashMap<H256, U256>>,
277+
pub original_accounts_info: FxHashMap<Address, LevmAccount>,
278+
pub original_account_storage_slots: FxHashMap<Address, FxHashMap<H256, U256>>,
278279
/// BAL checkpoint for EIP-7928 - used to restore state changes on revert
279280
/// while preserving touched_addresses.
280281
pub bal_checkpoint: Option<BlockAccessListCheckpoint>,

crates/vm/levm/src/environment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use crate::constants::{
88
MAX_BLOB_COUNT_ELECTRA, TARGET_BLOB_GAS_PER_BLOCK, TARGET_BLOB_GAS_PER_BLOCK_PECTRA,
99
};
1010

11-
use std::collections::HashMap;
11+
use rustc_hash::FxHashMap;
1212
/// [EIP-1153]: https://eips.ethereum.org/EIPS/eip-1153#reference-implementation
13-
pub type TransientStorage = HashMap<(Address, U256), U256>;
13+
pub type TransientStorage = FxHashMap<(Address, U256), U256>;
1414

1515
#[derive(Debug, Default, Clone)]
1616
/// Environmental information that the execution agent must provide.

crates/vm/levm/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use ethrex_common::{
2424
};
2525
use ethrex_common::{types::TxKind, utils::u256_from_big_endian_const};
2626
use ethrex_rlp;
27-
use std::collections::HashMap;
28-
pub type Storage = HashMap<U256, H256>;
27+
use rustc_hash::FxHashMap;
28+
pub type Storage = FxHashMap<U256, H256>;
2929

3030
// ================== Address related functions ======================
3131
/// Converts address (H160) to word (U256)

crates/vm/levm/src/vm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ use ethrex_common::{
2525
use rustc_hash::{FxHashMap, FxHashSet};
2626
use std::{
2727
cell::RefCell,
28-
collections::{BTreeMap, BTreeSet, HashMap},
28+
collections::{BTreeMap, BTreeSet},
2929
mem,
3030
rc::Rc,
3131
};
3232

3333
/// Storage mapping from slot key to value.
34-
pub type Storage = HashMap<U256, H256>;
34+
pub type Storage = FxHashMap<U256, H256>;
3535

3636
/// Specifies whether the VM operates in L1 or L2 mode.
3737
#[derive(Debug, Clone, Copy, Default)]

0 commit comments

Comments
 (0)