Skip to content

perf: a run takes the runtime a clean run left, and a program is compiled once - #99

Open
hearnadam wants to merge 2 commits into
perf/tsfrom
perf/ts-runtime-reuse
Open

hearnadam wants to merge 2 commits into
perf/tsfrom
perf/ts-runtime-reuse

Conversation

@hearnadam

Copy link
Copy Markdown
Contributor

Stacked on #98. Takes the TypeScript implementation from about 1.1x to about 4x on script/corpus.ts against dev (1,918 → about 465 µs; about 730 µs for programs not seen before), and the fixed cost of a run from 80 µs to about 6.

What it does

  • A run takes the runtime a clean run left. Making several hundred built-ins was 78 of the 80 µs return 1 took. A run that ends on its own having changed no intrinsic now leaves its runtime for the next run of the same Runtime. That run gets its own host globals, bindings, global scope, console state, intrinsic names and VM; only the built-ins carry over. Up to four are kept per Runtime.
  • A program compiled before is not compiled again: the last 64, by source text.
  • No reference walk is set up for a primitive argument.

The thing to review: isolation

Reuse is only right if "changed no intrinsic" is detected completely. It rests on the dirty bit snapshots already keep on every intrinsic (Obj.intrinsic, flipped by changed), plus the array-iteration protector. A run leaves no runtime if it changed a built-in, failed, timed out, was aborted or was suspended; the round-trip test mode takes none.

test/reuse.test.ts runs 29 programs that try to leave something behind (properties on built-ins, on built-in functions and accessor getters, freezing, deleting, setPrototypeOf, the array iterator, console state, a run left waiting), each followed by a probe run on the same runtime. That is evidence, not a proof: a built-in that kept state outside its properties, where the dirty bit does not see it, would cross runs. I found none.

Costs

  • The first run of a Runtime still pays the full setup.
  • Up to four spare runtimes are held per Runtime, roughly 0.5 MB each.
  • The compile cache only helps for source run again; the corpus benchmark runs each program repeatedly, so it shows that best case.

Not reached

The programs' own execution is about 430 µs of the corpus and was 28% of it on dev, so about 4.5x is the ceiling without a faster dispatch loop.

bun test and bun run test:roundtrip pass. Numbers are wall-clock medians on a busy machine.

containsOpaqueReference and containsRuntimeReference made a set, a list and an
iterator to walk a value that, for most arguments, is a string or a number.
…iled once

Making a run's several hundred built-ins was 78 µs of the 80 µs return 1 took,
and 63% of script/corpus.ts. A run that ends on its own, having changed no
intrinsic (each is still as made, by the dirty bit snapshots already keep) and
left arrays iterable directly, now leaves its runtime for the next run of the
same Runtime (Interpreter.reusable, recycle). That run gets host globals,
bindings, global scope, console state, intrinsic names and a VM of its own;
only the built-ins carry over. Up to four are kept. A run that changed a
built-in, failed, timed out, was aborted or was suspended leaves none, and the
round-trip test mode takes none.

The last 64 programs compiled are kept by their text, so one run again is not
parsed and compiled again.

test/reuse.test.ts runs 29 programs that try to leave something behind
(properties on built-ins and their functions, freezing, deleting, the array
iterator, console state, a run left waiting), each followed by a probe run.

script/corpus.ts, median µs: setup 64 to 6; return 1 from 65 to 6; the corpus
1,750 to about 465 (about 730 for programs not seen before).

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant