Skip to content

Optimistic state prefetch#4238

Open
bhartnett wants to merge 29 commits into
masterfrom
optimistic-state-prefetch
Open

Optimistic state prefetch#4238
bhartnett wants to merge 29 commits into
masterfrom
optimistic-state-prefetch

Conversation

@bhartnett
Copy link
Copy Markdown
Contributor

@bhartnett bhartnett commented May 14, 2026

Changes in this PR:

  • All lru caches in the database now use the thread safe ConcurrentLruCache. This is needed to support concurrent state lookups on the database.
  • Manual cleanup of caches using dispose.
  • Added optimistic state prefetching to speed up block execution by using background threads to execute the transaction out of order and in parallel. This has the effect of filling the caches before the main thread runs the block execution. One task is created for each transaction which does both the sender recovery and the prefetching. An atomic flag is used to signal to the main thread when the sender is available so that execution in the main thread can progress without waiting for the prefetching to complete.
  • Added a optimisticStatePrefetch debug parameter which is enabled by default (because when disabled the single threaded execution is a bit slower than previously due to the ConcurrentLruCache).
  • Updated the tests to enable the taskpool and set optimisticStatePrefetch = true.
  • Preload trusted setup to avoid race during block execution.

The benchmarking results for this look good so far:

Imported 20000 blocks starting at block 15 million with stateroot checks disabled using 16 threads:
master - elapsed=19m46s83ms
concurrentlru (without prefetch) - elapsed=23m11s610ms
optimistic-state-prefetch (with prefetch enabled) - elapsed=11m878ms781us

The ConcurrentLruCache does cause a slow down for the single threaded block import but when the prefetch is enabled in background threads to fill the caches we get a large speedup.

@bhartnett
Copy link
Copy Markdown
Contributor Author

bhartnett commented May 20, 2026

Imported 50000 blocks starting at block 24 million with stateroot checks disabled using 16 threads:
master - elapsed=1h29m44s
optimistic-state-prefetch (with prefetch enabled) - elapsed=1h4m50s

That's a ~28% improvement.

@bhartnett bhartnett marked this pull request as ready for review May 21, 2026 04:33
Comment thread execution_chain/nimbus_execution_client.nim Outdated
Comment thread tests/test_generalstate_json.nim Outdated
Comment thread tests/eest/eest_helpers.nim Outdated
@bhartnett
Copy link
Copy Markdown
Contributor Author

bhartnett commented May 21, 2026

Imported 500000 blocks starting at block 15 million with stateroot checks disabled using 16 threads:
master - elapsed=4h21m20s
optimistic-state-prefetch (with prefetch enabled) - elapsed=4h10m2s

In a longer run there is not as much improvement. I guess this is expected because once the caches are warmed up the prefetching doesn't have as much impact.

With that in mind, having better performance when the caches are empty will still be beneficial for various scenarios such as the benchmarking tests.

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.

2 participants