← Back to Pull requests

perf(l1): keep the memoize_hashes guard out of the recursive call #7101

Open
unknown opened this pull request with 271 lines diff across 3 files
View on GitHub
unknown unknown commented on Aug 04, 2026 15:04 UTC
Author

Motivation

NodeRef::memoize_hashes already skips subtries whose hash is memoized, and NodeRef::Hash references have nothing to memoize at all. The problem is where that check lives: Node::memoize_hashes visits all 16 children of a branch node, and because the body is recursive LLVM will not inline it — so every child costs a real function call even when the answer is immediately "nothing to do".

How lopsided that is, measured on a real mainnet block execution witness (block 25368371):

| child references | count | share |
|---|---|---|
| NodeRef::Node (embedded, must be hashed) | 1,273 | 7.3% |
| NodeRef::Hash (hash only, nothing to do) | 13,102 | 75.1% |
| empty slots | 3,086 | 17.6% |

So ~93% of the references a branch iterates over have no work behind them. Counting actual calls during a stateless execution of that block:

NodeRef::memoize_hashes entered   54,810 times
hash actually computed             2,094 times   -> 96.2% of calls were no-ops

Description

Split the guard into an #[inline] shell and move the recursive body into a separate function, so the overwhelmingly common no-op case becomes a load and a branch at the call site instead of a call.

Behaviour is unchanged — this is purely about where the code lives. cargo test -p ethrex-trie passes (61 tests), fmt and clippy are clean.

Effect

The win scales with how poorly the call overhead is amortised. The measurement above comes from the LambdaVM zkVM guest, where every executed RISC-V instruction is proven and there is no out-of-order core to hide the call:

| | cycles |
|---|---|
| before | 26,794,880 |
| after | 25,314,781 |
| | −1,480,099 (−5.52% of total block execution) |

memoize_hashes disappeared from the guest profile entirely (1,786,030 → 0 self cycles), and the trie went from 20.5% to 15.9% of the block.

Native gains will be considerably smaller — a modern core predicts and pipelines these calls well — but the change strictly removes work, so it should not regress anywhere. Happy to add a native benchmark if you'd like a number for that path before merging.

Diff: 271 lines (+203 / -68) across 3 files Reviewer: ElFantasma Review duration: 107957m
All Vouch review confidence checks passed
Residual risk 0.26 = Change Risk (0.46) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.46 · 271 lines diff across 3 files
0.46
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
44%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Shallow Review Depth shallow_review_depth
0.344
Diff Size diff_size
0.335
Real PR Conversation Timeline
1 comments
"Review active on #7101"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 44.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 46.5% (MEDIUM) · Higher = larger blast radius
46.5%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 44.7% · Lower = rushed or shallow review
44.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 25.7% · Safe to merge
25.7%