feat(metering): cache pending trie nodes for faster state root calculation #388
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Cache trie nodes computed during pending flashblock state root calculations and reuse them across bundle metering simulations. This optimization ensures that each bundle simulation only pays the I/O cost for its own state changes, not for all previous flashblock state changes.
Problem
Without caching, every bundle simulation must:
With many bundles being simulated per flashblock, this redundant I/O becomes a bottleneck.
Solution
Introduce
PendingTrieCachethat:TrieUpdatesandHashedPostStatefrom pending state root computationarc_swap::ArcSwapfor lock-free reads and atomic updatesensure_cached()for lazy computation and cachingTrieInputviaprepend_cached()Changes
PendingTrieCachewithensure_cached()method that computes and caches the pending trie input, or returns the cached entry if it matches the current flashblock.PendingTrieInputstruct containingTrieUpdatesandHashedPostState. Updatemeter_bundleto use cached trie input when available. UseTrieInput::prepend_cached()to prepend pending trie for incremental state root calculation.PendingTrieCacheto compute/retrieve cached trie data for bundle simulations.PendingTrieCacheandPendingTrieInput.arc-swapandreth-trie-commondependencies.Performance Impact
Before: Each bundle simulation reads trie nodes for all flashblock changes
After: Each bundle simulation only reads trie nodes for its own changes
The improvement scales with:
Test Plan
cargo +nightly clippypassescargo +nightly fmtpasses