refactor(trie): merge SparseTrieExt into SparseTrie trait (#22035)

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Georgios Konstantopoulos
2026-02-10 23:39:56 -05:00
committed by GitHub
parent 165b94c3fa
commit dec1cad318
5 changed files with 17 additions and 26 deletions

View File

@@ -28,7 +28,7 @@ use reth_trie_parallel::{
use reth_trie_sparse::{
errors::{SparseStateTrieResult, SparseTrieErrorKind, SparseTrieResult},
provider::{TrieNodeProvider, TrieNodeProviderFactory},
DeferredDrops, LeafUpdate, ParallelSparseTrie, SparseStateTrie, SparseTrie, SparseTrieExt,
DeferredDrops, LeafUpdate, ParallelSparseTrie, SparseStateTrie, SparseTrie,
};
use revm_primitives::{hash_map::Entry, B256Map};
use smallvec::SmallVec;
@@ -44,8 +44,8 @@ where
BPF: TrieNodeProviderFactory + Send + Sync,
BPF::AccountNodeProvider: TrieNodeProvider + Send + Sync,
BPF::StorageNodeProvider: TrieNodeProvider + Send + Sync,
A: SparseTrie + SparseTrieExt + Send + Sync + Default,
S: SparseTrie + SparseTrieExt + Send + Sync + Default + Clone,
A: SparseTrie + Send + Sync + Default,
S: SparseTrie + Send + Sync + Default + Clone,
{
Cleared(SparseTrieTask<BPF, A, S>),
Cached(SparseTrieCacheTask<A, S>),
@@ -56,8 +56,8 @@ where
BPF: TrieNodeProviderFactory + Send + Sync + Clone,
BPF::AccountNodeProvider: TrieNodeProvider + Send + Sync,
BPF::StorageNodeProvider: TrieNodeProvider + Send + Sync,
A: SparseTrie + SparseTrieExt + Send + Sync + Default,
S: SparseTrie + SparseTrieExt + Send + Sync + Default + Clone,
A: SparseTrie + Send + Sync + Default,
S: SparseTrie + Send + Sync + Default + Clone,
{
pub(super) fn run(&mut self) -> Result<StateRootComputeOutcome, ParallelStateRootError> {
match self {
@@ -117,8 +117,8 @@ where
BPF: TrieNodeProviderFactory + Send + Sync + Clone,
BPF::AccountNodeProvider: TrieNodeProvider + Send + Sync,
BPF::StorageNodeProvider: TrieNodeProvider + Send + Sync,
A: SparseTrie + SparseTrieExt + Send + Sync + Default,
S: SparseTrie + SparseTrieExt + Send + Sync + Default + Clone,
A: SparseTrie + Send + Sync + Default,
S: SparseTrie + Send + Sync + Default + Clone,
{
/// Creates a new sparse trie task with the given trie.
pub(super) const fn new(
@@ -277,8 +277,8 @@ pub(super) struct SparseTrieCacheTask<A = ParallelSparseTrie, S = ParallelSparse
impl<A, S> SparseTrieCacheTask<A, S>
where
A: SparseTrieExt + Default,
S: SparseTrieExt + Default + Clone,
A: SparseTrie + Default,
S: SparseTrie + Default + Clone,
{
/// Creates a new sparse trie, pre-populating with an existing [`SparseStateTrie`].
pub(super) fn new_with_trie(

View File

@@ -2,7 +2,7 @@ use crate::{
lower::LowerSparseSubtrie,
provider::{RevealedNode, TrieNodeProvider},
LeafLookup, LeafLookupError, RlpNodeStackItem, SparseNode, SparseNodeType, SparseTrie,
SparseTrieExt, SparseTrieUpdates,
SparseTrieUpdates,
};
use alloc::{borrow::Cow, boxed::Box, vec, vec::Vec};
use alloy_primitives::{
@@ -1146,9 +1146,7 @@ impl SparseTrie for ParallelSparseTrie {
subtrie.shrink_values_to(size_per_subtrie);
}
}
}
impl SparseTrieExt for ParallelSparseTrie {
/// O(1) size hint based on total node count (including hash stubs).
fn size_hint(&self) -> usize {
let upper_count = self.upper_subtrie.nodes.len();
@@ -3693,7 +3691,7 @@ mod tests {
use crate::{
parallel::ChangedSubtrie,
provider::{DefaultTrieNodeProvider, RevealedNode, TrieNodeProvider},
LeafLookup, LeafLookupError, SparseNode, SparseTrie, SparseTrieExt, SparseTrieUpdates,
LeafLookup, LeafLookupError, SparseNode, SparseTrie, SparseTrieUpdates,
};
use alloy_primitives::{
b256, hex,

View File

@@ -1,6 +1,6 @@
use crate::{
provider::{TrieNodeProvider, TrieNodeProviderFactory},
traits::{SparseTrie as SparseTrieTrait, SparseTrieExt},
traits::SparseTrie as SparseTrieTrait,
ParallelSparseTrie, RevealableSparseTrie,
};
use alloc::{collections::VecDeque, vec::Vec};
@@ -1100,8 +1100,8 @@ where
impl<A, S> SparseStateTrie<A, S>
where
A: SparseTrieTrait + SparseTrieExt + Default,
S: SparseTrieTrait + SparseTrieExt + Default + Clone,
A: SparseTrieTrait + Default,
S: SparseTrieTrait + Default + Clone,
{
/// Clears all trie data while preserving allocations for reuse.
///
@@ -1197,7 +1197,7 @@ struct StorageTries<S = ParallelSparseTrie> {
}
#[cfg(feature = "std")]
impl<S: SparseTrieTrait + SparseTrieExt> StorageTries<S> {
impl<S: SparseTrieTrait> StorageTries<S> {
/// Prunes and evicts storage tries.
///
/// Keeps the top `max_storage_tries` by a score combining size and heat.

View File

@@ -278,14 +278,7 @@ pub trait SparseTrie: Sized + Debug + Send + Sync {
/// Shrink the capacity of the sparse trie's value storage to the given size.
/// This will reduce memory usage if the current capacity is higher than the given size.
fn shrink_values_to(&mut self, size: usize);
}
/// Extension trait for sparse tries that support pruning.
///
/// This trait provides the `prune` method for sparse trie implementations that support
/// converting nodes beyond a certain depth into hash stubs. This is useful for reducing
/// memory usage when caching tries across payload validations.
pub trait SparseTrieExt: SparseTrie {
/// Returns a cheap O(1) size hint for the trie representing the count of revealed
/// (non-Hash) nodes.
///

View File

@@ -1,6 +1,6 @@
use crate::{
provider::TrieNodeProvider, LeafUpdate, ParallelSparseTrie, SparseTrie as SparseTrieTrait,
SparseTrieExt, SparseTrieUpdates,
SparseTrieUpdates,
};
use alloc::{boxed::Box, vec::Vec};
use alloy_primitives::{map::B256Map, B256};
@@ -258,7 +258,7 @@ impl<T: SparseTrieTrait> RevealableSparseTrie<T> {
}
}
impl<T: SparseTrieExt + Default> RevealableSparseTrie<T> {
impl<T: SparseTrieTrait + Default> RevealableSparseTrie<T> {
/// Applies batch leaf updates to the sparse trie.
///
/// For blind tries, all updates are kept in the map and proof targets are emitted