Skip to content

Commit ec4334c

Browse files
committed
perf(trie): parallelize HashedPostStateSorted::from_reverts
Parallelizes the hashing/sorting step using Rayon when account count exceeds a threshold (2500). This alleviates CPU bottlenecks during large state reverts or deep reorgs. Closes #20049
1 parent cc3fadf commit ec4334c

File tree

5 files changed

+23
-246
lines changed

5 files changed

+23
-246
lines changed

Cargo.lock

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

crates/trie/db/Cargo.toml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,22 @@ proptest-arbitrary-interop.workspace = true
5050
serde_json.workspace = true
5151
similar-asserts.workspace = true
5252

53-
criterion.workspace = true
54-
rand.workspace = true
55-
5653
[features]
54+
default = ["std"]
55+
std = [
56+
"dep:rayon",
57+
"alloy-consensus/std",
58+
"alloy-primitives/std",
59+
"alloy-rlp/std",
60+
"reth-chainspec/std",
61+
"reth-execution-errors/std",
62+
"reth-primitives-traits/std",
63+
"revm/std",
64+
"revm-database/std",
65+
"serde_json/std",
66+
"reth-trie-common/std",
67+
"tracing/std",
68+
]
5769
metrics = ["reth-trie/metrics"]
5870
serde = [
5971
"similar-asserts/serde",
@@ -64,7 +76,6 @@ serde = [
6476
"reth-primitives-traits/serde",
6577
"revm-database/serde",
6678
"revm/serde",
67-
"rand/serde",
6879
]
6980
test-utils = [
7081
"reth-trie-common/test-utils",
@@ -75,14 +86,3 @@ test-utils = [
7586
"reth-provider/test-utils",
7687
"reth-trie/test-utils",
7788
]
78-
79-
parallel-from-reverts = ["dep:rayon"]
80-
81-
[[bench]]
82-
name = "sorting_par_exp"
83-
harness = false
84-
required-features = ["parallel-from-reverts"]
85-
86-
[[bench]]
87-
name = "integration_bench"
88-
harness = false

crates/trie/db/benches/integration_bench.rs

Lines changed: 0 additions & 110 deletions
This file was deleted.

crates/trie/db/benches/sorting_par_exp.rs

Lines changed: 0 additions & 113 deletions
This file was deleted.

crates/trie/db/src/state.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::{
1919
};
2020
use tracing::{debug, instrument};
2121

22-
#[cfg(feature = "parallel-from-reverts")]
22+
#[cfg(feature = "std")]
2323
use rayon::iter::{IntoParallelIterator, ParallelIterator};
2424

2525
/// Extends [`StateRoot`] with operations specific for working with a database transaction.
@@ -286,12 +286,14 @@ impl<TX: DbTx> DatabaseHashedPostState<TX> for HashedPostStateSorted {
286286
// Threshold based on benchmark
287287
const PARALLEL_THRESHOLD: usize = 2_500;
288288

289-
// Check Feature Flag AND Threshold
290-
let use_parallel =
291-
cfg!(feature = "parallel-from-reverts") && storages.len() >= PARALLEL_THRESHOLD;
289+
#[cfg(feature = "std")]
290+
let use_parallel = storages.len() >= PARALLEL_THRESHOLD;
291+
292+
#[cfg(not(feature = "std"))]
293+
let use_parallel = false;
292294

293295
let hashed_storages = if use_parallel {
294-
#[cfg(feature = "parallel-from-reverts")]
296+
#[cfg(feature = "std")]
295297
{
296298
storages
297299
.into_par_iter()
@@ -301,7 +303,7 @@ impl<TX: DbTx> DatabaseHashedPostState<TX> for HashedPostStateSorted {
301303
})
302304
.collect()
303305
}
304-
#[cfg(not(feature = "parallel-from-reverts"))]
306+
#[cfg(not(feature = "std"))]
305307
{
306308
unreachable!()
307309
}

0 commit comments

Comments
 (0)