Skip to content

Conversation

@niran
Copy link
Contributor

@niran niran commented Jan 12, 2026

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:

  1. Compute the hashed post-state for all flashblock changes
  2. Read trie nodes from disk for all accounts/storage touched by flashblocks
  3. Then compute the bundle's incremental changes on top

With many bundles being simulated per flashblock, this redundant I/O becomes a bottleneck.

Solution

Introduce PendingTrieCache that:

  1. Caches TrieUpdates and HashedPostState from pending state root computation
  2. Uses arc_swap::ArcSwap for lock-free reads and atomic updates
  3. Provides ensure_cached() for lazy computation and caching
  4. Prepends cached data to bundle's TrieInput via prepend_cached()

Changes

  • metrics.rs: New module with metrics for cache performance tracking.
  • trie_cache.rs: New module implementing PendingTrieCache with ensure_cached() method that computes and caches the pending trie input, or returns the cached entry if it matches the current flashblock.
  • meter.rs: Add PendingTrieInput struct containing TrieUpdates and HashedPostState. Add shared compute_pending_trie_input() helper with metrics. Update meter_bundle to use cached trie input when available. Use TrieInput::prepend_cached() to prepend pending trie for incremental state root calculation.
  • rpc.rs: Integrate with PendingTrieCache to compute/retrieve cached trie data for bundle simulations.
  • lib.rs: Export PendingTrieCache and PendingTrieInput.
  • Cargo.toml: Add arc-swap, metrics, metrics-derive, and reth-trie-common dependencies.

Metrics

New Prometheus metrics for monitoring cache performance:

  • reth_metering_pending_trie_cache_hits - counter for cache hits
  • reth_metering_pending_trie_cache_misses - counter for cache misses (trie computed)
  • reth_metering_pending_trie_compute_duration - histogram of compute time in seconds

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:

  • Number of accounts/storage slots modified by flashblocks
  • Number of bundles simulated per flashblock

Test Plan

  • Existing metering tests pass (20 tests)
  • State root calculation produces correct results with cached data
  • cargo +nightly clippy passes
  • cargo +nightly fmt passes

@niran niran force-pushed the niran/meter-trie-cache branch from abeece0 to d96a3ce Compare January 12, 2026 20:03
@niran niran force-pushed the niran/meter-flashblocks-state-v2 branch from 994978c to 13ef63c Compare January 12, 2026 20:03
@danyalprout danyalprout added this to the v0.3.0 milestone Jan 12, 2026
@niran niran force-pushed the niran/meter-flashblocks-state-v2 branch from 13ef63c to 6817f24 Compare January 12, 2026 22:06
@niran niran force-pushed the niran/meter-trie-cache branch 2 times, most recently from d492e12 to afad0ab Compare January 13, 2026 00:58
@niran niran force-pushed the niran/meter-flashblocks-state-v2 branch 2 times, most recently from 8dadef0 to 724a0c9 Compare January 13, 2026 01:22
@niran niran force-pushed the niran/meter-trie-cache branch 2 times, most recently from 364f2ee to c9a7614 Compare January 13, 2026 01:55
@niran niran force-pushed the niran/meter-flashblocks-state-v2 branch 2 times, most recently from be1c7a5 to f1a198a Compare January 13, 2026 17:43
@niran niran force-pushed the niran/meter-trie-cache branch from c9a7614 to 63e8c33 Compare January 13, 2026 17:43
@niran niran force-pushed the niran/meter-flashblocks-state-v2 branch from f1a198a to 3e17305 Compare January 13, 2026 18:01
@niran niran force-pushed the niran/meter-trie-cache branch from 63e8c33 to 2867178 Compare January 13, 2026 18:01
@niran niran changed the title feat(metering): cache flashblock trie nodes for faster state root calculation feat(metering): cache pending trie nodes for faster state root calculation Jan 13, 2026
Base automatically changed from niran/meter-flashblocks-state-v2 to main January 13, 2026 18:44
@cb-heimdall
Copy link
Collaborator

cb-heimdall commented Jan 13, 2026

✅ Heimdall Review Status

Requirement Status More Info
Reviews 2/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 0
Sum 1

Copy link
Contributor

@refcell refcell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this needs a rebase, but otherwise, core triedb cache changes look solid

@niran niran force-pushed the niran/meter-trie-cache branch from 2867178 to 97374cc Compare January 13, 2026 19:09
danyalprout
danyalprout previously approved these changes Jan 13, 2026
@danyalprout danyalprout enabled auto-merge January 13, 2026 19:17
Add a single-entry cache for flashblock trie nodes to avoid redundant I/O
when metering multiple bundles against the same flashblock state.

Key changes:
- Add FlashblockTrieCache with ensure_cached() for lazy trie computation
- Add FlashblockTrieData containing TrieUpdates and HashedPostState
- Update meter_bundle to accept optional cached_flashblock_trie parameter
- Use TrieInput::prepend_cached() to prepend cached trie for state root
- Add arc-swap and reth-trie-common dependencies

When multiple bundles are metered against the same flashblock, the cache
ensures the flashblock's trie is computed only once. Each bundle's state
root calculation then measures only its own incremental I/O.
@danyalprout danyalprout enabled auto-merge January 13, 2026 19:20
@danyalprout danyalprout added this pull request to the merge queue Jan 13, 2026
Merged via the queue into main with commit c5cb254 Jan 13, 2026
29 checks passed
@danyalprout danyalprout deleted the niran/meter-trie-cache branch January 13, 2026 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants