Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bench(trie): RevealedSparseTrie::update_rlp_node_level #12046

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions crates/trie/sparse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ rand.workspace = true
[[bench]]
name = "root"
harness = false

[[bench]]
name = "rlp_node"
harness = false
78 changes: 78 additions & 0 deletions crates/trie/sparse/benches/rlp_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#![allow(missing_docs, unreachable_pub)]

use std::time::{Duration, Instant};

use alloy_primitives::{B256, U256};
use criterion::{criterion_group, criterion_main, Criterion};
use prop::strategy::ValueTree;
use proptest::{prelude::*, test_runner::TestRunner};
use rand::seq::IteratorRandom;
use reth_testing_utils::generators;
use reth_trie::Nibbles;
use reth_trie_sparse::RevealedSparseTrie;

pub fn update_rlp_node_level(c: &mut Criterion) {
let mut rng = generators::rng();

let mut group = c.benchmark_group("update rlp node level");
group.sample_size(20);

for size in [100_000] {
let mut runner = TestRunner::new(ProptestConfig::default());
let state = proptest::collection::hash_map(any::<B256>(), any::<U256>(), size)
.new_tree(&mut runner)
.unwrap()
.current();

// Create a sparse trie with `size` leaves
let mut sparse = RevealedSparseTrie::default();
for (key, value) in &state {
sparse
.update_leaf(Nibbles::unpack(key), alloy_rlp::encode_fixed_size(value).to_vec())
.unwrap();
}
sparse.root();

for updated_leaves in [0.1, 1.0] {
for key in state
.keys()
.choose_multiple(&mut rng, (size as f64 * (updated_leaves / 100.0)) as usize)
{
sparse
.update_leaf(
Nibbles::unpack(key),
alloy_rlp::encode_fixed_size(&rng.gen::<U256>()).to_vec(),
)
.unwrap();
}

// Calculate the maximum depth of the trie for the given number of leaves
let max_depth = (size as f64).log(16.0).ceil() as usize;

for depth in 0..=max_depth {
group.bench_function(
format!("size {size} | updated {updated_leaves}% | depth {depth}"),
|b| {
// Use `iter_custom` to avoid measuring clones and drops
b.iter_custom(|iters| {
let mut elapsed = Duration::ZERO;

let mut cloned = sparse.clone();
for _ in 0..iters {
let start = Instant::now();
cloned.update_rlp_node_level(depth);
elapsed += start.elapsed();
cloned = sparse.clone();
}

elapsed
})
},
);
}
}
}
}

criterion_group!(rlp_node, update_rlp_node_level);
criterion_main!(rlp_node);
1 change: 1 addition & 0 deletions crates/trie/sparse/benches/root.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(missing_docs, unreachable_pub)]

use alloy_primitives::{map::HashMap, B256, U256};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use itertools::Itertools;
Expand Down
2 changes: 1 addition & 1 deletion crates/trie/sparse/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl SparseTrie {
/// - Each leaf entry in `nodes` collection must have a corresponding entry in `values` collection.
/// The opposite is also true.
/// - All keys in `values` collection are full leaf paths.
#[derive(PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq)]
pub struct RevealedSparseTrie {
/// All trie nodes.
nodes: HashMap<Nibbles, SparseNode>,
Expand Down
Loading