perf: trie micro optimizations (#13282)

This commit is contained in:
DaniPopes
2024-12-11 05:52:42 +01:00
committed by GitHub
parent 0144a433df
commit 0494ca01d5
37 changed files with 306 additions and 246 deletions

View File

@ -92,13 +92,10 @@ mod tests {
assert_eq!(trie_account.code_hash, KECCAK_EMPTY);
// Check that the default Account converts to the same TrieAccount
assert_eq!(Into::<TrieAccount>::into((Account::default(), EMPTY_ROOT_HASH)), trie_account);
assert_eq!(TrieAccount::from((Account::default(), EMPTY_ROOT_HASH)), trie_account);
// Check that the default AccountInfo converts to the same TrieAccount
assert_eq!(
Into::<TrieAccount>::into((AccountInfo::default(), EMPTY_ROOT_HASH)),
trie_account
);
assert_eq!(TrieAccount::from((AccountInfo::default(), EMPTY_ROOT_HASH)), trie_account);
}
#[test]
@ -131,7 +128,7 @@ mod tests {
// Check that the Account converts to the same TrieAccount
assert_eq!(
Into::<TrieAccount>::into((
TrieAccount::from((
Account {
nonce: 10,
balance: U256::from(1000),
@ -144,7 +141,7 @@ mod tests {
// Check that the AccountInfo converts to the same TrieAccount
assert_eq!(
Into::<TrieAccount>::into((
TrieAccount::from((
AccountInfo {
nonce: 10,
balance: U256::from(1000),

View File

@ -1,8 +1,5 @@
use crate::Nibbles;
use alloy_primitives::{
map::{HashMap, HashSet},
B256,
};
use alloy_primitives::map::{B256HashMap, B256HashSet};
use std::sync::Arc;
/// Collection of mutable prefix sets.
@ -12,9 +9,9 @@ pub struct TriePrefixSetsMut {
pub account_prefix_set: PrefixSetMut,
/// A map containing storage changes with the hashed address as key and a set of storage key
/// prefixes as the value.
pub storage_prefix_sets: HashMap<B256, PrefixSetMut>,
pub storage_prefix_sets: B256HashMap<PrefixSetMut>,
/// A set of hashed addresses of destroyed accounts.
pub destroyed_accounts: HashSet<B256>,
pub destroyed_accounts: B256HashSet,
}
impl TriePrefixSetsMut {
@ -50,9 +47,9 @@ pub struct TriePrefixSets {
pub account_prefix_set: PrefixSet,
/// A map containing storage changes with the hashed address as key and a set of storage key
/// prefixes as the value.
pub storage_prefix_sets: HashMap<B256, PrefixSet>,
pub storage_prefix_sets: B256HashMap<PrefixSet>,
/// A set of hashed addresses of destroyed accounts.
pub destroyed_accounts: HashSet<B256>,
pub destroyed_accounts: B256HashSet,
}
/// A container for efficiently storing and checking for the presence of key prefixes.
@ -146,9 +143,9 @@ impl PrefixSetMut {
if self.all {
PrefixSet { index: 0, all: true, keys: Arc::new(Vec::new()) }
} else {
self.keys.sort();
self.keys.sort_unstable();
self.keys.dedup();
// we need to shrink in both the sorted and non-sorted cases because deduping may have
// We need to shrink in both the sorted and non-sorted cases because deduping may have
// occurred either on `freeze`, or during `contains`.
self.keys.shrink_to_fit();
PrefixSet { index: 0, all: false, keys: Arc::new(self.keys) }

View File

@ -4,7 +4,7 @@ use crate::{Nibbles, TrieAccount};
use alloy_consensus::constants::KECCAK_EMPTY;
use alloy_primitives::{
keccak256,
map::{hash_map, HashMap},
map::{hash_map, B256HashMap, B256HashSet, HashMap},
Address, Bytes, B256, U256,
};
use alloy_rlp::{encode_fixed_size, Decodable, EMPTY_STRING_CODE};
@ -16,6 +16,9 @@ use alloy_trie::{
use itertools::Itertools;
use reth_primitives_traits::Account;
/// Proof targets map.
pub type MultiProofTargets = B256HashMap<B256HashSet>;
/// The state multiproof of target accounts and multiproofs of their storage tries.
/// Multiproof is effectively a state subtrie that only contains the nodes
/// in the paths of target accounts.
@ -26,7 +29,7 @@ pub struct MultiProof {
/// The hash masks of the branch nodes in the account proof.
pub branch_node_hash_masks: HashMap<Nibbles, TrieMask>,
/// Storage trie multiproofs.
pub storages: HashMap<B256, StorageMultiProof>,
pub storages: B256HashMap<StorageMultiProof>,
}
impl MultiProof {

View File

@ -1,6 +1,6 @@
use crate::{BranchNodeCompact, HashBuilder, Nibbles};
use alloy_primitives::{
map::{HashMap, HashSet},
map::{B256HashMap, B256HashSet, HashMap, HashSet},
B256,
};
@ -15,7 +15,7 @@ pub struct TrieUpdates {
#[cfg_attr(any(test, feature = "serde"), serde(with = "serde_nibbles_set"))]
pub removed_nodes: HashSet<Nibbles>,
/// Collection of updated storage tries indexed by the hashed address.
pub storage_tries: HashMap<B256, StorageTrieUpdates>,
pub storage_tries: B256HashMap<StorageTrieUpdates>,
}
impl TrieUpdates {
@ -37,7 +37,7 @@ impl TrieUpdates {
}
/// Returns a reference to updated storage tries.
pub const fn storage_tries_ref(&self) -> &HashMap<B256, StorageTrieUpdates> {
pub const fn storage_tries_ref(&self) -> &B256HashMap<StorageTrieUpdates> {
&self.storage_tries
}
@ -84,7 +84,7 @@ impl TrieUpdates {
&mut self,
hash_builder: HashBuilder,
removed_keys: HashSet<Nibbles>,
destroyed_accounts: HashSet<B256>,
destroyed_accounts: B256HashSet,
) {
// Retrieve updated nodes from hash builder.
let (_, updated_nodes) = hash_builder.split();
@ -347,7 +347,7 @@ pub struct TrieUpdatesSorted {
pub removed_nodes: HashSet<Nibbles>,
/// Storage tries storage stored by hashed address of the account
/// the trie belongs to.
pub storage_tries: HashMap<B256, StorageTrieUpdatesSorted>,
pub storage_tries: B256HashMap<StorageTrieUpdatesSorted>,
}
impl TrieUpdatesSorted {
@ -362,7 +362,7 @@ impl TrieUpdatesSorted {
}
/// Returns reference to updated storage tries.
pub const fn storage_tries_ref(&self) -> &HashMap<B256, StorageTrieUpdatesSorted> {
pub const fn storage_tries_ref(&self) -> &B256HashMap<StorageTrieUpdatesSorted> {
&self.storage_tries
}
}
@ -411,10 +411,7 @@ fn exclude_empty_from_pair<V>(
#[cfg(feature = "serde-bincode-compat")]
pub mod serde_bincode_compat {
use crate::{BranchNodeCompact, Nibbles};
use alloy_primitives::{
map::{HashMap, HashSet},
B256,
};
use alloy_primitives::map::{B256HashMap, HashMap, HashSet};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
use std::borrow::Cow;
@ -438,7 +435,7 @@ pub mod serde_bincode_compat {
pub struct TrieUpdates<'a> {
account_nodes: Cow<'a, HashMap<Nibbles, BranchNodeCompact>>,
removed_nodes: Cow<'a, HashSet<Nibbles>>,
storage_tries: HashMap<B256, StorageTrieUpdates<'a>>,
storage_tries: B256HashMap<StorageTrieUpdates<'a>>,
}
impl<'a> From<&'a super::TrieUpdates> for TrieUpdates<'a> {