plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


559ac66 — Peter John 2026-11-16T23:17:18+05:30
U4h: run-time shared bit measured and dropped
docs/backlog.md CHANGED
@@ -28,6 +28,12 @@ The retained scene tree was replaced by master's screen view in functional form:
28
28
  - **Closure calls go through the table** (since U4a, 2026-11-16). A `fn(..)` value is a `ref null $Closure` (`struct {i32 table index}`), and a closure that captures values is its own final subtype with the captures as fields: one allocation (none for a closure that captures nothing: a global built at start), no cast at the call (`struct.get` then `call_indirect`), one cast to a final type in the body. Not `call_ref` on a typed funcref field: wasmtime 47 reads and writes a funcref inside a GC object through a host call (`get_interned_func_ref`), which made Orbit Runner's update 2-3x slower when tried, and a cast to a non-final type that the value only subtypes is a host call too (`is_subtype`), so a `fn` read out of a `Slice` (an `anyref` array) still pays one. Release, interleaved, min of 9 (the machine was loaded): orbit update 13.4 to 12.2 ms, Orb Run 40.7 to 38.2, `benchmarks/closures/calls` 179 to 165; orbit paint, Clockwork, Balloon Pop, `cons`, `cons_rec` within noise (their closures are inlined by closure specialisation or not hot). Left: the table bounds and signature check of `call_indirect`, and the env cast in each capturing body.
29
29
  - **A value struct inside an enum payload is boxed twice** (`Some(Pos)` allocates two objects). No measurement yet.
