Skip to content

Commit ac72e35

Browse files
perf: dashmap for nonces (#8)
1 parent 6d042bd commit ac72e35

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configs/max.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
inherits = "aggressive.toml"
2+
3+
[rate_limiting]
4+
initial_ratelimit = 500
5+
ratelimit_thresholds = [
6+
[2_500, 1_000],
7+
[5_000, 2_500],
8+
[25_000, 500_000],
9+
]
10+
11+
[network_worker]
12+
total_connections = 20_000
13+
tx_queue_empty_sleep_ms = 5
14+
15+
[workers]
16+
tx_gen_worker_percentage = 0.3
17+
network_worker_percentage = 0.7

crescendo/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ rand = "0.9.1"
2929
ratelimit = "0.10"
3030
clap = { version = "4.5", features = ["derive", "env"] }
3131
toml = "0.8"
32-
parking_lot = "0.12.4"
32+
parking_lot = "0.12.4"
33+
dashmap = "6.1"

crescendo/src/workers/tx_gen.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::HashMap;
21
use std::sync::LazyLock;
32
use std::time::Instant;
43

@@ -9,20 +8,20 @@ use alloy::sol_types::SolCall;
98
use alloy_consensus::{SignableTransaction, TxLegacy};
109
use alloy_signer_local::coins_bip39::English;
1110
use alloy_signer_local::{MnemonicBuilder, PrivateKeySigner};
12-
use parking_lot::Mutex;
11+
use dashmap::DashMap;
1312
use rand::Rng;
1413
use rayon::prelude::*;
1514
use thousands::Separable;
1615

1716
use crate::config;
1817
use crate::tx_queue::TX_QUEUE;
1918

20-
static NONCE_MAP: LazyLock<Mutex<HashMap<u32, u64>>> = LazyLock::new(|| {
21-
let mut map = HashMap::with_capacity(config::get().tx_gen_worker.num_accounts as usize);
19+
static NONCE_MAP: LazyLock<DashMap<u32, u64>> = LazyLock::new(|| {
20+
let map = DashMap::with_capacity(config::get().tx_gen_worker.num_accounts as usize);
2221
for i in 0..config::get().tx_gen_worker.num_accounts {
2322
map.insert(i, 0);
2423
}
25-
Mutex::new(map)
24+
map
2625
});
2726

2827
static SIGNER_LIST: LazyLock<Vec<PrivateKeySigner>> = LazyLock::new(|| {
@@ -54,9 +53,9 @@ pub fn tx_gen_worker(_worker_id: u32) {
5453

5554
// Get and increment nonce atomically.
5655
let nonce = {
57-
let mut nonce_map = NONCE_MAP.lock();
58-
let current_nonce = *nonce_map.get(&account_index).unwrap();
59-
nonce_map.insert(account_index, current_nonce + 1);
56+
let mut entry = NONCE_MAP.get_mut(&account_index).unwrap();
57+
let current_nonce = *entry;
58+
*entry = current_nonce + 1;
6059
current_nonce
6160
};
6261

testserver/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static CONCURRENT_REQUESTS: CachePadded<AtomicU64> = CachePadded::new(AtomicU64:
1717

1818
async fn handler() -> Json<serde_json::Value> {
1919
CONCURRENT_REQUESTS.fetch_add(1, Ordering::Relaxed);
20-
tokio::time::sleep(Duration::from_millis(500)).await; // Simulate processing time.
20+
tokio::time::sleep(Duration::from_millis(1)).await; // Simulate processing time.
2121
CONCURRENT_REQUESTS.fetch_sub(1, Ordering::Relaxed);
2222
TOTAL_REQUESTS.fetch_add(1, Ordering::Relaxed);
2323
Json(json!({

0 commit comments

Comments
 (0)