From dec1cad318103683339022a4e35b8a3ac6b0d925 Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Tue, 10 Feb 2026 23:39:56 -0500 Subject: [PATCH] refactor(trie): merge SparseTrieExt into SparseTrie trait (#22035) Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Co-authored-by: Amp --- .../src/tree/payload_processor/sparse_trie.rs | 18 +++++++++--------- crates/trie/sparse/src/parallel.rs | 6 ++---- crates/trie/sparse/src/state.rs | 8 ++++---- crates/trie/sparse/src/traits.rs | 7 ------- crates/trie/sparse/src/trie.rs | 4 ++-- 5 files changed, 17 insertions(+), 26 deletions(-) diff --git a/crates/engine/tree/src/tree/payload_processor/sparse_trie.rs b/crates/engine/tree/src/tree/payload_processor/sparse_trie.rs index b79550e347..ef62e42cee 100644 --- a/crates/engine/tree/src/tree/payload_processor/sparse_trie.rs +++ b/crates/engine/tree/src/tree/payload_processor/sparse_trie.rs @@ -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), Cached(SparseTrieCacheTask), @@ -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 { 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 SparseTrieCacheTask 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( diff --git a/crates/trie/sparse/src/parallel.rs b/crates/trie/sparse/src/parallel.rs index f704f20efd..51f04cfd72 100644 --- a/crates/trie/sparse/src/parallel.rs +++ b/crates/trie/sparse/src/parallel.rs @@ -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, diff --git a/crates/trie/sparse/src/state.rs b/crates/trie/sparse/src/state.rs index 23a43e1f3a..4fbb3c2839 100644 --- a/crates/trie/sparse/src/state.rs +++ b/crates/trie/sparse/src/state.rs @@ -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 SparseStateTrie 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 { } #[cfg(feature = "std")] -impl StorageTries { +impl StorageTries { /// Prunes and evicts storage tries. /// /// Keeps the top `max_storage_tries` by a score combining size and heat. diff --git a/crates/trie/sparse/src/traits.rs b/crates/trie/sparse/src/traits.rs index 7508a325da..4b6844ce1b 100644 --- a/crates/trie/sparse/src/traits.rs +++ b/crates/trie/sparse/src/traits.rs @@ -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. /// diff --git a/crates/trie/sparse/src/trie.rs b/crates/trie/sparse/src/trie.rs index 9b018737f3..ab0f9c9b73 100644 --- a/crates/trie/sparse/src/trie.rs +++ b/crates/trie/sparse/src/trie.rs @@ -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 RevealableSparseTrie { } } -impl RevealableSparseTrie { +impl RevealableSparseTrie { /// Applies batch leaf updates to the sparse trie. /// /// For blind tries, all updates are kept in the map and proof targets are emitted