Skip to content

Research: a cost model for LepusNG — what the interpreter charges for, so emission strategy becomes calculable #196

Open

Description

@Huxpro

What this is

Every emission decision in the #163 train so far has been settled by buying a measurement window: emit unrolled straight-line code, then A/B it when a number looks wrong (C7/C8's append order; #194 Q2's scaling curve). That works, but it prices one decision per window and predicts nothing. This issue builds the thing that makes the next ten decisions calculable: a measured cost model of the LepusNG interpreter — what a JIT-less VM actually charges for, per primitive.

#194 Q2 (code-staging vs data-staging at 1k/10k/30k) is one slice of this model. This issue generalizes it. Report, records, and a small derived table — no production code.

M1 — The primitive cost table

Microbenchmarks on device, each isolating one primitive the emitted code is built from, with per-op cost derived from a scaling series (not a single point):

  • Dispatch & calls: empty-loop iteration cost; function call overhead (0/3/8 args); call through a local binding vs a property load (f() vs o.f()); the cost of crossing into a host/PAPI function vs a Lepus-defined one.
  • Property & environment access: local read vs closure read vs global read; monomorphic property load/store; array index read/write; how cost changes with object shape count at the site (1/2/8 shapes).
  • Allocation & strings: object literal, array literal, closure creation; string concat vs template; the cost of a string that crosses to a host call vs one that stays in Lepus.
  • Branching: if chains vs switch vs computed dispatch (table of functions) at 4/16/64 arms — this directly prices the update wire's slot dispatch and Future: compiled main-thread programs inside native list rows — and whether the list API itself is the right surface #193's shape-class dispatch.
  • Each measured at n≥5 with the Device measurement round 1: the four questions the web windows cannot answer (#163 clause 4) #194 protocol (DevTool off, AB/BA, load control), reported as ns/op with the fit and residual, plus the same suite run once on V8 (web) so every row has a "JIT would have hidden this" column.

M2 — Validate the model against the decisions we already paid for

A model that cannot postdict the windows we already bought is not a model:

  • Recompute, from M1's table alone, the predicted script self-time of the emitted program at 1k/10k/30k, and compare against Device measurement round 1: the four questions the web windows cannot answer (#163 clause 4) #194 Q2's measured curve.
  • Predict the cost delta of the C8 append-order variants from the model's primitives (it should predict ~0 for script — the C7 finding was flush-side — and saying so is the test).
  • Target: within 25% on absolute, correct ordering on every pair. Report where it misses; the misses are the interesting part.

M3 — The derived emission guide

The deliverable that outlives the windows: a short table mapping emitter choices to model-predicted costs, each row marked calculated / validated / refuted:

Protocol and scope

Device measurement per #194's protocol section; check records into benchmarks/lynx-table/stages/results/ one file per window; name the LepusNG/engine version in every record — the model is version-stamped, and re-validation on engine upgrades is expected, not a failure. Web (V8) columns are context only and never spendable on device decisions. Ordering: M1 dispatch+property rows first — they dominate every downstream prediction; strings and branching after; M2 before any M3 row is marked validated.

Activity

  1. Huxpro commented on Aug 26, 2026

    @Huxpro
    OwnerAuthor

    LepusNG cost-model report — Aries 10 / LepusNG 2.3.0

    Implementation and checked-in evidence: draft PR #207. The full report and derived JSON model carry the engine stamp and SHA-256 provenance for every input.

    Verdict

    M1 completed, but M2 closed the validation gate. The frozen 1k Q2 emitted-program prediction was 7.757 ms; the protocol-valid device median was 150 ms, a 94.83% relative absolute error against the ≤25% target. The ordering prediction was correct (program faster than interpreted template), but the absolute model is refuted. Therefore no M3 row is marked validated.

    Q2 rows Frozen program prediction Program actual Template actual Result
    1,000 7.757 ms 150 ms median; samples 147,150,162,146,162 338 ms median; samples 341,338,337,339,332 measured; order correct; 94.83% error
    10,000 77.565 ms — — invalid: per-call observer perturbed the workload and timed out
    30,000 232.695 ms — — invalid: crossing-surrogate pilot timed out
    C8 0 ms script delta — — calculated-only; no completed device window

    M1 primitive rows

    All device rows use n=5 AB/BA paired deltas at four iteration counts. Positive is candidate minus control.

    • dispatch: empty loop 33.22 ns/op; calls 0/3/8 args 103.34 / 146.78 / 226.47 ns/op
    • access: global read 22.47; property load/store 31.03 / 18.16; array load/store 33.22 / 18.26 ns/op
    • resolution-limited: method-vs-local +0.14; host-PAPI-vs-call +5.63; closure read +1.15; 2-shape -1.29; 8-shape +0.14 ns/op
    • allocation/string: object 332.24; array 305.18; closure 1058.16; template-vs-concat +94.33; string crossing +989.46 ns/op
    • branch candidate-vs-if at 4/16/64: switch +15.83 / +5.63 / -12.63; table -8.90 / -133.43 / -599.34 ns/op

    V8 was recorded as context-only; its host globals are JS mocks and none of its values feed device decisions or M2.

    Boundary and attribution

    The valid 1k program window executes the real emitted program against an M1-matched crossing surrogate: numeric factory/append calls use getUniqueId, string calls use setAttribute on one detached sentinel, and no per-call clock is read. The template boundary ends after outer renderPlanNode and before paint. Both arms log and then throw a terminal sentinel, excluding paint/flush.

    Parse/eval is not observable with DevTool disabled because native evaluates bytecode before an in-bundle clock can start; launch wall mixes native load/layout and is not substituted. The frozen inventory also assumed 20 whole-cell crossings/row, while the emitted row-shaped program exposed 15.121 surrogate crossings/row and 5,041 program nodes at 1k. I did not revise the frozen prediction after seeing this miss.

    M3 guide (gate closed)

    Guide Evidence
    absolute emitted-program model refuted
    whole-tree unrolling refuted
    row-shape function + tight loop calculated
    slot dispatch calculated
    string construction/crossing calculated
    property hoisting calculated
    C8 append order calculated

    Protocol notes: DevTool disabled; n=5; AB/BA; full samples and medians retained; pre/post load, thermal, and battery recorded; each window has its own file; invalid attempts are non-spendable and never aggregated. Device stamp: aries_10, Android 10, arm64-v8a; LepusNG 2.3.0; app-bundle engine 3.9; Lynx SDK 4.0.

    Validation: all 5 targeted Node test files pass, all harness files pass node --check, targeted Prettier passes, pnpm sync passes, and all frozen/report hashes plus the M2→M3 gate were reverified. Full pnpm test could not start because dependency preparation exhausted host disk space (ENOSPC); no test assertion failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions