mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-04-30 03:01:58 -04:00
avoid extra alloc on map
- Before this change the code did:
StorageRootTargets::new(
prefix_sets.account_prefix_set.iter().map(|n|
B256::from_slice(&nibbles.pack())),
prefix_sets.storage_prefix_sets.clone(),
).len()
StorageRootTargets::new(..) builds a brand-new
B256Map<PrefixSet> by:
- cloning every entry in storage_prefix_sets (each PrefixSet can
hold dozens of nibble prefixes), and
- allocating a fresh B256Map to hold them.
- We only needed the resulting length, so those clones/allocations
were immediately thrown away.
This commit is contained in:
@@ -182,11 +182,10 @@ where
|
||||
// Extend prefix sets with targets
|
||||
let prefix_sets = Self::extend_prefix_sets_with_targets(&self.prefix_sets, &targets);
|
||||
|
||||
let storage_root_targets_len = StorageRootTargets::new(
|
||||
prefix_sets.account_prefix_set.iter().map(|nibbles| B256::from_slice(&nibbles.pack())),
|
||||
prefix_sets.storage_prefix_sets.clone(),
|
||||
)
|
||||
.len();
|
||||
let storage_root_targets_len = StorageRootTargets::count(
|
||||
&prefix_sets.account_prefix_set,
|
||||
&prefix_sets.storage_prefix_sets,
|
||||
);
|
||||
|
||||
trace!(
|
||||
target: "trie::parallel_proof",
|
||||
|
||||
@@ -24,6 +24,23 @@ impl StorageRootTargets {
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns the total number of unique storage root targets without allocating new maps.
|
||||
pub fn count(
|
||||
account_prefix_set: &PrefixSet,
|
||||
storage_prefix_sets: &B256Map<PrefixSet>,
|
||||
) -> usize {
|
||||
let mut count = storage_prefix_sets.len();
|
||||
|
||||
for nibbles in account_prefix_set.iter() {
|
||||
let hashed_address = B256::from_slice(&nibbles.pack());
|
||||
if !storage_prefix_sets.contains_key(&hashed_address) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
count
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoIterator for StorageRootTargets {
|
||||
|
||||
Reference in New Issue
Block a user