Skip to content

execution/exec: execute read-ahead blocks to warm state - #26332

Closed
awskii wants to merge 17 commits into
mainfrom
awskii/readahead-exec-prefetch
Closed

awskii wants to merge 17 commits into
mainfrom
awskii/readahead-exec-prefetch

Conversation

@awskii

@awskii awskii commented Nov 15, 2026

Copy link
Copy Markdown
Member
  • Multi-block exec faults state pages in on demand: blocksReadAheadFunc warms only sender, to, code and access-list keys, and read-ahead never runs in the initial cycle.
  • READ_AHEAD_EXEC runs block N+500's txs on a read-only reader, results dropped, to warm what they touch; READ_AHEAD_EXEC_COMMITMENT also prefetches their commitment branches; READ_AHEAD_INITIAL starts read-ahead in the initial cycle. All default off.
  • Draft: a live node shared the box; a quiet rerun decides. Commitment prefetch shows no win.
arm ms/block CPU s
off 55.56 608.5
exec 51.2 (−6.0%, p .031) 723.3
exec + commitment 53.66 (−2.6%, p .094) 825.5

arb1-dev, mainnet archive from 26054550, ~3,300 blocks, cold cache, 6 ABBA rounds, medians, e76f2914ea0.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Speculative block states are never closed, draining resource pools, and the loading-based watermark can skip useful read-ahead before actual execution starts.

2 open issues
What changed in this PR

This PR adds opt-in speculative block execution to Erigon’s read-ahead pipeline, warming state pages before normal execution reaches them. Speculative results are discarded, and the new execution, commitment-prefetch, and initial-cycle options default to off.

Changes:

  • Adds speculative transaction execution, optional commitment prefetch, and warmup metrics.
  • Makes read-ahead distance and worker count configurable, with matching database reader capacity.
  • Adds tests for state isolation, cancellation, commitment keys, reader capacity, and initial-cycle execution.
File Description
node/​node.go Includes configured read-ahead workers in database reader capacity.
execution/​stagedsync/​read_ahead_initial_test.go Compares state roots and receipts with initial-cycle read-ahead enabled.
execution/​stagedsync/​exec3.go Wires read-ahead options and the block-progress watermark into staged execution.
execution/​exec/​historical_trace_worker.go Adapts historical tracing to the expanded read-ahead API.
execution/​exec/​blocks_read_ahead.go Implements speculative execution, key recording, metrics, and cancellation.
execution/​exec/​blocks_read_ahead_test.go Adds speculative execution and resource-handling regression tests.
execution/​exec/​bal_commitment_warmup.go Shares commitment-key generation and supports prefetch with an existing transaction.
execution/​exec/​bal_commitment_warmup_test.go Updates tests for explicit prefetch parallelism.
common/​dbg/​experiments.go Defines read-ahead experiment switches and tuning settings.
cmd/​utils/​flags.go Updates reader-capacity validation and diagnostics.
cmd/​rpcdaemon/​cli/​httpcfg/​http_cfg.go Sizes the reader limit using configurable read-ahead workers.
cmd/​rpcdaemon/​cli/​httpcfg/​http_cfg_test.go Tests reader limits with configurable and disabled worker pools.

Review effort: Balanced

Comment thread execution/exec/blocks_read_ahead.go Resolved
Comment thread execution/stagedsync/exec3.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Speculative execution failures can be recorded as successful warmups and prefetch incomplete write sets, while the tests rely on uncontrolled scheduling and stored header roots that cannot verify persisted-state equivalence.

1 open issue
2 previously missed issues

In code that hasn't changed since last review

Medium severity Mark failed speculative execution as errored and stop warmup

execution/​exec/​blocks_read_ahead.go:982

An ApplyMessage or FinalizeTx failure leaves Errored false, so the caller records this partial warmup as "done" and can prefetch an incomplete write set. State-read failures also need an explicit StateReadError check because they are not always returned by ApplyMessage. Check these errors, mark the speculative block as errored, and stop it without failing canonical execution.

Medium severity Compare persisted state instead of stored header roots

execution/​stagedsync/​read_ahead_initial_test.go:104

These roots come from m.Current(tx), which reads the stored block header, not the state produced by Sync.Run. Both runs insert the same generated headers, so this equality cannot detect a difference in persisted state. Please compare a root computed from the resulting state, or read and assert the sender nonce and contract storage through m.NewStateReader(tx) in both runs, and check that execution reached the expected head.

2 resolved since last review

Review effort: Balanced

…nit test, drop scheduling-dependent late assertion

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Read-ahead gives each transaction a fresh block-sized gas pool, allowing speculative work to exceed the cumulative block gas budget and compete with real execution for CPU.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Bound speculative execution by the block gas budget

execution/​exec/​blocks_read_ahead.go:981

