You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
engine: make graphical-function lookups faster in the VM #602
Graphical-function lookups were about 4% of the C-LEARN run in an early profile, and this issue collected ideas to make them cheaper.
Most of them are now done or measured as not worth it.
Status of each idea
Binary search over the x points: vm::lookup already does this.
The float comparison on the hot path: float::approx_eq is now a small inline function, not a library call.
Remember the last segment per table and check it first: implemented and reverted. It saved about 0.5% of the run, was wrong for tables whose x values are not sorted (which no import path rejects, engine: unordered or NaN graphical-function x values are not rejected #715), and the sound version made the run 7% slower by perturbing code generation of the VM loop.
Compute the segment directly for evenly spaced x values: implemented, not landed. C-LEARN instructions fell 0.6%, but WORLD3, whose tables are small, got 7.7% worse inside lookup.
Follow-up idea from VM perf round 2 (2026-06-03): per-GF last-segment memo
Adding this here rather than as a new issue since it belongs to this issue's "hoist invariant per-table work" / large-table-search scope. (docs/design/engine-performance.md already lists "Lookup last-segment memo (#602)" under its unprioritized run-side swings.)
Observation. C-LEARN has 161 graphical functions (36,814 points, ~229 avg) that are mostly year-indexed tables evaluated at slowly-advancing TIME (dt = 0.25 against a year-indexed x-axis). Consecutive lookups therefore land in the same x-segment ~75%+ of the time.
Idea. Keep a per-GF "last segment index" memo — e.g. a Vec<u32> hint in the Vm, one entry per GF id, correctness-independent (a wrong hint just falls back to the full search). On a lookup:
Check whether x still falls in [breakpoints[hint], breakpoints[hint+1]] (and the adjacent segment for the common forward step).
On a hit, interpolate directly and skip the ~8-iteration binary search entirely.
On a miss, run the normal search and update the hint.
Expected impact. ~780k lookups per C-LEARN run; the lookup path is ~2.2% of the run (after this issue's other suggested optimizations / round-2 measurements). With a ~75%+ same-segment hit rate the memo would eliminate most of the binary-search iterations on the hot path. Bit-preserving — the memo only changes how the segment is located, not the interpolation result.
Note. This stacks with (and is independent of) the binary-search-vs-linear-scan suggestion already in this issue: the memo short-circuits the search on hits; binary search bounds the cost on misses for large tables.
Data point: the last-segment memo was implemented, adversarially reviewed, and reverted on branch engine-vm-perf (commits a411b5a / efd7425, reverted in 83f3c60). Measurements on an Apple M-series (Asahi) host against the ~137 ms C-LEARN run:
Gross win of the memo alone: ~0.7 ms (0.5%). The ~8-deep binary search over the 229-point tables is already well-predicted at a slowly-advancing TIME index, so there was little to save.
Every sound formulation (sentinel checked inside lookup_with_hint, or hoisted into the dispatch arm) measured a consistent +9..15 ms regression via interleaved worktree A/B: one extra branch + call edge in eval_bytecode's Lookup arm perturbs the giant function's codegen (instructions +1.5%, cycles +6.4%, IPC 4.34 -> 4.13, branch count down).
Net: 0.5% gross win, ~7% soundness cost on this microarchitecture -- the memo idea is a measured dead end here (recorded in docs/design/engine-performance.md as negative result #2). The remaining ideas on this issue (approx_eq on the hot path, hoisting invariant per-table work) are unaffected by this data; a future attempt on a BTB-limited core (the Ryzen profile) might see different economics, but should budget for the dispatch-arm codegen sensitivity.
changed the title [-]engine: optimize the graphical-function Lookup hot path (~4% of the C-LEARN run)[/-][+]engine: make graphical-function lookups faster in the VM[/+]on Oct 12, 2026
lookup already binary-searches, approx_eq is now a cheap inline function, and the last-segment memo and uniform-grid index were both implemented and measured as net losses (engine-performance.md negative results #2, #4).
Activity
bpowers commentedon Jun 4, 2026
Follow-up idea from VM perf round 2 (2026-06-03): per-GF last-segment memo
Adding this here rather than as a new issue since it belongs to this issue's "hoist invariant per-table work" / large-table-search scope. (
docs/design/engine-performance.mdalready lists "Lookup last-segment memo (#602)" under its unprioritized run-side swings.)Observation. C-LEARN has 161 graphical functions (36,814 points, ~229 avg) that are mostly year-indexed tables evaluated at slowly-advancing
TIME(dt = 0.25against a year-indexed x-axis). Consecutive lookups therefore land in the same x-segment ~75%+ of the time.Idea. Keep a per-GF "last segment index" memo — e.g. a
Vec<u32>hint in theVm, one entry per GF id, correctness-independent (a wrong hint just falls back to the full search). On a lookup:xstill falls in[breakpoints[hint], breakpoints[hint+1]](and the adjacent segment for the common forward step).Expected impact. ~780k lookups per C-LEARN run; the lookup path is ~2.2% of the run (after this issue's other suggested optimizations / round-2 measurements). With a ~75%+ same-segment hit rate the memo would eliminate most of the binary-search iterations on the hot path. Bit-preserving — the memo only changes how the segment is located, not the interpolation result.
Note. This stacks with (and is independent of) the binary-search-vs-linear-scan suggestion already in this issue: the memo short-circuits the search on hits; binary search bounds the cost on misses for large tables.
bpowers commentedon Jun 4, 2026
Data point: the last-segment memo was implemented, adversarially reviewed, and reverted on branch engine-vm-perf (commits a411b5a / efd7425, reverted in 83f3c60). Measurements on an Apple M-series (Asahi) host against the ~137 ms C-LEARN run:
lookupon unsorted-x tables, which are reachable (no import path validates x ordering -- engine: unordered or NaN graphical-function x values are not rejected #715). Soundness therefore requires a per-table gate.lookup_with_hint, or hoisted into the dispatch arm) measured a consistent +9..15 ms regression via interleaved worktree A/B: one extra branch + call edge ineval_bytecode'sLookuparm perturbs the giant function's codegen (instructions +1.5%, cycles +6.4%, IPC 4.34 -> 4.13, branch count down).Net: 0.5% gross win, ~7% soundness cost on this microarchitecture -- the memo idea is a measured dead end here (recorded in docs/design/engine-performance.md as negative result #2). The remaining ideas on this issue (approx_eq on the hot path, hoisting invariant per-table work) are unaffected by this data; a future attempt on a BTB-limited core (the Ryzen profile) might see different economics, but should budget for the dispatch-arm codegen sensitivity.
[-]engine: optimize the graphical-function Lookup hot path (~4% of the C-LEARN run)[/-][+]engine: make graphical-function lookups faster in the VM[/+]bpowers commentedon Oct 12, 2026
Closing as won't fix.
lookup already binary-searches, approx_eq is now a cheap inline function, and the last-segment memo and uniform-grid index were both implemented and measured as net losses (engine-performance.md negative results #2, #4).