perf(runtime): skip per-clause state lookups in fork corrections via in-memory flags#41
Open
simonzg wants to merge 2 commits into
Open
perf(runtime): skip per-clause state lookups in fork corrections via in-memory flags#41simonzg wants to merge 2 commits into
simonzg wants to merge 2 commits into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Why
Each
EnforceTeslaForkN_Correctionsfunction (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: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
uint32package-level flags (fork8DoneFlag…fork13DoneFlag). Each correction function now starts with: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