30
30
  - **In-place updates still miss** (recounted 2026-11-16 after U4c, `--explain-fusion`, each "not in place" line by its innermost reason): Orb Run 36 in place, 39 not (10 a value still used after the update, all the time travel's `rewindTo`/`undo`/`redo` from a fold; 8 bound to a shared value; 6 rebound by a pattern, `match self.history / Some(h)` and `Shift.after`; 5 a shared value; 3 taken out of `List.head`; 2 fields out of declaration order; 2 a receiver read again; 1 each other). Orbit Runner 31 in place of 96, Clockwork 26 of 88, Menu 20 of 86, Balloon Pop 23 of 86; the biggest reason in the four games is a receiver kept or read again after a method call (19-22, mostly the time travel and `Looping`). No closure reason is left (Balloon Pop's 5 "an argument is passed from inside a closure" went with U4c: its fold's closures are copies now), but the counts are the same as before U4c: each fix moved a spread's reason one step down. U4c added three proofs, each with a test in `uniqueness_fold_test.rs`: a field moved in one arm of a whole-body `match` (`moveRegions`: `Steer` in `PlayingScreen.update` is now a move of `self.ship`), a variable handed over through an inline `if` (`sized := if c then self.resized(p) else self`), and a field read into a local that is only read (`aliasReads`: `ship := self.ship` then `ship.body.pos`); and a pattern binder handed to a read-only parameter counts as a read. What blocks the hot ones now: `PlayingScreen.ship` is shared because the view holds the ship (`if !self.over then self.ship`), so `Ship.steer`/`step`/`hurtFor` copy; `List.head` is shared because `List.find` returns the head and `List.next` pairs it (a field's uniqueness is one fact for every element type), so the actors' steps copy; `Game.run`'s `sized` needs `Game.resized` to return its `self` in place, which `Game.remake` (shared) blocks. The Elm-style `update` regression (Orbit Runner's update 7-10% slower after the Elm screens) was not found in instructions retired here (`screen_views/orbit.plum update` 283M before U4c, 280M after).
31
+ - **A run-time shared bit** (U4h, 2026-11-16, `sl/shared-flag`: measured, not built, dropped): a one-way bit on each GC struct, set at every copy of a reference, would let a spread the static analysis leaves a copy update in place when the bit is clear and its variable is dead. Most of the copies it aims at are of values that are shared at run time: `Delivery.deliverTo` makes a shallow `@copy` of the screen each tick, and the old screen stays in `Game.screens` until the tick ends, so the screen's ship, its lists and every actor in them have two holders while the tick's spreads run (an exact reference count would copy them too). So the actors' steps (`Orb`/`Mine`/`Ring`/`Body.step`, Clockwork's `Walker`/`Orbit`/`Spinner`, `scale.plum`'s `ViewDot`) stay copies. What the bit could reach is the game's own chain (`Game.run`'s `ticked` and last spread, `Game.step`'s `Game(..ran, ..)`; `base := Game(..sized, ..)` reads `sized` after it, so not that one) and a value spread again after its first copy in the tick (`Ship.step` after `Ship.steer`, the ship's `Body.step`). Measured with release builds, instructions retired by the whole process (wasmtime's compile included), min of 3:
32
+ - Spreads run per tick (a debug counter per type): Orbit Runner update copies 7.8 `Body`, 3 `Game`, 2.9 `Orb`, 2.9 `Mine`, 2 `Ship`, and writes 3 `PlayingScreen`, 2 `Trail` and 1 `Game` in place; Clockwork copies 77 `Body`, 60 `Walker`, 9 `Spinner`, 8 `Orbit`, 3 `Game`; `scale.plum` copies 11,000 `ViewDot`s a tick (`update view`); Balloon Pop copies 6 `Balloon`, 6 `Body`, 3 `Game`, 2 `Viewport`, 1 `PlaygroundScreen`. `map`, `particles` and `cons` run no record spread.
33
+ - What the bit could reach, every such spread forced in place (`Game.run`, `Game.step`, `Ship.step`; same checksums): Orbit Runner update 188.7M to 178.8M (-5.3%; about 1.7M more for the ship's `Body.step` by estimate), Orb Run 541.0M to 527.7M (-2.5%), Balloon Pop 4,722M to 4,689M (-0.7%), Clockwork update 404.9M to 403.9M (-0.25%), `scale.plum` nothing.
34
+ - The header alone (an `i32` first in every struct, made with each one; no sets, no checks): Orbit Runner update +3.0M (+1.6%), paint +2.5M (+0.3%), Orb Run +6.4M (+1.2%), Balloon Pop +55M (+1.2%), Clockwork +3.2M (+0.8%), `scale.plum` +1.6M (+0.3%), `map`/`particles`/`cons` within noise. The sets at each copy (a `struct.set` behind a null test at each read of a field that is kept, each pattern binder of a value still live, each argument used again, each capture) and the checks come on top.
35
+ - So the bit loses on every benchmark but Orbit Runner, where the gain is the `Game` chain, which the static analysis could prove instead (`sized`/`after`, above in "In-place updates still miss").
36
+ - What would pay: the screen taken out of `Game.screens` for the tick (and put back) in place of the `@copy`, so it has one holder. Forced in place, the actors' spreads are worth Orbit Runner update -16% (all of its hot spreads: 188.7M to about 158M), Clockwork -10.5% (404.9M to 362.5M), `scale.plum` update -27% (477.6M to 347.8M). With one holder the static analysis still blocks most of them (`List.head` shared through `List.find`, the ship held by the view), and a run-time bit would follow those per value; that is when to try it again.
31
37
  - **Paint** (U4b, 2026-11-16, `sl/paint-direct`): each kind of view has its own copy of the draw's step (`View.paintAt`, `paintCulledAt`, `@final`), so an item costs one trait call in place of four; the walks recurse over the list with no closure (`List.each` with a capturing closure cost about 15% of a paint), skip the walk under an empty view, and an unturned `Place.under`, `Affine.scaleX`/`scaleY`/`rotation`, `Views.through` and a `Left` label skip the matrix product, `hypot`, `atan2` and the text width. `PLUM_NULL_GFX`'s stubs are raw host functions (a typed `Func::new` loaded every argument as a `Val`: a quarter of a timed paint). Instructions retired by the paint (paint run minus update run): Orbit Runner 2,659M to 1,087M, Clockwork (`benchmarks/screen_views/clockwork.plum`, new) 1,394M to 692M, `scale.plum` 7,750M to 2,740M; about a third of each drop is the null host. Open: wasmtime's `compiler_inlining` stays off (inlined frames drop out of a crash's `in f (called from g)`); Plum's own inliner (U4e, below) takes about half of what it took; the view's list and each item's own list are no longer made to be drawn (U4g); `List.each`/`map` with a capturing closure is still slow everywhere else (`Views.stepIn`, `drawnIn`).
32
38
  - **Small functions inlined when they cannot trap** (U4e, 2026-11-16, `optimize::inlineSmall`): a function of at most 100 bytes of wasm code (400 when one call is all that names it) is copied into each direct caller, locals and branches included (a block of the callee's type, its locals renumbered, `return` a branch), when nothing in it can trap: no division or remainder (by a constant other than 0 and -1 is fine), no array access or computed-length allocation (`at`, an index), no `crash`, `assert` or host range check (`unreachable`; an exhaustive match's or a trait call's impossible fall-through is written `nop; unreachable` and does not count), no indirect call, and every function it calls the same; not recursive, no loop (a loop copied into `main` ran 10% more instructions on `loop`), and not a call on the way to a crash (its message's `Str` copied into `Slice.at` cost `array` 2.8%). So no trap ever needs an inlined name. Instructions retired, one run of each (in-process, `(REPS=6 - REPS=1) / 5`, min of 3): Orbit Runner update 199M to 164M (-18%), paint 1,285M to 1,104M (-14%); Clockwork update 550M to 503M (-9%), paint 1,235M to 1,103M (-11%); `scale.plum` update 632M to 557M (-12%), paint 3,354M to 2,776M (-17%); `map` -7%, `trig` -9%, `str` -3%, `particles` -3%, `cons`/`fib`/`loop`/`array` unchanged; the run.py drivers Orb Run 742M to 646M (-13%), Balloon Pop 7,261M to 6,765M (-7%); checksums unchanged. Cost: Orbit Runner's .wasm 84,313 to 98,146 bytes (+16%; Clockwork +12%, scale +14%), `plum compile` +2% instructions (6.40G to 6.53G), `plum build` (with Cranelift) +2.5% (7.84G to 8.03G). Left (wasmtime's inliner took 27% of paint): everything that reaches a trap stays a call: `Place.under` (through `Math.cos`'s reduction tables), the `Gfx.*` wrappers (their `Int`-to-`i32` range check), `List.at`, the kinds' `paintAt` (172 bytes); `Views.blendOf` on a kind that is never a `Blend` is copied but still tests (the argument goes through a local of the callee's parameter type, so the static type that would fold the `ref.test` is lost).
33
39
  - **Closures inlined by size** (U4c, 2026-11-16): a callee of at most 64 expression nodes (`closure_spec::SMALL_CALLEE`; std's closure-taking fns are 3-59, `List.map` 3, `fold` 8, `Range.fold` 33, `Slice.mergeRun` 59, the biggest in the repository 68) gets a copy for every closure handed to it; a bigger one keeps a cap of 8 (`MAX_BIG_COPIES`) per callee, in the order its calls are met, so which of its calls are inlined still depends on the rest of the program. Copies made inside copies stop at 4 levels (`MAX_DEPTH`), and 256 copies of one small callee is a safety net. The escape analysis now resolves `x.m(..)` by the checker's receiver type (`FnCall.recv`, stamped before closure specialisation): `List.all` had never been specialised, as `t.all(keep)` resolved to every `all` and `Set.all` captures its closure. Cost: Orbit Runner compiles in 2.7% more instructions (8,722M to 8,957M), its .wasm is 352 bytes bigger (112,515 to 112,867). Run time within 1% in instructions retired on every benchmark (Orb Run 839M to 845M, Orbit Runner update 283M to 280M, Clockwork update 639M to 643M, `closures/calls` 3,084M to 3,083M; whole process, wasmtime's compile included).
docs/plans/2026-11-13-no-globals-tests-ports.md CHANGED
@@ -57,5 +57,5 @@ Decisions (user's answers):
57
57
  - **U4e a Plum inliner** for small functions that can't trap (no `crash`, checked `at`, division): `inlineLeaves` only takes <=20-byte bodies with no locals; wasmtime's inlining bound the gain at 20-25% of update and paint; keep crash traces' names.
58
58
  - **U4f `Nil`/`None` as null** and `br_on_cast` for matches (6 ref.test/ref.cast per element in `mapCells`; no `Some` allocation for an `Option` of a reference).
59
59
  - **U4g view lists fused into the paint walk or scalar-replaced** (~55 cells per Orbit frame, `__upcastOnto*` ~4%, an actor's one-item `[Sprite]` 14% of scale paint).
60
- - **U4h run-time shared flag** (user, 2026-11-16: "Yes do it after", after U4g): every GC struct gets a one-way shared bit, set wherever a reference is copied (a value used again after a use, a store into a list/field/closure capture/global/snapshot, anything the host holds), never cleared. A spread the static analysis can't prove unique checks the bit at run time and updates in place when clear. Soundness first: a missed copy point corrupts a shared value, so the copy points are found by the same liveness the in-place pass uses and fuzzed against the copying build (`--no-fbip` gives the same results). Measure: in-place counts and update time; the prize is about 5% of Orbit update, so drop it if the bit's cost eats it.
60
+ - **U4h run-time shared flag** (user, 2026-11-16: "Yes do it after", after U4g): every GC struct gets a one-way shared bit, set wherever a reference is copied (a value used again after a use, a store into a list/field/closure capture/global/snapshot, anything the host holds), never cleared. A spread the static analysis can't prove unique checks the bit at run time and updates in place when clear. Soundness first: a missed copy point corrupts a shared value, so the copy points are found by the same liveness the in-place pass uses and fuzzed against the copying build (`--no-fbip` gives the same results). Measure: in-place counts and update time; the prize is about 5% of Orbit update, so drop it if the bit's cost eats it. Dropped on 2026-11-16 after measuring, not built (`sl/shared-flag`): the copies it could reach are fewer than the copies its header costs; see backlog's "A run-time shared bit".
61
61
  - **U5 field layout** (user, 2026-11-16: the grouped form, "With commas after all fields" "except the last one"): `struct PlayingScreen[ScreenSlice<PlayAction>](` / ` dice: Dice, ticks: Int, clock: Float,` / ` score: Int, best: Int, lives: Int, over: Bool,` / ` ship: Ship, orbs: List<Orb>, mines: List<Mine>, rings: List<Ring>,` / ` assets: Assets` / `) =`. This already parses (no grammar change); the formatter keeps the user's line grouping, and games and std types with many fields move to grouped lines; docs show the form. Starts when U4a or U4b finishes.