Skip to content

perf(runtime): skip per-clause state lookups in fork corrections via in-memory flags#41

Open
simonzg wants to merge 2 commits into
mainnetfrom
perf/fork-corrections-cache
Open

perf(runtime): skip per-clause state lookups in fork corrections via in-memory flags#41
simonzg wants to merge 2 commits into
mainnetfrom
perf/fork-corrections-cache

Conversation

@simonzg

@simonzg simonzg commented May 1, 2026

Copy link
Copy Markdown
Contributor

Why

Each EnforceTeslaForkN_Corrections function (forks 8–13) is called on every clause execution during block processing, not just once per block. Every call begins with a state trie read to check whether the correction has already been applied:

enforceFlag := builtin.Params.Native(rt.State()).Get(meter.KeyEnforceTesla_ForkN_Correction)

This state lookup runs on every single clause — forever — even though each correction fires exactly once in the node's entire lifetime. After the fork activation block, the correction is done and the flag is 1, but the trie read still happens on every clause of every subsequent block. With 6 fork correction functions (forks 8–13), that is 6 state trie reads wasted per clause execution across all future blocks on mainnet.

What Changed

Added six atomic uint32 package-level flags (fork8DoneFlagfork13DoneFlag). Each correction function now starts with:

if atomic.LoadUint32(&forkNDoneFlag) == 1 {
    return
}

When the correction successfully writes its on-chain flag, it also atomically sets the in-memory flag. From that point forward, the function returns in ~1 ns (a single atomic load) instead of doing a state trie read.

The in-memory flag is set in the same code path as the on-chain flag, so they are always consistent. On node restart, the in-memory flag starts at 0 — but the very first clause execution after the fork will re-check the state, find the flag already set, and set the in-memory flag, so only one state lookup happens on restart.

Expected Impact

  • Eliminates 6 state trie reads per clause execution on all blocks after fork13 activation.
  • At typical mainnet load (hundreds of clauses per block, blocks every 2 seconds), this removes millions of wasted state reads per day.
  • Particularly impactful for blocks with many transactions/clauses (DeFi activity), where the per-clause overhead is paid many times per block.

simonzg and others added 2 commits July 10, 2025 09:54
…n-memory flags

Each EnforceTeslaForkN_Corrections function is called on every clause
execution (not just every block). Each call starts with:

  enforceFlag := builtin.Params.Native(rt.State()).Get(meter.KeyEnforceTesla_ForkN_Correction)

This is a state trie read on every single clause — even though the correction
is a one-time operation that fires exactly once in the node's lifetime.
After the flag is set to 1 in state, the heavy work is gated out, but the
state lookup still runs on every clause indefinitely.

Add per-fork atomic uint32 in-memory flags (fork8DoneFlag through fork13DoneFlag).
Each function checks its flag first — if set, returns immediately without any
state access.  The flag is set atomically when the correction writes the
on-chain flag, so the two always agree.

This eliminates 6 state trie reads per clause execution after the latest fork
has activated — a direct reduction in per-block overhead that accumulates
across every transaction on mainnet going forward.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

1 participant