plum

#treesitter#compiler#wasm

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

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


653e893 — Peter John 2026-11-07T11:09:59+05:30
Simpler language: compare imperative and functional games
compiler/game/web-test/fixtures/stress_scene_port.plum ADDED
@@ -0,0 +1,132 @@
1
+ # The functional port of master's imperative stress_scene, drawing the same scene call for
2
+ # call: 5000 sprites in 50 slowly rotating groups, 500 rect shapes, 200 circle shapes and
3
+ # 50 texts with a changing number, through a 0.8 camera. The state is plain lists of plain
4
+ # structs; `update` returns the next state with `List.map` and spreads, and `view` rebuilds
5
+ # the scene tree every frame. Motion is per frame, as in the original. The text is rebuilt
6
+ # in `update` and input is polled every frame, also as in the original, so the
7
+ # update and draw split stays comparable. Written the plain way and not tuned for speed.
8
+ module stress_scene_port
9
+ import std/Str
10
+ import std/Int
11
+ import std/Float
12
+ import std/List
13
+ import std/game/Vec2
14
+ import std/game/Color
15
+ import std/game/gfx
16
+ import std/game/scene
17
+ import std/game/input
18
+ import std/Io
19
+
20
+ Sprites = 5000
21
+ Rects = 500
22
+ Circles = 200
23
+ Texts = 50
24
+ Groups = 50
25
+ WorldW = 1600.0
26
+ WorldH = 900.0
27
+
28
+ struct Dancer(x: Float, y: Float, rotation: Float, srcX: Float)
29
+
30
+ struct Spin(x: Float, y: Float, rotation: Float, dancers: List[Dancer])
31
+
32
+ struct Drift(x: Float, y: Float, color: Color)
33
+
34
+ struct Count(x: Float, y: Float, n: Int, text: Str)
35
+
36
+ struct State(sheet: Texture, spins: List[Spin], rects: List[Drift], circles: List[Drift], counts: List[Count])
37
+
38
+ fun hue(i: Int) -> Color =
39
+ Color(r: Float(i % 7) / 7.0, g: Float(i % 5) / 5.0, b: 0.4 + Float(i % 3) / 5.0, a: 0.8)
40
+
41
+ fun buildFrom[T](i: Int, n: Int, f: fn(Int) -> T) -> List[T] =
42
+ if i >= n then Nil else Cons(head: f(i), tail: buildFrom(i + 1, n, f))
43
+
44
+ # The list f(0), f(1) ... f(n - 1).
45
+ fun build[T](n: Int, f: fn(Int) -> T) -> List[T] =
46
+ buildFrom(0, n, f)
47
+
48
+ fun newDancer(gi: Int, j: Int) -> Dancer =
49
+ i := gi * {Sprites / Groups} + j
50
+ Dancer(x: Float({j * j * 13 + j * 37} % 150), y: Float({j * j * 29 + j * 53} % 170), rotation: Float(i), srcX: Float(i % 4 * 16))
51
+
52
+ fun newSpin(gi: Int) -> Spin =
53
+ Spin(x: Float(gi % 10 * 160), y: Float(gi / 10 * 180), rotation: 0.0, dancers: build(Sprites / Groups, |j| newDancer(gi, j)))
54
+
55
+ fun newState(sheet: Texture) -> State =
56
+ State(
57
+ sheet: sheet,
58
+ spins: build(Groups, |gi| newSpin(gi)),
59
+ rects: build(Rects, |i| Drift(x: Float(i * 61 % 1600), y: Float(i * 29 % 900), color: hue(i))),
60
+ circles: build(Circles, |i| Drift(x: Float(i * 83 % 1600), y: Float(i * 47 % 900), color: hue(i + 3))),
61
+ counts: build(Texts, |i| Count(x: Float(i * 30 % 1600), y: Float(i * 17 % 900), n: i, text: "n {i}")))
62
+
63
+ # --- update: every entity moves by a fixed step per frame ---
64
+
65
+ fun stepDancer(d: Dancer) -> Dancer =
66
+ r := d.rotation + 0.02
67
+ Dancer(..d, rotation: r, srcX: Float(Int(r * 50.0) / 6 % 4 * 16))
68
+
69
+ fun stepSpin(s: Spin) -> Spin =
70
+ Spin(..s, rotation: s.rotation + 0.004, dancers: s.dancers.map(|d| stepDancer(d)))
71
+
72
+ fun wrap(v: Float, limit: Float) -> Float =
73
+ if v > limit then v - limit else v
74
+
75
+ fun stepRect(d: Drift) -> Drift =
76
+ Drift(..d, x: wrap(d.x + 2.0, WorldW))
77
+
78
+ fun stepCircle(d: Drift) -> Drift =
79
+ Drift(..d, x: wrap(d.x + 3.0, WorldW), y: wrap(d.y + 1.0, WorldH))
80
+
81
+ fun stepCount(c: Count) -> Count =
82
+ n := c.n + 1
83
+ Count(..c, n: n, text: "n {n}")
84
+
85
+ fun step(s: State) -> State =
86
+ State(
87
+ ..s,
88
+ spins: s.spins.map(|g| stepSpin(g)),
89
+ rects: s.rects.map(|d| stepRect(d)),
90
+ circles: s.circles.map(|d| stepCircle(d)),
91
+ counts: s.counts.map(|c| stepCount(c)))
92
+
93
+ # --- view: the whole tree every frame ---
94
+
95
+ fun pivot(x: Float, y: Float, ox: Float, oy: Float, rad: Float) -> Transform =
96
+ Compose(first: Translate(dx: x + ox, dy: y + oy), then: Compose(first: Rotate(radians: rad), then: Translate(dx: 0.0 - ox, dy: 0.0 - oy)))
97
+
98
+ fun dancerView(sheet: Texture, d: Dancer) -> SceneNode[Int] =
99
+ Group(
100
+ transform: pivot(d.x, d.y, 8.0, 8.0, d.rotation),
101
+ children: [Sprite(tex: sheet, x: 0.0, y: 0.0, w: 16.0, h: 16.0, srcX: d.srcX, srcY: 0.0, srcW: 16.0, srcH: 16.0, tint: Color.White)])
102
+
103
+ fun spinView(sheet: Texture, g: Spin) -> SceneNode[Int] =
104
+ Group(transform: pivot(g.x, g.y, 75.0, 85.0, g.rotation), children: g.dancers.map(|d| dancerView(sheet, d)))
105
+
106
+ fun rectView(d: Drift) -> SceneNode[Int] =
107
+ RectShape(x: d.x, y: d.y, w: 12.0, h: 8.0, color: d.color)
108
+
109
+ fun circleView(d: Drift) -> SceneNode[Int] =
110
+ CircleShape(cx: d.x + 6.0, cy: d.y + 6.0, r: 6.0, color: d.color)
111
+
112
+ fun countView(c: Count) -> SceneNode[Int] =
113
+ Text(text: c.text, x: c.x, y: c.y, size: 14.0, color: Color.White)
114
+
115
+ fun view(s: State) -> SceneNode[Int] =
116
+ texts := s.counts.map(|c| countView(c))
117
+ circles := s.circles.map(|d| circleView(d)).append(texts)
118
+ rects := s.rects.map(|d| rectView(d)).append(circles)
119
+ Group(transform: Identity, children: s.spins.map(|g| spinView(s.sheet, g)).append(rects))
120
+
121
+ struct World(state: State, input: Input) =
122
+ fun update(self, io: Io, dt: Float) -> World =
123
+ World(state: step(self.state), input: self.input.poll(io))
124
+
125
+ fun draw(self, io: Io) =
126
+ gfx.clear(io, Color.rgb(0.05, 0.06, 0.1))
127
+ gfx.setCamera(io, 0.0, 0.0, 0.8)
128
+ view(self.state).draw(io)
129
+ gfx.resetCamera(io)
130
+
131
+ fun main(io: Io) -> World =
132
+ World(state: newState(gfx.loadTexture(io, "sprites.png")), input: Input.create())
docs/imperative-vs-functional.md ADDED
@@ -0,0 +1,76 @@
1
+ # Imperative against functional: what the numbers say
2
+
3
+ Plum's `master` writes games as objects that change themselves. The `simpler-language` branch removes inheritance, `return`, `while`, `break`, `continue` and local reassignment, and a game returns its next state each frame. This page compares the two on the same scenes. The tables and method are in [the benchmark doc](plans/2026-11-06-simpler-language.benchmark.md).
4
+
5
+ The short version: the functional games are slower, by about 0.05 to 0.16 ms per frame, on this benchmark. Both hold 60 fps with room to spare, since a frame has 16.7 ms and these use under 2 ms. The clearer cost is memory churn, which is up to 70 times higher in the world game. The in-place proof makes no measurable difference to frame time in these games.
6
+
7
+ ## Who wins each measure
8
+
9
+ | measure | winner | by how much | how sure |
10
+ |---|---|---|---|
11
+ | Scene game, CPU per frame | imperative | 1.6 against 1.8 ms, about 10% | Clear: same gap in 3 separate passes, and the scene was checked call for call |
12
+ | World game, CPU per frame | imperative | 1.3 against 1.4 ms, 4 to 11% | Direction seen in all 3 passes, size is not firm, and the run ranges overlap in two |
13
+ | Same state-and-view game on both compilers | neither | 1 to 2% | Noise: the language change alone costs nothing measurable |
14
+ | Memory churn, scene | imperative | about 0.5 to 0.8 against 2.6 MB per frame | Clear |
15
+ | Memory churn, world | imperative | 0.115 against 8.0 MB per frame, 70 times | Clear |
16
+ | GC time, scene | neither | 0.038 against 0.037 ms per frame | Same |
17
+ | GC time, world | imperative | 0.004 against 0.187 ms per frame | Clear |
18
+ | Longest GC pause | imperative | 0.28 against 0.82 ms (world), 1.30 in one repeat | Both are small against a 16.7 ms frame |
19
+ | Worst frame's work | no consistent winner | world: imperative 6.8, functional 2.6 ms; scene: 3.3 and 3.0 ms | A few samples, so only the fact that all are small is safe |
20
+ | Dropped frames | undecided | both sides show stalls, up to 317 ms on imperative and 233 ms on functional | Not program work, likely machine load |
21
+ | `.wasm` size | functional | 49 against 93 KB (scene), 36 against 69 KB (world) | Clear; the old scene library is no longer linked |
22
+ | Compile time | neither | 0.44-0.47 against 0.48-0.50 s (scene), 0.36 against 0.33 s (world) | Different programs, so small either way |
23
+ | Game source lines | imperative | 117 against 132 (scene), 80 against 89 (world) | Exact, but it reflects how these were written |
24
+ | `std` lines | branch | 9377 down to 7091, 24% fewer | Exact |
25
+ | Compiler lines | master | 52311 up to 54566, 4% more | Exact |
26
+
27
+ ## Why the functional games lose
28
+
29
+ **Scene game:** the whole gap is in drawing. Update costs the same, 0.06 ms on both. Each frame the functional game builds a full tree of scene nodes and then walks it, while the imperative game walks objects it already has. Draw takes about 0.17 ms longer, and this is also where most of the extra memory appears to come from, though the split between update and draw wasn't measured. GC time is the same on both.
30
+
31
+ **World game:** update is slower and draw is faster. Update goes from 0.57 to about 0.9 ms, and draw from 0.61 to about 0.35 ms. Update turns the 20000-sprite list into a slice to sort it and back into a list every frame, and allocates about 8 MB doing so. That brings GC to 0.19 ms per frame, which is more than the whole gap, so on these numbers, without it the functional world game would be level or ahead. GC was measured in a separate pass, so this is an estimate.
32
+
33
+ Those two world numbers come from a port with one known difference: the old game picks a different 500 walking sprites after each sort, and the port keeps the same 500. The camera, sprite counts and sorting match, but individual sprites end up in different places.
34
+
35
+ ## What stayed copying, and what it costs
36
+
37
+ `--explain-fusion` says none of the spreads in the two exact ports are done in place. The reason is mostly the same: a `List.map` element counts as possibly shared. `orb_run` gets 8 of 31 and `stress_state_view` 1 of 6.
38
+
39
+ Those copies cost too little to measure in these games:
40
+
41
+ - For the two ports, a `--no-fbip` build is byte for byte the same program, because the proof doesn't fire at all.
42
+ - For the two state-and-view games, turning the proof off changes frame time by 0.003 ms or less.
43
+ - `orb_run` wasn't timed in a browser, so nothing here covers it.
44
+
45
+ **If the proof covered `List.map` elements:** the compiler wasn't changed to try this, so this is a stand-in that writes the same fields through `mut` fields.
46
+
47
+ - Scene game: it wouldn't close the gap. In-place writing was no faster, and update takes only 0.06 ms in total while the gap of 0.16 ms is in draw.
48
+ - World game: the stand-in dropped to 1.09 ms, faster than the imperative game's 1.30 ms. That can't be credited to the proof, because adding `mut` also changes how the sprite struct is laid out, and only 500 of 20000 sprites are written each frame. The layout is the likelier cause, but this wasn't isolated.
49
+
50
+ So on this evidence, wider proof coverage wouldn't change the scene result, and its effect on the world game is unknown. How `Spr` is laid out and sorted looks worth testing first.
51
+
52
+ ## What the simpler language removed
53
+
54
+ - `std` lost 2286 lines (24%) and 3 files.
55
+ - Game `.wasm` files for these two games are about half the size.
56
+ - The compiler grew by 2255 lines of Rust (4%), and `src/` by 2193. The removals were outweighed by what was added. Modules, the in-place proof and the second compiled versions are the likely sources, but the growth wasn't broken down by feature. So the language got smaller and its compiler didn't.
57
+ - The sweep went from 961 tests in 209 files to 827 in 186. That count includes tests of removed features and tests merged or rewritten, so it doesn't measure simplicity.
58
+
59
+ ## Loops
60
+
61
+ - `for`, `while`, the closure fold, the struct-state loop and `findFirst` can't be told apart between master and the branch at this load.
62
+ - The `for` gap reported earlier as +28% read about +7% here, with one branch run of 105 ms left out. The noise is too large to confirm or rule out +28%.
63
+ - One clear loss: summing with `0.until(n).fold(..)`, the replacement for a local `total = total + i`, took 42 to 66 ms for 50 million passes against 13 to 14 ms on master. A hand-written tail-recursive fold with a closure takes 13 to 14 ms, so the cost is specific to `Range.fold`.
64
+
65
+ ## What the noise doesn't support
66
+
67
+ - The machine was busy throughout, with load 4 to 16, so these are not quiet-machine numbers.
68
+ - The world gap's size isn't firm. It ranged from +4% to +11%, so "about 10%" would overstate what's known.
69
+ - Differences under about 0.05 ms in one phase aren't meaningful, and that covers every `--no-fusion` result except the deep-list game. There, fusion saves about 0.1 ms of draw and half the memory churn.
70
+ - Nothing here says the functional style causes stutter. Frame stalls appeared on both sides, and no single program's work came near the frame budget.
71
+ - Nothing here says how a real GPU, a slower machine or Safari would behave. The render step ran in software, and all figures are CPU time on one Apple M3 Pro.
72
+ - These are stress scenes of a few thousand objects, and `orb_run`, the one real game, wasn't timed. They don't show how a typical game would feel.
73
+
74
+ ## The result in one paragraph
75
+
76
+ On CPU per frame, the imperative games win by about 10% on the scene game, and by a smaller, less certain margin on the world game. On memory churn they win by a wide margin. Those gaps are under 1% of a 60 fps frame on this machine. The language change itself costs nothing when the game's style stays the same, as the state-and-view game shows. What the branch gains is a smaller `std` and smaller games, with a larger compiler. The direction is the user's decision.
docs/plans/2026-11-06-simpler-language.benchmark.md CHANGED
@@ -129,3 +129,166 @@ Question: did rewriting std off `return`, `while` and local reassignment slow an
129
129
  Loops (50,000,000 iterations each, `--no-fusion` so `find` is timed at the right place): `for`, `while`, tail-recursive fold with a closure, struct-state loop, `findFirst` all take 12 to 13 ms in both compilers, with the same answers (1249999975000000 for the first four, 49999999 for `find`). Under the default build `find` reads 0 ms in both because fusion moves the call past the timer.