Each transaction receives a fresh block-sized gas pool, so speculative execution has no cumulative block gas budget. On the older snapshot, calls that return cheaply in real execution can instead exhaust their gas limits, making read-ahead workers consume many times the block gas limit and compete with execution for CPU. Share a bounded gas pool across the loop and stop when the remaining budget cannot admit the next transaction.

1 resolved since last review

Review effort: Balanced

@AskAlexSharov

Copy link
Copy Markdown
Collaborator
  1. execution/exec/blocks_read_ahead.go L924: delete: the READ_AHEAD_EXEC_COMMITMENT arm. The body measures no win for it (−2.6% vs −6.0% for exec alone, and +102 CPU s). Delete the flag, readAheadCommitmentKeys, the written-key maps, readAheadPrefetchCommitment, the par parameter added through bal_commitment_warmup.go, the prefetchCommitmentWithTx split (L457), and the three TestReadAheadCommitment* tests. readAheadRecordingWriter then only counts writes, so state.NewNoopWriter() can be used directly.
  2. L715, L733: delete: StorageReadKeys. Only a test reads it, but every speculative SLOAD inserts into this map. Assert on StorageRead in the test instead.
  3. L703, L708: idiom: ReadAheadStorageKey and ReadAheadExecStats are exported, but only code in this package uses them. Make them unexported.
  4. L943: bug: recover() != nil discards the panic value. A real EVM bug on this path then shows up only as an error metric. Log the value with dbg.Stack() before returning.
  5. L967: speed: types.MakeSigner is called once per tx, but the signer depends only on the block. Create it once before the loop.
  6. blocks_read_ahead_test.go L1366, L1388, L1431: idiom: the TestReview* names record which review round added them. Remove the Review prefix.

@AskAlexSharov

Copy link
Copy Markdown
Collaborator
  1. execution/stagedsync/exec3.go L222: idiom the read-ahead comment (L222-224) now sits above pe :=, and var clean func() (L200) moved to function scope, but only the if at L242 uses it. Move the comment to L242 and declare clean inside the if, as before.
  2. execution/exec/blocks_read_ahead.go L964: shrink each tx checks the ctx twice (L964 and L974-976). One if ctx.Err() != nil { return } right after evm.Reset covers both cases: a cancel before the check returns, and a cancel after it reaches evm.Cancel through context.AfterFunc.
  3. execution/exec/blocks_read_ahead.go L930: shrink the map[string]int{...} literal allocates a map on every block only to loop over three counters. Write three AddInt calls, like L933-934 do.
  4. execution/exec/blocks_read_ahead_test.go L1329, L1349: idiom the helpers reviewReadAheadTx and reviewReadAheadFixture also carry the review-round name. Rename them too, for example readAheadStubTx and readAheadFixture.

@awskii

awskii commented Nov 20, 2026

Copy link
Copy Markdown
Member Author
  • Tip numbers for the same mechanism (a block's txs pre-executed on a read-only reader, results dropped). Taken on another branch with commitment v3, not on this PR's build.
  • At the tip it nets zero on newPayload: the commitment round and the time outside the exec loop get shorter, and the exec loop pays it back per Mgas.
arb1-dev mainnet tip, 154 pairs, 2026-11-19 pre-exec main's read-ahead paired change p
newPayload p50, ms 29 28 −1.7% .95
exec loop, ms per Mgas 0.779 0.737 −7.2% 5.2e-05
commitment round + wait, ms 2.704 3.25 +17.1% 5.5e-11
outside the exec loop, ms 5 6 +20.0% 1.2e-06
  • So the case for this PR is catch-up sync only: the −6.0% (p .031) in the body, taken on a shared box for +19% CPU (608.5 → 723.3 s).
  • It stays a draft until that table is rerun on a quiet box. If the rerun shows no win, it gets closed.

@awskii

awskii commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Closing: on fresh main the gain is small, costs about a fifth more CPU, and does not reach the tip.

arb1-dev, archive catch-up of 3,295 blocks from 26,054,550, cold page cache per run, 6 ABBA rounds wall CPU
read-ahead off 150.8 s 625.4 s
main's read-ahead (READ_AHEAD_INITIAL) 148.3 s
this PR (READ_AHEAD_EXEC) 144.8 s 767.8 s
  • This PR against main's read-ahead: −2.1% wall; against off: −3.7% wall and +22.6% CPU. Wilcoxon over 6 rounds, p .031 each, the floor for 6 pairs.
  • At the tip it reads 500 blocks ahead, which do not exist yet, so newPayload does not change.
  • The commitment-prefetch arm cannot run on main since execution/state: state.New builds a versioned state #26617, because state.New is versioned and FinalizeTx no longer calls the recording writer.

@awskii awskii closed this Nov 23, 2026
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.

3 participants