Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions crates/trie/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ alloy-primitives.workspace = true
# tracing
tracing.workspace = true

# rayon
rayon = { workspace = true, optional = true }

[dev-dependencies]
# reth
reth-chainspec.workspace = true
Expand All @@ -48,6 +51,21 @@ serde_json.workspace = true
similar-asserts.workspace = true

[features]
default = ["std"]
std = [
"dep:rayon",
"alloy-consensus/std",
"alloy-primitives/std",
"alloy-rlp/std",
"reth-chainspec/std",
"reth-execution-errors/std",
"reth-primitives-traits/std",
"revm/std",
"revm-database/std",
"serde_json/std",
"reth-trie-common/std",
"tracing/std",
]
metrics = ["reth-trie/metrics"]
serde = [
"similar-asserts/serde",
Expand Down
44 changes: 36 additions & 8 deletions crates/trie/db/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use std::{
};
use tracing::{debug, instrument};

#[cfg(feature = "std")]
use rayon::iter::{IntoParallelIterator, ParallelIterator};

/// Extends [`StateRoot`] with operations specific for working with a database transaction.
pub trait DatabaseStateRoot<'a, TX>: Sized {
/// Create a new [`StateRoot`] instance.
Expand Down Expand Up @@ -280,14 +283,39 @@ impl<TX: DbTx> DatabaseHashedPostState<TX> for HashedPostStateSorted {
}
}

// Sort storage slots and convert to HashedStorageSorted
let hashed_storages = storages
.into_iter()
.map(|(address, mut slots)| {
slots.sort_unstable_by_key(|(slot, _)| *slot);
(address, HashedStorageSorted { storage_slots: slots, wiped: false })
})
.collect();
// Threshold based on benchmark
const PARALLEL_THRESHOLD: usize = 2_500;

#[cfg(feature = "std")]
let use_parallel = storages.len() >= PARALLEL_THRESHOLD;

#[cfg(not(feature = "std"))]
let use_parallel = false;

let hashed_storages = if use_parallel {
#[cfg(feature = "std")]
{
storages
.into_par_iter()
.map(|(address, mut slots)| {
slots.sort_unstable_by_key(|(slot, _)| *slot);
(address, HashedStorageSorted { storage_slots: slots, wiped: false })
})
.collect()
}
#[cfg(not(feature = "std"))]
{
unreachable!()
}
} else {
storages
.into_iter()
.map(|(address, mut slots)| {
slots.sort_unstable_by_key(|(slot, _)| *slot);
(address, HashedStorageSorted { storage_slots: slots, wiped: false })
})
.collect()
};

Ok(Self::new(accounts, hashed_storages))
}
Expand Down
Loading