130
130
 
131
131
  Result: no probe is more than 20% slower. The worst are Map set/get (about +3% on the median, 150 against 145; one run at +12%) and Slice push and sort (about +4%, 160 against 154). Both are within a few ms and look like the extra function call per loop pass in the rewritten `Map` and `Slice.insertBefore`; not confirmed with `--explain-fusion`, since neither passes the 20% bar. Nothing else moved.
132
+
133
+ ## Final comparison (T14): master against the branch
134
+
135
+ Taken 2026-11-07, master's export at 33fe838 against the branch at a6cfb1c. Both compilers and both harnesses were rebuilt in release on Rust 1.99.1, and no older number in this file is used here. The measuring stopped early at the user's request, so the allocation split below is missing.
136
+
137
+ ### Method
138
+
139
+ - Runs: headless Chromium with software WebGPU, 60 fps vsync-capped, 8 s settle, 5 s measured, about 299 frames. Configurations alternate within a round and the order reverses every round.
140
+ - Harness: a scratch copy of `plum-web-test` (`.superlite/t14/harness`, git-ignored) that serves a site already built by each side's release `plum`, so `--no-fusion` can be measured too. It also injects an observer that records update, draw, render and JS heap for each frame.
141
+ - Control: the unmodified `plum-web-test` of each tree, 3 runs of 3 pairs. It agrees with the observer runs on every gap of 0.1 ms or more, so the observer doesn't explain any result.
142
+ - Load: 1 minute load average 4.4 to 16.4 throughout. The machine was never quiet, and old and new saw the same conditions only because they alternate.
143
+ - Timer: `performance.now()` has 0.1 ms resolution here, so single-frame values are coarse. Means over 299 frames are finer.
144
+
145
+ ### Scene match, as checked
146
+
147
+ A scene-check run wrapped the host imports and recorded every draw call of each frame, with its numeric arguments for frames 2 and 120.
148
+
149
+ | old | new | calls per frame | arguments |
150
+ |---|---|---|---|
151
+ | `stress_scene` | `stress_scene_port` (new in T14) | same: 5000 sprites, 500 rects, 200 circles, 50 texts, 1 camera | all 5753 calls identical at frames 2 and 120, same order, difference 0. Text strings can't be read at this hook and were not compared. |
152
+ | `stress_world` | `stress_world` | old 2024 to 2125 sprites, new 2021 to 2130; same on 42 of 322 frames, gap at most 15 | same camera path and y-sorted on both. Individual sprites differ, by design of the port: at frame 2, 1886 of 2005 distinct sprites match, and at frame 120 only 49. Old re-picks its 500 walkers after each sort, so small moves spread over many sprites; new keeps 500 fixed walkers. |
153
+ | `stress_state_view` | `stress_state_view` | same: 5900 sprites, 20 rects, 70 texts | counts only |
154
+ | `stress_state_view_deep` | `stress_state_view_deep` | same: 5700 sprites, 50 texts | counts only |
155
+ | `stress_pure_scene` | none | 5700 sprites, 50 texts | a different scene from `stress_scene`; shown for reference |
156
+
157
+ `stress_state_view` is not an exact port of `stress_scene`: it has 220 more entities, draws rects and circles as sprites, and never changes `srcX`. That is why `stress_scene_port` exists. It uses plain `List.map` and spreads and polls input each frame as the original does. It wasn't tuned, and its first measured version is the one reported.
158
+
159
+ ### Frame time (ms, median of 5 runs, range of the total in brackets)
160
+
161
+ Load 5.6 to 16.4. `--no-fbip` builds of `stress_scene_port` and `stress_world` are byte-identical to the default, so those rows are repeat measurements of the same program and show the noise directly.
162
+
163
+ | game | build | update | draw | render | total | heap growth MB/frame |
164
+ |---|---|---|---|---|---|---|
165
+ | scene | master `stress_scene` | 0.063 | 1.278 | 0.305 | 1.653 (1.583-1.727) | 0.51 |
166
+ | scene | new `stress_scene_port` | 0.064 | 1.459 | 0.309 | 1.831 (1.738-1.877) | 2.57 |
167
+ | scene | new `--no-fbip` (same bytes) | 0.062 | 1.454 | 0.292 | 1.809 (1.734-1.855) | 2.57 |
168
+ | scene | new `--no-fusion` | 0.061 | 1.458 | 0.298 | 1.817 (1.733-1.867) | 2.57 |
169
+ | scene, other style | master `stress_pure_scene` | 0.233 | 1.228 | 0.245 | 1.699 (1.623-1.731) | 1.30 |
170
+ | state and view | master `stress_state_view` | 0.061 | 1.554 | 0.250 | 1.870 (1.833-1.922) | 2.76 |
171
+ | state and view | new `stress_state_view` | 0.066 | 1.522 | 0.249 | 1.833 (1.799-1.865) | 2.75 |
172
+ | state and view | new `--no-fbip` | 0.059 | 1.510 | 0.252 | 1.834 (1.802-1.867) | 2.75 |
173
+ | state and view | new `--no-fusion` | 0.064 | 1.547 | 0.253 | 1.864 (1.791-1.977) | 2.75 |
174
+ | deep list | master `stress_state_view_deep` | 0.049 | 1.285 | 0.229 | 1.566 (1.487-1.592) | 1.12 |
175
+ | deep list | new `stress_state_view_deep` | 0.035 | 1.245 | 0.216 | 1.497 (1.475-1.589) | 1.12 |
176
+ | deep list | new `--no-fbip` | 0.036 | 1.248 | 0.220 | 1.500 (1.486-1.631) | 1.12 |
177
+ | deep list | new `--no-fusion` | 0.034 | 1.345 | 0.218 | 1.596 (1.568-1.725) | 2.37 |
178
+ | world | master `stress_world` | 0.575 | 0.618 | 0.126 | 1.328 (1.246-1.430) | 0.115 |
179
+ | world | new `stress_world` | 0.888 | 0.347 | 0.144 | 1.382 (1.361-1.447) | 8.02 |
180
+ | world | new `--no-fbip` (same bytes) | 0.879 | 0.351 | 0.151 | 1.381 (1.367-1.456) | 8.02 |
181
+ | world | new `--no-fusion` | 0.854 | 0.351 | 0.145 | 1.349 (1.327-1.395) | 8.25 |
182
+
183
+ Heap growth is the sum of the rises in `usedJSHeapSize` between frames. It is an estimate, and it reads low for allocation that a collection hides within the same frame. Master's `stress_scene` read 0.51 in this pass and 0.76 in another.
184
+
185
+ Total, old against new, in all three passes (update, draw and render means; control is unmodified harnesses, median of 3):
186
+
187
+ | pair | timing pass | second pass | control | gap |
188
+ |---|---|---|---|---|
189
+ | `stress_scene` to `stress_scene_port` | 1.653 to 1.831 | 1.610 to 1.764 | 1.581 to 1.741 | +10 to +11%, about +0.16 ms |
190
+ | `stress_world` to `stress_world` | 1.328 to 1.382 | 1.304 to 1.444 | 1.290 to 1.416 | +4 to +11%, +0.05 to +0.14 ms |
191
+ | `stress_state_view`, same fixture on both | 1.870 to 1.833 | not run | 1.764 to 1.744 | -1 to -2%, inside the noise |
192
+
193
+ ### Garbage collection and worst frames
194
+
195
+ GC from a separate pass with Chromium startup tracing, 3 runs each, counting `MinorGC` and `MajorGC` events in a window of about 5 s after the 8 s settle. The window is placed from the first page function, so it is approximate.
196
+
197
+ | build | minor GCs per 5 s | minor total ms per 5 s | worst minor ms | major GCs | GC ms per frame |
198
+ |---|---|---|---|---|---|
199
+ | master `stress_scene` | 73 | 11.5 | 0.28 | 0 | 0.038 |
200
+ | new `stress_scene_port` | 30 | 11.2 | 0.46 | 0 | 0.037 |
201
+ | master `stress_state_view` | 32 | 12.4 | 0.45 | 0 | 0.041 |
202
+ | new `stress_state_view` | 33 | 13.1 | 0.45 | 0 | 0.044 |
203
+ | master `stress_state_view_deep` | 19 | 7.7 | 0.84 | 1 | 0.030 |
204
+ | new `stress_state_view_deep` | 19 | 7.7 | 0.85 | 1 | 0.029 |
205
+ | new deep `--no-fusion` | 27 | 10.3 | 0.43 | 0 | 0.034 |
206
+ | master `stress_world` | 5 | 1.2 | 0.28 | 0 | 0.004 |
207
+ | new `stress_world` | 75 | 56.1 | 0.82 | 0 | 0.187 |
208
+ | new `stress_world --no-fbip` (same bytes) | 75 | 53.8 | 1.30 | 0 | 0.179 |
209
+
210
+ Worst frames in the timing pass:
211
+
212
+ | build | work p50 / p95 / p99 (ms) | worst work (ms) | longest gap between frames, 5 runs (ms) | late frames (over 20 ms), 5 runs |
213
+ |---|---|---|---|---|
214
+ | master `stress_scene` | 1.6 / 1.8 / 1.8 | 3.3 | 16.8, 16.8, 16.8, 50, 50 | 0, 0, 0, 3, 6 |
215
+ | new `stress_scene_port` | 1.8 / 1.9 / 2.0 | 3.0 | 16.8, 16.8, 16.8, 50, 16.8 | 0, 0, 0, 4, 0 |
216
+ | master `stress_world` | 1.3 / 1.5 / 1.6 | 6.8 | 16.8 in all 5 | 0 in all 5 |
217
+ | new `stress_world` | 1.4 / 1.5 / 1.6 | 2.6 | 16.8, 233, 16.8, 16.8, 16.8 | 0, 29, 0, 0, 0 |
218
+
219
+ The long gaps don't come from program work: the worst work in any new `stress_world` run was 2.6 ms, and they appear on both sides. In the second pass, master's `stress_scene` had a 317 ms gap with 29 late frames and master's `stress_world` had 32 late frames. They look like machine load, but this data can't tell whether GC ever adds to one.
220
+
221
+ ### What `--explain-fusion` says stayed copying
222
+
223
+ | game | spreads in place | why the others copy |
224
+ |---|---|---|
225
+ | `stress_scene_port` | 0 | `stepDancer` and `stepCount` get `List.map` elements (`` `h` is rebound by a pattern ``); `step` may get a shared `s` |
226
+ | `stress_world` | 0 | `walk` gets `List.map` elements; `step` may get a shared `s` |
227
+ | `stress_state_view` | 1 of 6 | the other 5 are in `uiTransition`, where a caller may share `ui`. Its `step` uses plain constructors, not spreads, so the count covers little of its work |
228
+ | `stress_state_view_deep` | 0 | it reports no spreads to prove |
229
+ | `orb_run` | 8 of 31 (coordinator's count from the T11-T13 review; this run saw 8 in place and 15 distinct lines not in place) | `Orb` and `Mine` updates as `List.map` elements, and state passed on without being written back |
230
+
231
+ Cost of those copies: `--no-fbip` can't show it for the two ports, because the bytes are identical. For `stress_state_view` and the deep list, default and `--no-fbip` differ by 0.003 ms or less, which is noise. `orb_run` was not timed in the browser.
232
+
233
+ ### What if the proof covered `List.map` elements?
234
+
235
+ The compiler wasn't changed. As a rough stand-in, scratch copies of the two ports write the same fields through `mut` fields (`.superlite/t14/whatif`). 5 alternating runs, load 8.4 to 14.2:
236
+
237
+ | game | build | update | draw | render | total | heap MB/frame |
238
+ |---|---|---|---|---|---|---|
239
+ | scene | master | 0.067 | 1.256 | 0.286 | 1.610 (1.595-1.700) | 0.76 |
240
+ | scene | new | 0.063 | 1.421 | 0.285 | 1.764 (1.709-1.845) | 2.57 |
241
+ | scene | new, elements written in place | 0.105 | 1.450 | 0.284 | 1.844 (1.763-1.984) | 2.18 |
242
+ | world | master | 0.570 | 0.614 | 0.124 | 1.304 (1.240-1.704) | 0.115 |
243
+ | world | new | 0.941 | 0.360 | 0.136 | 1.444 (1.383-1.460) | 8.02 |
244
+ | world | new, walkers written in place | 0.557 | 0.404 | 0.135 | 1.094 (1.029-1.110) | 3.81 |
245
+
246
+ - Scene: writing in place was no faster. Update rose from 0.063 to 0.105 ms, though the cause wasn't traced. Heap growth fell by only 15%, so most of it comes from something other than element copies. Update is 0.063 ms in all, so even a free update would leave about 0.1 ms of the 0.16 ms gap, which is in draw.
247
+ - World: this result does not measure the proof. Adding `mut` changes the layout of `Spr` from flat to boxed (`--explain-fusion`: `layout Spr: flat` against `layout Spr: boxed`). Only 500 of 20000 sprites are written each frame, yet heap growth halved. The gain probably comes from the layout rather than from avoiding copies, but that wasn't isolated. It does show that a large part of the world's cost can be removed without leaving the functional style, and that is worth testing separately.
248
+
249
+ ### Compile time, size, code, sweep
250
+
251
+ | item | master | branch |
252
+ |---|---|---|
253
+ | compile, scene game, 3 alternating runs | 0.47, 0.45, 0.44 s | 0.50, 0.49, 0.48 s |
254
+ | compile, `stress_world`, 3 alternating runs | 0.36, 0.36, 0.36 s | 0.33, 0.33, 0.33 s |
255
+ | `.wasm`, scene game | 93014 | 49182 (no-fusion 48065) |
256
+ | `.wasm`, `stress_world` | 69024 | 36152 (no-fusion 38714) |
257
+ | `.wasm`, `stress_state_view` | 62720 | 62543 |
258
+ | `.wasm`, `stress_state_view_deep` | 51765 | 44358 |
259
+ | fixture source lines, scene game | 117 | 132 |
260
+ | fixture source lines, `stress_world` (with its tests) | 80 | 89 |
261
+ | `std/` | 9377 lines, 40 files | 7091 lines, 37 files |
262
+ | `compiler/` Rust, all | 52311 | 54566 |
263
+ | `compiler/` Rust, under `src/` | 35811 | 38004 |
264
+ | core, checker, wasm-codegen `src/` | 29242 | 31684 |
265
+ | sweep `plum test std examples tests` (release) | 209/209 files, 961 tests, 0 failed | 186/186 files, 827 tests, 0 failed |
266
+
267
+ The scene compile times are for two different programs, as are the sizes. The sweep is the default build only here.
268
+
269
+ ### Loop probes
270
+
271
+ 50,000,000 iterations, `plum run --no-fusion`, release, 3 alternating runs, load about 5 to 9. Answers match on both sides. The first pass of the corrected master probe ran after the branch's, so the `for` and `while` rows of this table are not strictly alternated.
272
+
273
+ | probe | master (ms) | branch (ms) |
274
+ |---|---|---|
275
+ | `for`, `mut` field | 56, 96, 96 | 64, 60, 60 |
276
+ | `while`, `mut` field | 76, 78, 98 | 77, 74, 96 |
277
+ | tail-recursive fold with a closure | 14, 13, 13 | 14, 13, 14 |
278
+ | struct-state loop | 13, 14, 14 | 14, 14, 13 |
279
+ | `findFirst` | 14, 13, 13 | 14, 13, 13 |
280
+ | `for` with a local `total =` (master only, the form is removed) | 14, 13, 13 | not valid |
281
+ | `0.until(n).fold(0, ..)` (default build, the replacement for that form) | not available | 44, 66, 42 |
282
+
283
+ The `for` +28% gap, using the same probe that found it (`loops2.plum`, default build): master 56, 56, 55 ms; branch 105, 60, 60 ms. Dropping the 105 outlier leaves about +7%. `while` in the same probe: master 101, 75, 98; branch 102, 91, 98. Under this load the +28% is neither confirmed nor ruled out; the data does not settle it.
284
+
285
+ The local accumulator's replacement is a loss outside that noise: `Range.fold` took 42 to 66 ms where master's `for` with a local took 13 to 14 ms, about 3 to 5 times as long. The hand-written tail-recursive fold with a closure takes 13 to 14 ms, so the gap is specific to `Range.fold`.
286
+
287
+ ### Not measured
288
+
289
+ - A real GPU. Render is software WebGPU, and all the numbers are main-thread CPU.
290
+ - Allocation split between update and draw. The probe failed in all 24 runs (a JS proxy invariant), and the rerun was dropped to stop early.
291
+ - `orb_run` frame times, and any actual imperative version of it.
292
+ - A quiet machine, a slower machine, or WebKit.
293
+ - A proof that really covers `List.map` elements. The stand-in above is not that.
294
+ - Uncapped runs as a comparison. One uncapped trial showed render at 9.2 ms under software WebGPU, which would hide these gaps.