Skip to content
Merged
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
32 changes: 11 additions & 21 deletions crates/primitives/benches/trie_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,24 @@ pub fn trie_root_benchmark(c: &mut Criterion) {
let group_name =
|description: &str| format!("receipts root | size: {size} | {description}");

let (test_data, expected) = generate_test_data(size);
use implementations::*;
let receipts = &generate_test_data(size)[..];
assert_eq!(trie_hash_ordered_trie_root(receipts), hash_builder_root(receipts));

group.bench_function(group_name("triehash::ordered_trie_root"), |b| {
b.iter(|| {
let receipts = test_data.clone();
let result = black_box(trie_hash_ordered_trie_root(receipts.into_iter()));
assert_eq!(result, expected);
});
b.iter(|| trie_hash_ordered_trie_root(black_box(receipts)));
});

group.bench_function(group_name("HashBuilder"), |b| {
b.iter(|| {
let receipts = test_data.clone();
let result = black_box(hash_builder_root(receipts));
assert_eq!(result, expected);
});
b.iter(|| hash_builder_root(black_box(receipts)));
});
}
}

fn generate_test_data(size: usize) -> (Vec<ReceiptWithBloom>, H256) {
let receipts = prop::collection::vec(any::<ReceiptWithBloom>(), size)
fn generate_test_data(size: usize) -> Vec<ReceiptWithBloom> {
prop::collection::vec(any::<ReceiptWithBloom>(), size)
.new_tree(&mut TestRunner::new(ProptestConfig::default()))
.unwrap()
.current();
let root = implementations::hash_builder_root(receipts.clone());
(receipts, root)
.current()
}

criterion_group! {
Expand All @@ -59,17 +49,16 @@ mod implementations {
proofs::adjust_index_for_rlp,
trie::{HashBuilder, Nibbles},
};
use std::vec::IntoIter;

pub fn trie_hash_ordered_trie_root(receipts: IntoIter<ReceiptWithBloom>) -> H256 {
triehash::ordered_trie_root::<KeccakHasher, _>(receipts.map(|receipt| {
pub fn trie_hash_ordered_trie_root(receipts: &[ReceiptWithBloom]) -> H256 {
triehash::ordered_trie_root::<KeccakHasher, _>(receipts.iter().map(|receipt| {
let mut receipt_rlp = Vec::new();
receipt.encode_inner(&mut receipt_rlp, false);
receipt_rlp
}))
}

pub fn hash_builder_root(receipts: Vec<ReceiptWithBloom>) -> H256 {
pub fn hash_builder_root(receipts: &[ReceiptWithBloom]) -> H256 {
let mut index_buffer = BytesMut::new();
let mut value_buffer = BytesMut::new();

Expand All @@ -90,3 +79,4 @@ mod implementations {
hb.root()
}
}
use implementations::*;