Fast trine run (roadmap #43)
trine run app.tr picks the fastest way to get the answer now, without changing it:
| situation | what happens | typical start-to-finish |
|---|---|---|
| this exact program ran before | its cached native build runs directly, before any parsing | json.tr: 7 ms (was 47 ms interpreted) |
| new or edited program, short run | interpreted at once; a background process builds it natively for next time | json.tr: 59 ms (same as before) |
| new or edited program, long run | interpreted at once, then tiered up to the native build when it lands, mid-run | fib(35): 0.24 s (was 19 s interpreted) |
| this file's runs are known to be long | compiled first, then run natively |
--interp always interprets (the reference semantics). --native always compiles and waits. --vm runs the ownership-IR VM. Setting TRINE_NO_CACHE=1 turns the cache off, and TRINE_RUN_VERBOSE=1 reports which path a run took.
How it works
- The cache key covers the source bytes, the schedule file's bytes, the path as given (it appears in error messages) and the compiler build (size and modification time of the
trineexecutable). On a hit, nothing is parsed. The whole hit path is one SHA-256 over the file, astatcall and running the program. - The cache lives in the store, under
.trine/bin/(the 32 most recent builds) and.trine/runs/(per-file timings), so git never sees it (bet 4). Builds are written to a temporary file and renamed, so an entry is never half written. One lock per key keeps builds of the same program from running twice. - Tier-up by replay. Every few thousand interpreter steps (calls and loop iterations), the interpreter checks, at most once every 20 ms, whether the native build has landed. When it has, the interpreter stops. The native executable then runs from the start, and the first N bytes of its output are discarded, where N is how much the interpreter had already printed. This is sound because Trine programs are deterministic today: no input, clock or randomness, and stdout is the only effect. The native run therefore prints the same bytes, so the user sees one continuous, identical output.
- Errors look the same in every engine. Native runtime errors are re-rendered by
trinewith the source line and caret, exactly as the interpreter shows them. Exit codes match too. - Adaptive. If a file's last interpreted run took longer than its native build (with a 300 ms floor), a cache miss compiles first.
Evidence
fast_run_caches_native_buildschecks, in order: - a miss is interpreted and starts a background build; - the next run uses the cached build; - an edit is a miss again, never the stale build; - the output matches every time.fast_run_tiers_up_mid_runruns a program that prints, computes for a while, then hits a runtime error. Its first run tiers up partway through. Stdout, the error message (with caret) and the exit code are byte-identical to--interp.- Every golden program gives its expected output through the cached native path.
- The interpreter's tier-up checks cost nothing measurable:
fibinterprets in 353 ms against 356 ms in v0.15 on the same machine.
Honest notes
- Tier-up by replay depends on determinism. When I/O arrives (#21), a program that has read input or touched the outside world can't simply be replayed. It will tier up only while it is still effect-free, which its capabilities will show, or else stay in the interpreter until the next run.
- An edit-run loop on a big program still waits for the C compiler in the background, and a short run never benefits from that build. Large programs build slowly, because the generated C is about 12× the source size. Parallel or incremental C compilation by definition hash is the next speed-up.
- The cache is per directory (beside the source), like the rest of the store. Clones and fresh checkouts start cold.
- **
tools/bench.py's interpreter column now passes--interp.** It had silently started measuring the cache. The P1 baseline was re-saved, because this session runs on a different machine instance (CPython is about 1.6× slower here). The v0.15 binary built on this machine gives the same VM and interpreter times, so there is no code regression.
Update: the VM runs the first phase (P2a)
While the native build is compiled in the background, the program now runs on the ownership-IR VM instead of the tree-walking interpreter: 1.1-2.2x faster on the examples (particles 755 → 338 ms, raytrace 150 → 85 ms), with the same output, the same step counts and the same tier-up points. --interp still selects the tree walker, the reference semantics.