plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
67d593b
— Peter John
2026-11-09T02:36:52+05:30
Scene trees: benchmark the branch's persistent tree against master's three
benchmarks/scene_trees/game_enum_tree.plum
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import std/Int
|
|
2
|
+
import std/Float
|
|
3
|
+
import std/Bool
|
|
4
|
+
import std/Str
|
|
5
|
+
import std/Io
|
|
6
|
+
import std/os
|
|
7
|
+
import std/Option
|
|
8
|
+
import std/Result
|
|
9
|
+
import std/List
|
|
10
|
+
import std/Slice
|
|
11
|
+
import std/game/Vec2
|
|
12
|
+
import std/game/Affine
|
|
13
|
+
import std/game/Color
|
|
14
|
+
import std/game_enum_tree/ETree
|
|
15
|
+
import std/game_enum_tree/EBody
|
|
16
|
+
import std/game_enum_tree/ECtx
|
|
17
|
+
import std/game_enum_tree/EStage
|
|
18
|
+
|
|
19
|
+
# Scene-tree benchmark, branch version: master's benchmarks/scene_trees/game_enum_tree.plum
|
|
20
|
+
# (a persistent tree of immutable bodies, referred to by id) in the branch's language, on
|
|
21
|
+
# the branch's std/game_enum_tree. It builds the same scene from the same random numbers as
|
|
22
|
+
# master's three programs and prints the same checksums; see
|
|
23
|
+
# docs/benchmarks/2026-11-08-scene-trees-branch.md and run.py.
|
|
24
|
+
#
|
|
25
|
+
# The branch's programs get no command-line arguments, so the workload and N come from a
|
|
26
|
+
# file, `scene_trees.args` in the current folder ("<workload> <N>"); without it, it runs
|
|
27
|
+
# workloads 1 to 4 at N = 100. The clock is the branch's, whole ms.
|
|
28
|
+
#
|
|
29
|
+
# The scene: a root, 10 groups (moved, scaled and turned), and N - 11 leaves, 20 by 20
|
|
30
|
+
# rectangles, spread over the groups in turn. Workloads (the numbers are master's):
|
|
31
|
+
# 1 step: 1000 ticks; every leaf moves by its velocity and bounces in its group's box.
|
|
32
|
+
# 2 churn: 1000 ticks; 1% of the leaves (at least one) die and as many are born, picked
|
|
33
|
+
# and placed by the same random numbers, pushed before a step that applies them
|
|
34
|
+
# in its walk.
|
|
35
|
+
# 3 draw: 1000 flattens of the tree to the draw list, with transforms.
|
|
36
|
+
# 4 hit: 1000 hit tests at random stage points.
|
|
37
|
+
# 5 step, no transforms: the walk hands every body the root's context, so no parent's
|
|
38
|
+
# world transform is computed.
|
|
39
|
+
# 6 step, eager transforms: every body's world transform is computed each tick.
|
|
40
|
+
# 7 churn, apply after: the kills and spawns are pushed after the step and applied with
|
|
41
|
+
# a second walk (`EStage.apply`).
|
|
42
|
+
# 8 step, 60 kept: 1 with the last 60 stages kept (a ring of snapshots), so nothing is
|
|
43
|
+
# only held once and no cell can be reused.
|
|
44
|
+
# 9 churn, 60 kept: 2 with the last 60 stages kept.
|
|
45
|
+
|
|
46
|
+
struct Rng(mut s: Int) =
|
|
47
|
+
fun next(self, k: Int) -> Int =
|
|
48
|
+
self.s = {self.s * 1103515245 + 12345} % 2147483648
|
|
49
|
+
{self.s / 65536} % k
|
|
50
|
+
|
|
51
|
+
fun float(self, k: Int) -> Float = Float(self.next(k * 10)) / 10.0
|
|
52
|
+
|
|
53
|
+
# One struct for the scene's two kinds of body: the groups (`moves` false: a step returns
|
|
54
|
+
# them as they are) and the leaves (`moves` true: they move by (vx, vy) and bounce). Its
|
|
55
|
+
# fields are master's `Leaf`: `EBody`'s and the velocity.
|
|
56
|
+
struct Bod(
|
|
57
|
+
id: Int,
|
|
58
|
+
x: Float, y: Float, width: Float, height: Float,
|
|
59
|
+
originX: Float, originY: Float,
|
|
60
|
+
scaleX: Float, scaleY: Float,
|
|
61
|
+
rotation: Float,
|
|
62
|
+
color: Color,
|
|
63
|
+
visible: Bool,
|
|
64
|
+
dead: Bool,
|
|
65
|
+
vx: Float, vy: Float,
|
|
66
|
+
moves: Bool) =
|
|
67
|
+
|
|
68
|
+
fun key(self) -> Int = self.id
|
|
69
|
+
|
|
70
|
+
fun gone(self) -> Bool = self.dead
|
|
71
|
+
|
|
72
|
+
fun shown(self) -> Bool = self.visible && !self.dead
|
|
73
|
+
|
|
74
|
+
fun opacity(self) -> Float = self.color.a
|
|
75
|
+
|
|
76
|
+
fun local(self) -> Affine =
|
|
77
|
+
ebody.localOf(self.x, self.y, self.originX, self.originY, self.scaleX, self.scaleY, self.rotation)
|
|
78
|
+
|
|
79
|
+
fun hit(self, p: Vec2) -> Bool = ebody.hitBox(self.width, self.height, p)
|
|
80
|
+
|
|
81
|
+
fun paint(self, io: Io, m: Affine, alpha: Float) =
|
|
82
|
+
if self.width > 0.0
|
|
83
|
+
ebody.paintBox(io, m, alpha, self.width, self.height, self.color)
|
|
84
|
+
|
|
85
|
+
fun step(self, ctx: ECtx[Bod], dt: Float) -> Bod =
|
|
86
|
+
if !self.moves
|
|
87
|
+
self
|
|
88
|
+
else
|
|
89
|
+
nx := self.x + self.vx * dt
|
|
90
|
+
ny := self.y + self.vy * dt
|
|
91
|
+
vx := if nx < 0.0 || nx > 200.0 then 0.0 - self.vx else self.vx
|
|
92
|
+
vy := if ny < 0.0 || ny > 200.0 then 0.0 - self.vy else self.vy
|
|
93
|
+
Bod(..self, x: nx, y: ny, vx: vx, vy: vy)
|
|
94
|
+
|
|
95
|
+
fun group(id: Int, x: Float, y: Float, s: Float, r: Float) -> Bod =
|
|
96
|
+
Bod(
|
|
97
|
+
id: id, x: x, y: y, width: 0.0, height: 0.0, originX: 0.0, originY: 0.0,
|
|
98
|
+
scaleX: s, scaleY: s, rotation: r, color: Color.White, visible: true, dead: false,
|
|
99
|
+
vx: 0.0, vy: 0.0, moves: false)
|
|
100
|
+
|
|
101
|
+
fun newLeaf(rng: Rng, id: Int) -> ETree[Bod] =
|
|
102
|
+
x := rng.float(200)
|
|
103
|
+
y := rng.float(200)
|
|
104
|
+
vx := rng.float(100) - 50.0
|
|
105
|
+
vy := rng.float(100) - 50.0
|
|
106
|
+
etree.leaf(Bod(
|
|
107
|
+
id: id, x: x, y: y, width: 20.0, height: 20.0, originX: 0.0, originY: 0.0,
|
|
108
|
+
scaleX: 1.0, scaleY: 1.0, rotation: 0.0, color: Color.Red, visible: true, dead: false,
|
|
109
|
+
vx: vx, vy: vy, moves: true))
|
|
110
|
+
|
|
111
|
+
# What the workloads change besides the stage: the live ids, the random numbers, the next
|
|
112
|
+
# id to give out.
|
|
113
|
+
struct Bench(live: Slice[Int], rng: Rng, mut nextId: Int)
|
|
114
|
+
|
|
115
|
+
fun bench(n: Int) -> Bench = Bench(live: Slice[Int](), rng: Rng(s: 12345), nextId: n)
|
|
116
|
+
|
|
117
|
+
# The scene, built bottom up as master builds it: each group's children first, then the
|
|
118
|
+
# groups under the root. Ids: the root 0, the groups 1 to 10, the leaves from 11.
|
|
119
|
+
fun build(n: Int, sc: Bench) -> EStage[Bod] =
|
|
120
|
+
kids := Slice[Slice[ETree[Bod]]]()
|
|
121
|
+
for g := range 10
|
|
122
|
+
kids.push(Slice[ETree[Bod]]())
|
|
123
|
+
for i := range n - 11
|
|
124
|
+
kids.at(i % 10).push(newLeaf(sc.rng, 11 + i))
|
|
125
|
+
sc.live.push(11 + i)
|
|
126
|
+
groups := Slice[ETree[Bod]]()
|
|
127
|
+
for g := range 10
|
|
128
|
+
gb := group(g + 1, Float(100 + 300 * {g % 5}), Float(100 + 300 * {g / 5}), 1.0 + 0.05 * Float(g), 0.1 * Float(g))
|
|
129
|
+
empty: ETree[Bod] := EEmpty
|
|
130
|
+
groups.push(ENode(value: gb, firstChild: etree.etreeChainFrom(kids.at(g), 0), nextSibling: empty))
|
|
131
|
+
empty: ETree[Bod] := EEmpty
|
|
132
|
+
root := ENode(value: group(0, 0.0, 0.0, 1.0, 0.0), firstChild: etree.etreeChainFrom(groups, 0), nextSibling: empty)
|
|
133
|
+
EStage(root: root, camX: 0.0, camY: 0.0, camScale: 1.0)
|
|
134
|
+
|
|
135
|
+
# Every node's position, in preorder, folded into one number.
|
|
136
|
+
fun hashTree(t: ETree[Bod], h: Int) -> Int =
|
|
137
|
+
t.fold(h, |r0, b|
|
|
138
|
+
r := {r0 * 31 + Int(b.x * 1000.0)} % 1000000007
|
|
139
|
+
{r * 31 + Int(b.y * 1000.0)} % 1000000007
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
# `t` ticks of `stage.step`.
|
|
143
|
+
fun steps(stage: EStage[Bod], ctx: ECtx[Bod], dt: Float, t: Int) -> EStage[Bod] =
|
|
144
|
+
if t == 0 then stage else steps(stage.step(ctx, dt), ctx, dt, t - 1)
|
|
145
|
+
|
|
146
|
+
# `steps` with each tick's stage also stored in `kept`, a ring of the last 60.
|
|
147
|
+
fun stepsKept(stage: EStage[Bod], ctx: ECtx[Bod], dt: Float, t: Int, kept: Slice[EStage[Bod]]) -> EStage[Bod] =
|
|
148
|
+
if t == 0
|
|
149
|
+
stage
|
|
150
|
+
else
|
|
151
|
+
keep(kept, stage, t)
|
|
152
|
+
stepsKept(stage.step(ctx, dt), ctx, dt, t - 1, kept)
|
|
153
|
+
|
|
154
|
+
fun keep(kept: Slice[EStage[Bod]], stage: EStage[Bod], t: Int) =
|
|
155
|
+
if kept.length() < 60
|
|
156
|
+
kept.push(stage)
|
|
157
|
+
else
|
|
158
|
+
_ := kept.set(t % 60, stage)
|
|
159
|
+
|
|
160
|
+
# Pushes the tick's kills and spawns to `ctx.out`: `k` random live leaves die and `k` new
|
|
161
|
+
# ones are born under random groups.
|
|
162
|
+
fun pushChurn(sc: Bench, ctx: ECtx[Bod], k: Int) =
|
|
163
|
+
for j := range k
|
|
164
|
+
i := sc.rng.next(sc.live.length())
|
|
165
|
+
victim := sc.live.at(i)
|
|
166
|
+
_ := sc.live.set(i, sc.live.at(sc.live.length() - 1))
|
|
167
|
+
_ := sc.live.pop()
|
|
168
|
+
ctx.out.push(EKill(id: victim))
|
|
169
|
+
for j := range k
|
|
170
|
+
g := sc.rng.next(10)
|
|
171
|
+
id := sc.nextId
|
|
172
|
+
sc.nextId = id + 1
|
|
173
|
+
ctx.out.push(ESpawn(parent: g + 1, node: newLeaf(sc.rng, id)))
|
|
174
|
+
sc.live.push(id)
|
|
175
|
+
|
|
176
|
+
# `t` ticks of churn: the commands pushed, then a step that applies them in its walk, or
|
|
177
|
+
# (`after`) a step, then the commands pushed and applied with a second walk.
|
|
178
|
+
fun churns(stage: EStage[Bod], sc: Bench, ctx: ECtx[Bod], dt: Float, k: Int, t: Int, after: Bool) -> EStage[Bod] =
|
|
179
|
+
if t == 0
|
|
180
|
+
stage
|
|
181
|
+
else if after
|
|
182
|
+
s1 := stage.step(ctx, dt)
|
|
183
|
+
pushChurn(sc, ctx, k)
|
|
184
|
+
s2 := s1.apply(ctx.out)
|
|
185
|
+
ctx.out.clear()
|
|
186
|
+
churns(s2, sc, ctx, dt, k, t - 1, after)
|
|
187
|
+
else
|
|
188
|
+
pushChurn(sc, ctx, k)
|
|
189
|
+
churns(stage.step(ctx, dt), sc, ctx, dt, k, t - 1, after)
|
|
190
|
+
|
|
191
|
+
# `churns` (commands before the step) with each tick's stage also kept in a ring of 60.
|
|
192
|
+
fun churnsKept(stage: EStage[Bod], sc: Bench, ctx: ECtx[Bod], dt: Float, k: Int, t: Int, kept: Slice[EStage[Bod]]) -> EStage[Bod] =
|
|
193
|
+
if t == 0
|
|
194
|
+
stage
|
|
195
|
+
else
|
|
196
|
+
keep(kept, stage, t)
|
|
197
|
+
pushChurn(sc, ctx, k)
|
|
198
|
+
churnsKept(stage.step(ctx, dt), sc, ctx, dt, k, t - 1, kept)
|
|
199
|
+
|
|
200
|
+
# EStage's walk with no commands and the root's context for every body: no parent's world
|
|
201
|
+
# transform is computed.
|
|
202
|
+
fun stepFlat(t: ETree[Bod], ctx: ECtx[Bod], dt: Float) -> ETree[Bod] = match t
|
|
203
|
+
EEmpty => t
|
|
204
|
+
ENode(b, k, n) =>
|
|
205
|
+
nb := b.step(ctx, dt)
|
|
206
|
+
if nb.gone()
|
|
207
|
+
stepFlat(n, ctx, dt)
|
|
208
|
+
else
|
|
209
|
+
k2 := if k.isEmpty() then k else stepFlat(k, ctx, dt)
|
|
210
|
+
ENode(value: nb, firstChild: k2, nextSibling: stepFlat(n, ctx, dt))
|
|
211
|
+
|
|
212
|
+
# EStage's walk with no commands, every body's world transform computed (and checked, so
|
|
213
|
+
# it is used).
|
|
214
|
+
fun stepEager(t: ETree[Bod], ctx: ECtx[Bod], dt: Float) -> ETree[Bod] = match t
|
|
215
|
+
EEmpty => t
|
|
216
|
+
ENode(b, k, n) =>
|
|
217
|
+
m := ctx.worldOf(b.local())
|
|
218
|
+
assert m.e > -1.0e30
|
|
219
|
+
cc := ctx.under(b.local(), b.opacity())
|
|
220
|
+
nb := b.step(ctx, dt)
|
|
221
|
+
if nb.gone()
|
|
222
|
+
stepEager(n, ctx, dt)
|
|
223
|
+
else
|
|
224
|
+
k2 := if k.isEmpty() then k else stepEager(k, cc, dt)
|
|
225
|
+
ENode(value: nb, firstChild: k2, nextSibling: stepEager(n, ctx, dt))
|
|
226
|
+
|
|
227
|
+
fun walks(stage: EStage[Bod], ctx: ECtx[Bod], dt: Float, t: Int, eager: Bool) -> EStage[Bod] =
|
|
228
|
+
if t == 0
|
|
229
|
+
stage
|
|
230
|
+
else if eager
|
|
231
|
+
walks(stage.withRoot(stepEager(stage.root, ctx, dt)), ctx, dt, t - 1, eager)
|
|
232
|
+
else
|
|
233
|
+
walks(stage.withRoot(stepFlat(stage.root, ctx, dt)), ctx, dt, t - 1, eager)
|
|
234
|
+
|
|
235
|
+
# 1000 flattens: the entries over all of them plus the summed translations of the last.
|
|
236
|
+
fun draws(stage: EStage[Bod], r: Int, total: Int) -> Int =
|
|
237
|
+
d := stage.flatten()
|
|
238
|
+
if r == 999
|
|
239
|
+
last := lastSum(d, 0, 0.0)
|
|
240
|
+
total + d.length() + Int(last)
|
|
241
|
+
else
|
|
242
|
+
draws(stage, r + 1, total + d.length())
|
|
243
|
+
|
|
244
|
+
fun lastSum(d: Slice[EDrawn[Bod]], i: Int, acc: Float) -> Float =
|
|
245
|
+
if i >= d.length() then acc else lastSum(d, i + 1, acc + d.at(i).m.e + d.at(i).m.f)
|
|
246
|
+
|
|
247
|
+
# 1000 hit tests at random stage points: the positions of the bodies hit, in order.
|
|
248
|
+
fun hits(stage: EStage[Bod], rng: Rng, q: Int, sum: Int) -> Int =
|
|
249
|
+
if q == 1000
|
|
250
|
+
sum
|
|
251
|
+
else
|
|
252
|
+
x := rng.float(1600)
|
|
253
|
+
y := rng.float(800)
|
|
254
|
+
next := match stage.hit(x, y)
|
|
255
|
+
Some(a) => {sum * 31 + Int(a.x * 100.0) + 7 * Int(a.y * 100.0)} % 1000000007
|
|
256
|
+
None => {sum * 31 + 1} % 1000000007
|
|
257
|
+
hits(stage, rng, q + 1, next)
|
|
258
|
+
|
|
259
|
+
# Runs workload `w` on `stage`, a scene of `n` nodes, and returns its checksum.
|
|
260
|
+
fun work(stage: EStage[Bod], sc: Bench, w: Int, n: Int) -> Int =
|
|
261
|
+
dt := 1.0 / 60.0
|
|
262
|
+
ctx := ECtx[Bod]()
|
|
263
|
+
k := {{n - 11} / 100}.max(1)
|
|
264
|
+
if w == 1
|
|
265
|
+
hashTree(steps(stage, ctx, dt, 1000).root, 0)
|
|
266
|
+
else if w == 2 || w == 7
|
|
267
|
+
hashTree(churns(stage, sc, ctx, dt, k, 1000, w == 7).root, 0) + sc.live.length()
|
|
268
|
+
else if w == 3
|
|
269
|
+
draws(stage, 0, 0)
|
|
270
|
+
else if w == 4
|
|
271
|
+
hits(stage, sc.rng, 0, 0)
|
|
272
|
+
else if w == 5 || w == 6
|
|
273
|
+
hashTree(walks(stage, ctx, dt, 1000, w == 6).root, 0)
|
|
274
|
+
else if w == 8
|
|
275
|
+
hashTree(stepsKept(stage, ctx, dt, 1000, Slice[EStage[Bod]]()).root, 0)
|
|
276
|
+
else
|
|
277
|
+
hashTree(churnsKept(stage, sc, ctx, dt, k, 1000, Slice[EStage[Bod]]()).root, 0) + sc.live.length()
|
|
278
|
+
|
|
279
|
+
# Builds the scene, then times the workload alone with the wall clock.
|
|
280
|
+
fun run(io: Io, w: Int, n: Int) -> Int =
|
|
281
|
+
sc := bench(n)
|
|
282
|
+
stage := build(n, sc)
|
|
283
|
+
t0 := io.rawNowMillis()
|
|
284
|
+
sum := work(stage, sc, w, n)
|
|
285
|
+
ms := io.rawNowMillis() - t0
|
|
286
|
+
io.printLn("game_enum_tree w{w} n{n} ms {ms} checksum {sum}")
|
|
287
|
+
sum
|
|
288
|
+
|
|
289
|
+
fun main(io: Io) =
|
|
290
|
+
args := os.readFile(io, "scene_trees.args").unwrapOr("").trim().split(" ", 2)
|
|
291
|
+
if args.length() == 2
|
|
292
|
+
_ := run(io, Int.parse(args.at(0)).unwrapOr(1), Int.parse(args.at(1)).unwrapOr(100))
|
|
293
|
+
else
|
|
294
|
+
for w := range 4
|
|
295
|
+
_ := run(io, w + 1, 100)
|
|
296
|
+
|
|
297
|
+
fun check(w: Int) -> Int =
|
|
298
|
+
sc := bench(100)
|
|
299
|
+
work(build(100, sc), sc, w, 100)
|
|
300
|
+
|
|
301
|
+
# The same checksums as master's three versions.
|
|
302
|
+
test "the nine workloads at N = 100 give master's checksums"
|
|
303
|
+
assert check(1) == 809683012
|
|
304
|
+
assert check(2) == 998134459
|
|
305
|
+
assert check(3) == 211827
|
|
306
|
+
assert check(4) == 462421202
|
|
307
|
+
assert check(5) == 809683012
|
|
308
|
+
assert check(6) == 809683012
|
|
309
|
+
assert check(7) == 998134459
|
|
310
|
+
assert check(8) == 809683012
|
|
311
|
+
assert check(9) == 998134459
|
benchmarks/scene_trees/master/game_enum_tree_kept.plum
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import std/Int
|
|
2
|
+
import std/Float
|
|
3
|
+
import std/Bool
|
|
4
|
+
import std/Str
|
|
5
|
+
import std/Io
|
|
6
|
+
import std/Option
|
|
7
|
+
import std/collections/List
|
|
8
|
+
import std/math/Vec2
|
|
9
|
+
import std/math/Affine
|
|
10
|
+
import std/game/Color
|
|
11
|
+
import std/game_enum_tree/ETree
|
|
12
|
+
import std/game_enum_tree/EBody
|
|
13
|
+
import std/game_enum_tree/ECtx
|
|
14
|
+
import std/game_enum_tree/EGroup
|
|
15
|
+
import std/game_enum_tree/ERect
|
|
16
|
+
import std/game_enum_tree/EStage
|
|
17
|
+
|
|
18
|
+
# A copy of master's benchmarks/scene_trees/game_enum_tree.plum (at e0ccfc7) with two more
|
|
19
|
+
# workloads, 8 and 9: 1 and 2 with the last 60 stages kept in a ring, the snapshot variant
|
|
20
|
+
# the branch's version also runs. Build it with master's compiler and master's std
|
|
21
|
+
# (benchmarks/scene_trees/run.py does). The rest of this header is master's.
|
|
22
|
+
#
|
|
23
|
+
# Scene-tree benchmark, std/game_enum_tree version: a persistent tree of immutable bodies,
|
|
24
|
+
# referred to by id; every step makes a new tree.
|
|
25
|
+
# The three versions (game.plum, game_tree.plum, game_enum_tree.plum) build the same scene
|
|
26
|
+
# from the same random numbers, do the same work and print the same checksum; see
|
|
27
|
+
# docs/benchmarks/2026-11-08-scene-trees.md and run.py. With two arguments, a workload and
|
|
28
|
+
# N (`plum run game.plum -- 1 10000`), it runs that one, otherwise all seven at N = 100.
|
|
29
|
+
#
|
|
30
|
+
# The scene: a root, 10 groups (moved, scaled and turned), and N - 11 leaves, 20 by 20
|
|
31
|
+
# rectangles, spread over the groups in turn. Workloads:
|
|
32
|
+
# 1 step: 1000 ticks; every leaf moves by its velocity and bounces in its group's box.
|
|
33
|
+
# 2 churn: 1000 ticks; a step, then 1% of the leaves (at least one) die and as many are
|
|
34
|
+
# born, picked and placed by the same random numbers.
|
|
35
|
+
# 3 draw: 1000 flattens of the tree to the draw list, with transforms.
|
|
36
|
+
# 4 hit: 1000 hit tests at random stage points.
|
|
37
|
+
# Variants that split step's cost (a version a variant doesn't apply to runs its base):
|
|
38
|
+
# 5 step, no transforms: the tree versions walk with one context for every node, so no
|
|
39
|
+
# node's world transform is computed; std/game's is the same as 1.
|
|
40
|
+
# 6 step, eager transforms: every node's world transform (parent * local()) is computed
|
|
41
|
+
# each tick: std/game's walk computes it, the tree versions' walk asks every
|
|
42
|
+
# node's lazy context for it (`ctx.world()`).
|
|
43
|
+
# 7 churn, apply after: std/game_enum_tree pushes the tick's kills and spawns after its
|
|
44
|
+
# step and applies them with a second walk (`EStage.apply`), as before T30;
|
|
45
|
+
# std/game and std/game_tree run 2.
|
|
46
|
+
|
|
47
|
+
struct Rng(s: Int) =
|
|
48
|
+
fun next(&self, k: Int) -> Int
|
|
49
|
+
self.s = {self.s * 1103515245 + 12345} % 2147483648
|
|
50
|
+
return {self.s / 65536} % k
|
|
51
|
+
|
|
52
|
+
fun float(&self, k: Int) -> Float
|
|
53
|
+
return Float(self.next(k * 10)) / 10.0
|
|
54
|
+
|
|
55
|
+
struct Leaf(EBody(id, x, y, width, height, originX, originY, scaleX, scaleY, rotation, color, visible, dead), vx: Float, vy: Float) =
|
|
56
|
+
override fun step(self, ctx: ECtx, dt: Float) -> EBody
|
|
57
|
+
nx := self.x + self.vx * dt
|
|
58
|
+
ny := self.y + self.vy * dt
|
|
59
|
+
vx := self.vx
|
|
60
|
+
vy := self.vy
|
|
61
|
+
if nx < 0.0 || nx > 200.0
|
|
62
|
+
vx = 0.0 - vx
|
|
63
|
+
if ny < 0.0 || ny > 200.0
|
|
64
|
+
vy = 0.0 - vy
|
|
65
|
+
return Leaf(..self, x: nx, y: ny, vx: vx, vy: vy)
|
|
66
|
+
|
|
67
|
+
struct Bench(stage: EStage, live: List[Int], rng: Rng, nextId: Int)
|
|
68
|
+
|
|
69
|
+
fun newLeaf(rng: Rng, id: Int) -> ETree[EBody]
|
|
70
|
+
x := rng.float(200)
|
|
71
|
+
y := rng.float(200)
|
|
72
|
+
vx := rng.float(100) - 50.0
|
|
73
|
+
vy := rng.float(100) - 50.0
|
|
74
|
+
b: EBody := Leaf(
|
|
75
|
+
id: id, x: x, y: y, width: 20.0, height: 20.0, originX: 0.0, originY: 0.0,
|
|
76
|
+
scaleX: 1.0, scaleY: 1.0, rotation: 0.0, color: Color.Red, visible: true, dead: false,
|
|
77
|
+
vx: vx, vy: vy)
|
|
78
|
+
return etree.leaf(b)
|
|
79
|
+
|
|
80
|
+
# Built bottom up, as a persistent tree is: each group's children first, then the groups
|
|
81
|
+
# under the root. Ids: the root 0, the groups 1 to 10, the leaves from 11.
|
|
82
|
+
fun build(n: Int) -> Bench
|
|
83
|
+
rng := Rng(s: 12345)
|
|
84
|
+
kids := List[List[ETree[EBody]]]()
|
|
85
|
+
for g := range 10
|
|
86
|
+
kids.push(List[ETree[EBody]]())
|
|
87
|
+
live := List[Int]()
|
|
88
|
+
for i := range n - 11
|
|
89
|
+
l := newLeaf(rng, 11 + i)
|
|
90
|
+
kids.at(i % 10).push(l)
|
|
91
|
+
live.push(11 + i)
|
|
92
|
+
groups := List[ETree[EBody]]()
|
|
93
|
+
for g := range 10
|
|
94
|
+
s := 1.0 + 0.05 * Float(g)
|
|
95
|
+
gb: EBody := EGroup.turned(g + 1, Float(100 + 300 * {g % 5}), Float(100 + 300 * {g / 5}), s, 0.1 * Float(g))
|
|
96
|
+
groups.push(etree.of(gb, kids.at(g)))
|
|
97
|
+
stage := EStage.create()
|
|
98
|
+
stage = stage.withRoot(stage.root.addChildren(groups))
|
|
99
|
+
return Bench(stage: stage, live: live, rng: rng, nextId: n)
|
|
100
|
+
|
|
101
|
+
# Every node's position, in preorder, folded into one number.
|
|
102
|
+
fun hashTree(t: ETree[EBody], h: Int) -> Int
|
|
103
|
+
return t.fold(h, |r0, b|
|
|
104
|
+
r := {r0 * 31 + Int(b.x * 1000.0)} % 1000000007
|
|
105
|
+
return {r * 31 + Int(b.y * 1000.0)} % 1000000007
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
# EStage.step's walk (estepChain) with one context, `ctx`, for every body instead of
|
|
109
|
+
# `ctx.under(b)`: no world transform is computed. Otherwise the same: a new body and link
|
|
110
|
+
# node for every changed one, the unchanged tail of a chain shared.
|
|
111
|
+
fun stepFlat(t: ETree[EBody], ctx: ECtx, dt: Float) -> ETree[EBody]
|
|
112
|
+
vals := List[EBody]()
|
|
113
|
+
kids := List[ETree[EBody]]()
|
|
114
|
+
upTo := 0
|
|
115
|
+
tail := t
|
|
116
|
+
cur := t
|
|
117
|
+
while ENode(b, k, n) := cur
|
|
118
|
+
keep := false
|
|
119
|
+
if !b.dead
|
|
120
|
+
nb := b.step(ctx, dt)
|
|
121
|
+
if !nb.dead
|
|
122
|
+
keep = true
|
|
123
|
+
k2 := k
|
|
124
|
+
if !k.isEmpty()
|
|
125
|
+
k2 = stepFlat(k, ctx, dt)
|
|
126
|
+
vals.push(nb)
|
|
127
|
+
kids.push(k2)
|
|
128
|
+
if nb != b || k2 != k
|
|
129
|
+
upTo = vals.length()
|
|
130
|
+
tail = n
|
|
131
|
+
if !keep
|
|
132
|
+
upTo = vals.length()
|
|
133
|
+
tail = n
|
|
134
|
+
cur = n
|
|
135
|
+
if tail == t
|
|
136
|
+
return t
|
|
137
|
+
return etree.etreeLink(vals, kids, upTo, tail)
|
|
138
|
+
|
|
139
|
+
# EStage.step's walk (estepChain, without commands) with every body asking for its world
|
|
140
|
+
# transform: what the step costs when every body reads `ctx.world()`.
|
|
141
|
+
fun stepEager(t: ETree[EBody], ctx: ECtx, dt: Float) -> ETree[EBody]
|
|
142
|
+
vals := List[EBody]()
|
|
143
|
+
kids := List[ETree[EBody]]()
|
|
144
|
+
upTo := 0
|
|
145
|
+
tail := t
|
|
146
|
+
cur := t
|
|
147
|
+
c := ctx
|
|
148
|
+
first := true
|
|
149
|
+
seen := 0.0
|
|
150
|
+
while ENode(b, k, n) := cur
|
|
151
|
+
keep := false
|
|
152
|
+
if !b.dead
|
|
153
|
+
if first
|
|
154
|
+
c = ctx.under(b)
|
|
155
|
+
first = false
|
|
156
|
+
else
|
|
157
|
+
c.retarget(ctx, b)
|
|
158
|
+
seen = seen + c.world().e
|
|
159
|
+
nb := b.step(c, dt)
|
|
160
|
+
if !nb.dead
|
|
161
|
+
keep = true
|
|
162
|
+
k2 := k
|
|
163
|
+
if !k.isEmpty()
|
|
164
|
+
k2 = stepEager(k, c, dt)
|
|
165
|
+
vals.push(nb)
|
|
166
|
+
kids.push(k2)
|
|
167
|
+
if nb != b || k2 != k
|
|
168
|
+
upTo = vals.length()
|
|
169
|
+
tail = n
|
|
170
|
+
if !keep
|
|
171
|
+
upTo = vals.length()
|
|
172
|
+
tail = n
|
|
173
|
+
cur = n
|
|
174
|
+
# never true; it keeps the transforms in use
|
|
175
|
+
if seen < -1.0e30
|
|
176
|
+
return t
|
|
177
|
+
if tail == t
|
|
178
|
+
return t
|
|
179
|
+
return etree.etreeLink(vals, kids, upTo, tail)
|
|
180
|
+
|
|
181
|
+
# Keeps `stage` in `kept`, a ring of the last 60 stages.
|
|
182
|
+
fun keepStage(kept: List[EStage], stage: EStage, t: Int)
|
|
183
|
+
if kept.length() < 60
|
|
184
|
+
kept.push(stage)
|
|
185
|
+
else
|
|
186
|
+
_ := kept.set(t % 60, stage)
|
|
187
|
+
|
|
188
|
+
# Runs workload `w` on `sc`, a scene of `n` nodes, and returns its checksum.
|
|
189
|
+
fun work(sc: Bench, wl: Int, n: Int) -> Int
|
|
190
|
+
dt := 1.0 / 60.0
|
|
191
|
+
sum := 0
|
|
192
|
+
ctx := ECtx.blank()
|
|
193
|
+
stage := sc.stage
|
|
194
|
+
nextId := sc.nextId
|
|
195
|
+
w := wl
|
|
196
|
+
# 8 and 9: 1 and 2 with the last 60 stages kept in a ring
|
|
197
|
+
kept := List[EStage]()
|
|
198
|
+
keepOn := false
|
|
199
|
+
if w == 8
|
|
200
|
+
w = 1
|
|
201
|
+
keepOn = true
|
|
202
|
+
if w == 9
|
|
203
|
+
w = 2
|
|
204
|
+
keepOn = true
|
|
205
|
+
if w == 6
|
|
206
|
+
for t := range 1000
|
|
207
|
+
stage = stage.withRoot(stepEager(stage.root, ctx, dt))
|
|
208
|
+
sum = hashTree(stage.root, 0)
|
|
209
|
+
if w == 5
|
|
210
|
+
for t := range 1000
|
|
211
|
+
stage = stage.withRoot(stepFlat(stage.root, ctx, dt))
|
|
212
|
+
sum = hashTree(stage.root, 0)
|
|
213
|
+
if w == 1
|
|
214
|
+
for t := range 1000
|
|
215
|
+
if keepOn
|
|
216
|
+
keepStage(kept, stage, t)
|
|
217
|
+
stage = stage.step(ctx, dt)
|
|
218
|
+
sum = hashTree(stage.root, 0)
|
|
219
|
+
if w == 2 || w == 7
|
|
220
|
+
k := {n - 11} / 100
|
|
221
|
+
if k < 1
|
|
222
|
+
k = 1
|
|
223
|
+
for t := range 1000
|
|
224
|
+
# churn: the tick's kills and spawns are pushed before the step, which applies them
|
|
225
|
+
# in its own walk (after the bodies' steps, as `apply` would); 7 pushes them after
|
|
226
|
+
# the step and applies them with a second walk, `apply`. The picks depend only on
|
|
227
|
+
# the random numbers and the live list, so both make the same tree.
|
|
228
|
+
if keepOn
|
|
229
|
+
keepStage(kept, stage, t)
|
|
230
|
+
if w == 7
|
|
231
|
+
stage = stage.step(ctx, dt)
|
|
232
|
+
for j := range k
|
|
233
|
+
i := sc.rng.next(sc.live.length())
|
|
234
|
+
victim := sc.live.at(i)
|
|
235
|
+
_ := sc.live.set(i, sc.live.at(sc.live.length() - 1))
|
|
236
|
+
_ := sc.live.pop()
|
|
237
|
+
ctx.out.push(EKill(id: victim))
|
|
238
|
+
for j := range k
|
|
239
|
+
g := sc.rng.next(10)
|
|
240
|
+
id := nextId
|
|
241
|
+
nextId = id + 1
|
|
242
|
+
ctx.out.push(ESpawn(parent: g + 1, node: newLeaf(sc.rng, id)))
|
|
243
|
+
sc.live.push(id)
|
|
244
|
+
if w == 7
|
|
245
|
+
stage = stage.apply(ctx.out)
|
|
246
|
+
ctx.out.clear()
|
|
247
|
+
else
|
|
248
|
+
stage = stage.step(ctx, dt)
|
|
249
|
+
sum = hashTree(stage.root, 0) + sc.live.length()
|
|
250
|
+
if w == 3
|
|
251
|
+
total := 0
|
|
252
|
+
last := 0.0
|
|
253
|
+
for r := range 1000
|
|
254
|
+
d := stage.flatten()
|
|
255
|
+
total = total + d.length()
|
|
256
|
+
if r == 999
|
|
257
|
+
for e := range d
|
|
258
|
+
last = last + e.m.e + e.m.f
|
|
259
|
+
sum = total + Int(last)
|
|
260
|
+
if w == 4
|
|
261
|
+
for q := range 1000
|
|
262
|
+
x := sc.rng.float(1600)
|
|
263
|
+
y := sc.rng.float(800)
|
|
264
|
+
if Some(a) := stage.hit(x, y)
|
|
265
|
+
sum = {sum * 31 + Int(a.x * 100.0) + 7 * Int(a.y * 100.0)} % 1000000007
|
|
266
|
+
else
|
|
267
|
+
sum = {sum * 31 + 1} % 1000000007
|
|
268
|
+
return sum
|
|
269
|
+
|
|
270
|
+
# Builds the scene, then times the workload alone with the wall clock.
|
|
271
|
+
fun run(w: Int, n: Int, io: Io) -> Int
|
|
272
|
+
sc := build(n)
|
|
273
|
+
t0 := io.nowMicros()
|
|
274
|
+
sum := work(sc, w, n)
|
|
275
|
+
us := io.nowMicros() - t0
|
|
276
|
+
io.printLn("game_enum_tree w{w} n{n} ms {Float(us) / 1000.0} checksum {sum}")
|
|
277
|
+
return sum
|
|
278
|
+
|
|
279
|
+
fun main(io: Io)
|
|
280
|
+
args := io.args()
|
|
281
|
+
if args.length() == 2
|
|
282
|
+
_ := run(Int.parse(args.at(0)).unwrap(), Int.parse(args.at(1)).unwrap(), io)
|
|
283
|
+
else
|
|
284
|
+
for w := range 7
|
|
285
|
+
_ := run(w + 1, 100, io)
|
|
286
|
+
|
|
287
|
+
# The same checksums in all three versions (game.plum, game_tree.plum, game_enum_tree.plum).
|
|
288
|
+
test "the nine workloads at N = 100 give the shared checksums"
|
|
289
|
+
assert work(build(100), 1, 100) == 809683012
|
|
290
|
+
assert work(build(100), 2, 100) == 998134459
|
|
291
|
+
assert work(build(100), 3, 100) == 211827
|
|
292
|
+
assert work(build(100), 4, 100) == 462421202
|
|
293
|
+
assert work(build(100), 5, 100) == 809683012
|
|
294
|
+
assert work(build(100), 6, 100) == 809683012
|
|
295
|
+
assert work(build(100), 7, 100) == 998134459
|
|
296
|
+
assert work(build(100), 8, 100) == 809683012
|
|
297
|
+
assert work(build(100), 9, 100) == 998134459
|
benchmarks/scene_trees/run.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Times the persistent scene tree on this branch against master's three scene trees.
|
|
3
|
+
|
|
4
|
+
usage: python3 benchmarks/scene_trees/run.py --master DIR [--runs 5] [--sizes 100,1000,10000]
|
|
5
|
+
[--workloads 1,...,9] [--out target/scene_trees]
|
|
6
|
+
|
|
7
|
+
Run from the repository root of this branch, after `cargo build -p plum-cli`. `--master` is
|
|
8
|
+
a checkout of master (e0ccfc7 or later) with its compiler built (`cargo build -p plum-cli`
|
|
9
|
+
there); a copy made with `git archive` works, and keeps the build out of your own master
|
|
10
|
+
checkout:
|
|
11
|
+
|
|
12
|
+
mkdir -p target/master-src
|
|
13
|
+
git archive e0ccfc7 | tar -x -C target/master-src # from a clone that has master
|
|
14
|
+
(cd target/master-src && cargo build -p plum-cli)
|
|
15
|
+
python3 benchmarks/scene_trees/run.py --master target/master-src
|
|
16
|
+
|
|
17
|
+
Versions:
|
|
18
|
+
game, game_tree master's benchmarks/scene_trees/game.plum and game_tree.plum
|
|
19
|
+
enum_tree benchmarks/scene_trees/master/game_enum_tree_kept.plum: master's
|
|
20
|
+
game_enum_tree.plum plus workloads 8 and 9 (master's compiler)
|
|
21
|
+
branch benchmarks/scene_trees/game_enum_tree.plum, this branch's default
|
|
22
|
+
branch --no-fbip the same without in-place updates or cell reuse
|
|
23
|
+
branch --no-fusion the same without fusion
|
|
24
|
+
|
|
25
|
+
Each is built once with `plum build` (a native executable that runs the wasm under
|
|
26
|
+
wasmtime). Then every (workload, N, version) runs once per round, in a rotated order, for
|
|
27
|
+
`--runs` rounds, each a fresh process. Master's programs take the workload and N as
|
|
28
|
+
arguments and time with the microsecond clock; the branch's program reads them from
|
|
29
|
+
`scene_trees.args` in its working folder and times with the branch's whole-ms clock. The
|
|
30
|
+
table gives the median ms with the range, and fails if two runs that should agree print
|
|
31
|
+
different checksums. Workloads 8 and 9 (snapshots kept) only run on the persistent trees.
|
|
32
|
+
"""
|
|
33
|
+
import argparse
|
|
34
|
+
import os
|
|
35
|
+
import re
|
|
36
|
+
import statistics
|
|
37
|
+
import subprocess
|
|
38
|
+
import sys
|
|
39
|
+
|
|
40
|
+
WORKLOADS = {1: "step", 2: "churn", 3: "draw", 4: "hit",
|
|
41
|
+
5: "step, no transforms", 6: "step, eager transforms", 7: "churn, apply after",
|
|
42
|
+
8: "step, 60 kept", 9: "churn, 60 kept"}
|
|
43
|
+
MASTER = ["game", "game_tree", "enum_tree"]
|
|
44
|
+
BRANCH = [("branch", []), ("branch --no-fbip", ["--no-fbip"]), ("branch --no-fusion", ["--no-fusion"])]
|
|
45
|
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def fmt(ms):
|
|
49
|
+
"""Whole ms from 10 ms up, one decimal below."""
|
|
50
|
+
return f"{ms:.0f}" if ms >= 10 else f"{ms:.1f}"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def build(plum, src, exe, flags, cwd, lib=None):
|
|
54
|
+
cmd = [plum, "build", *flags, src, "-o", exe]
|
|
55
|
+
if lib:
|
|
56
|
+
cmd += ["--lib-path", lib]
|
|
57
|
+
subprocess.run(cmd, check=True, cwd=cwd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def main():
|
|
61
|
+
ap = argparse.ArgumentParser()
|
|
62
|
+
ap.add_argument("--master", required=True, help="a built checkout of master")
|
|
63
|
+
ap.add_argument("--runs", type=int, default=5)
|
|
64
|
+
ap.add_argument("--sizes", default="100,1000,10000")
|
|
65
|
+
ap.add_argument("--workloads", default="1,2,3,4,5,6,7,8,9")
|
|
66
|
+
ap.add_argument("--out", default="target/scene_trees")
|
|
67
|
+
args = ap.parse_args()
|
|
68
|
+
master = os.path.abspath(args.master)
|
|
69
|
+
mplum = os.path.join(master, "target/debug/plum")
|
|
70
|
+
out = os.path.abspath(args.out)
|
|
71
|
+
os.makedirs(out, exist_ok=True)
|
|
72
|
+
exes = {}
|
|
73
|
+
for v in ("game", "game_tree"):
|
|
74
|
+
exes[v] = os.path.join(out, "m_" + v)
|
|
75
|
+
build(mplum, os.path.join(master, "benchmarks/scene_trees", v + ".plum"), exes[v], [], master)
|
|
76
|
+
exes["enum_tree"] = os.path.join(out, "m_enum_tree")
|
|
77
|
+
build(mplum, os.path.join(HERE, "master/game_enum_tree_kept.plum"), exes["enum_tree"], [], master, master)
|
|
78
|
+
for name, flags in BRANCH:
|
|
79
|
+
exes[name] = os.path.join(out, "b" + "".join(flags).replace("-", "_"))
|
|
80
|
+
build("target/debug/plum", os.path.join(HERE, "game_enum_tree.plum"), exes[name], flags, ".")
|
|
81
|
+
versions = MASTER + [b for b, _ in BRANCH]
|
|
82
|
+
sizes = [int(x) for x in args.sizes.split(",")]
|
|
83
|
+
workloads = [int(x) for x in args.workloads.split(",")]
|
|
84
|
+
cells = [(w, n) for w in workloads for n in sizes]
|
|
85
|
+
times, sums = {}, {}
|
|
86
|
+
for r in range(args.runs):
|
|
87
|
+
for w, n in cells:
|
|
88
|
+
vs = [v for v in versions if w < 8 or v not in ("game", "game_tree")]
|
|
89
|
+
k = r % len(vs)
|
|
90
|
+
for v in vs[k:] + vs[:k]:
|
|
91
|
+
if v in MASTER:
|
|
92
|
+
res = subprocess.run([exes[v], str(w), str(n)], check=True, capture_output=True, text=True)
|
|
93
|
+
else:
|
|
94
|
+
with open(os.path.join(out, "scene_trees.args"), "w") as f:
|
|
95
|
+
f.write(f"{w} {n}\n")
|
|
96
|
+
res = subprocess.run([exes[v]], check=True, capture_output=True, text=True, cwd=out)
|
|
97
|
+
m = re.search(r"w(\d+) n(\d+) ms (\S+) checksum (\S+)", res.stdout)
|
|
98
|
+
if not m or (int(m.group(1)), int(m.group(2))) != (w, n):
|
|
99
|
+
sys.exit(f"no result from {v} w{w} n{n}: {res.stdout!r}")
|
|
100
|
+
times.setdefault((w, n, v), []).append(float(m.group(3)))
|
|
101
|
+
sums.setdefault((w, n), set()).add(m.group(4))
|
|
102
|
+
print(f"round {r + 1}: {WORKLOADS[w]} N={n} done", file=sys.stderr)
|
|
103
|
+
print("| workload | N | " + " | ".join(versions) + " | branch / enum_tree | checksum |")
|
|
104
|
+
print("|---|---|" + "---|" * (len(versions) + 2))
|
|
105
|
+
for w, n in cells:
|
|
106
|
+
row = f"| {WORKLOADS[w]} | {n} |"
|
|
107
|
+
for v in versions:
|
|
108
|
+
ts = times.get((w, n, v))
|
|
109
|
+
if not ts:
|
|
110
|
+
row += " - |"
|
|
111
|
+
continue
|
|
112
|
+
row += f" {fmt(statistics.median(ts))} ({fmt(min(ts))}-{fmt(max(ts))}) |"
|
|
113
|
+
e = statistics.median(times[(w, n, "enum_tree")])
|
|
114
|
+
b = statistics.median(times[(w, n, "branch")])
|
|
115
|
+
row += f" {b / e:.2f}x |" if e > 0 else " - |"
|
|
116
|
+
row += f" {', '.join(sorted(sums[(w, n)]))} |"
|
|
117
|
+
print(row)
|
|
118
|
+
bad = [(w, n, s) for (w, n), s in sums.items() if len(s) != 1]
|
|
119
|
+
if bad:
|
|
120
|
+
sys.exit(f"checksums disagree: {bad}")
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
if __name__ == "__main__":
|
|
124
|
+
main()
|
docs/benchmarks/2026-11-08-scene-trees-branch.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# The persistent scene tree on simpler-language, against master's three trees
|
|
2
|
+
|
|
3
|
+
Taken 2026-11-09 on an Apple M3 Pro (18 GB), macOS 26.6.2, load average about 3 (2.8 to 3.7 during the run). Master at `e0ccfc7` (lazy `ctx.world()`, kills and spawns in the step's walk, bitsets: "After T30" in master's docs/benchmarks/2026-11-08-scene-trees.md, which is not on this branch), this branch at `ed1be37` plus this change. Five rounds, each cell the median with the range in brackets, in ms.
|
|
4
|
+
|
|
5
|
+
The question: the branch has in-place updates for values only one place holds (FBIP: a spread written into the old value, a rebuilt enum cell written over the old one) and fusion. Does that make a persistent tree, which rebuilds the bodies and links every tick, competitive with master's mutable trees? And what would master need to get the same gain?
|
|
6
|
+
|
|
7
|
+
**Short answer: no, because neither fires on a tree.** A default build, a `--no-fbip` build and a `--no-fusion` build of the benchmark run at the same speed in every cell, and the default and `--no-fbip` wasm are byte for byte the same file. The branch's persistent tree is still 10 to 30% faster than master's on step, churn and draw, and twice as fast on hit, but for other reasons, which master can take without FBIP (see [the recommendation](#recommendation)).
|
|
8
|
+
|
|
9
|
+
## What was ported
|
|
10
|
+
|
|
11
|
+
`std/game_enum_tree` on this branch is master's, in the branch's language (no `return`, `while` or local reassignment, no inheritance):
|
|
12
|
+
|
|
13
|
+
- **`ETree[T]`**: the same enum, `| EEmpty | ENode(value, firstChild, nextSibling)`, left child and right sibling, with the same operations (`fold`, `map`, `filter`, `append`, `appendAll`, `addChild`, `replace`, `update`, `find`, ...) and the same tests. Every walk is a recursion over the chain: `fold`, `each` and `find` are tail calls along the siblings, while the rebuilding walks recurse once per sibling, so a chain's length is bounded by the stack. The tests go up to 10,000 siblings, against master's 100,000.
|
|
14
|
+
- **`EBody`**: master's fields and copy helpers (`at`, `moveBy`, `killed`, ...). Master's subtypes (`EGroup`, `ERect`, a game's `Walker`) can't exist without inheritance, so **`EStage[T]` is generic over the body type**: any type with `key`, `gone`, `shown`, `local`, `opacity`, `hit`, `step(ctx, dt)` and `paint` will do. The tests use an enum of kinds, each holding an `EBody`. The benchmark uses one flat struct with master's `Leaf` fields plus a `moves` flag, so a moving leaf is one allocation, as on master.
|
|
15
|
+
- **`ECtx[T]`** is a value: the parent's world transform and alpha, plus the command list (`out`). A body asks for its own transform with `ctx.worldOf(self.local())`. The walk makes one context per parent that has children (one multiply), from the body as the step reached it, and hands it to every child. That is master's lazy context without the cache field, and without the `retarget` mutation master uses to reuse one context. Master's `actions` dispatcher is left out, since the branch's game library has none.
|
|
16
|
+
- **`EStage[T]`**: `step` applies the commands pushed before it in its own walk (killed bodies dropped unstepped, spawns on top of their parent, unstepped), then `apply`s those pushed during it. It uses the same bitsets for kill and spawn ids below 2^22, with the `Set` and `Map` for the rest. `apply`, `flatten`, `hit`, `draw`, `add`, `update`, `kill` and `find` work as on master, and the tests are master's.
|
|
17
|
+
|
|
18
|
+
One difference in behaviour: master's walks return an unchanged chain as it was (a step that changes nothing returns the same root). Here `step` and `apply` rebuild every link they pass. Sharing needs the old node after the `match`, and that use would rule out writing the new node into the old cell, which was the point of the experiment. `filter`, `replace` and `update` still share what they don't touch, and their tests check it.
|
|
19
|
+
|
|
20
|
+
## The workloads
|
|
21
|
+
|
|
22
|
+
`benchmarks/scene_trees/game_enum_tree.plum` builds master's scene from master's random numbers: a root, 10 groups (moved, scaled and turned) and N - 11 twenty-by-twenty leaves, spread over the groups in turn. Workloads 1 to 7 are master's; 8 and 9 are new:
|
|
23
|
+
|
|
24
|
+
| # | workload | what each tick or repeat does | repeats |
|
|
25
|
+
|---|---|---|---|
|
|
26
|
+
| 1 | step | every leaf moves by its velocity and bounces in its group's 200 by 200 box | 1000 ticks |
|
|
27
|
+
| 2 | churn | 1% of the leaves (at least 1) die and as many are born, pushed before a step that applies them in its walk | 1000 ticks |
|
|
28
|
+
| 3 | draw | flatten the tree to the draw list, with every shown node's stage transform and alpha | 1000 |
|
|
29
|
+
| 4 | hit | a hit test at a random stage point, topmost first | 1000 points |
|
|
30
|
+
| 5 | step, no transforms | the step's walk with the root's context for every body: no transform computed | 1000 ticks |
|
|
31
|
+
| 6 | step, eager transforms | the step's walk with every body's world transform computed | 1000 ticks |
|
|
32
|
+
| 7 | churn, apply after | a step, then the kills and spawns pushed and applied with a second walk (`apply`) | 1000 ticks |
|
|
33
|
+
| 8 | step, 60 kept | 1, with the last 60 stages kept in a ring: snapshots, so no stage is held in only one place | 1000 ticks |
|
|
34
|
+
| 9 | churn, 60 kept | 2, with the last 60 stages kept | 1000 ticks |
|
|
35
|
+
|
|
36
|
+
Master's `game.plum` (std/game, parent links) and `game_tree.plum` (std/game_tree, no parent links) are mutable, so they can't keep snapshots, and they don't run 8 and 9. For master's persistent tree, `benchmarks/scene_trees/master/game_enum_tree_kept.plum` is master's `game_enum_tree.plum` plus 8 and 9, built with master's compiler and std.
|
|
37
|
+
|
|
38
|
+
Every cell's checksum is the same in all six versions on every run, and those are master's checksums. The branch program's `test` checks all nine at N = 100 (`target/debug/plum test benchmarks/scene_trees`, which also passes with `--no-fbip` and `--no-fusion`).
|
|
39
|
+
|
|
40
|
+
## Results
|
|
41
|
+
|
|
42
|
+
Master's columns time with `io.nowMicros()`. The branch's clock (`io.rawNowMillis()`) ticks in whole ms, so its N = 100 cells are near its resolution. The last ratio is the branch's default build against master's persistent tree.
|
|
43
|
+
|
|
44
|
+
| workload | N | std/game | std/game_tree | master enum tree | branch | branch --no-fbip | branch --no-fusion | branch / master enum | checksum |
|
|
45
|
+
|---|---|---|---|---|---|---|---|---|---|
|
|
46
|
+
| step | 100 | 1.8 (1.8-1.8) | 6.0 (5.9-6.0) | 8.9 (8.8-9.0) | 7.0 (7.0-8.0) | 7.0 (7.0-8.0) | 7.0 (7.0-8.0) | 0.79x | 809683012 |
|
|
47
|
+
| step | 1000 | 18 (18-18) | 46 (45-46) | 66 (65-67) | 57 (57-59) | 58 (57-59) | 57 (57-58) | 0.87x | 459572469 |
|
|
48
|
+
| step | 10000 | 171 (170-174) | 425 (414-440) | 638 (636-641) | 551 (549-553) | 550 (547-554) | 550 (549-553) | 0.86x | 77718206 |
|
|
49
|
+
| churn | 100 | 2.0 (2.0-2.1) | 6.2 (6.2-6.2) | 11 (11-11) | 9.0 (9.0-10) | 9.0 (8.0-10) | 9.0 (9.0-9.0) | 0.81x | 998134459 |
|
|
50
|
+
| churn | 1000 | 25 (25-26) | 50 (50-50) | 88 (87-89) | 74 (73-75) | 74 (73-75) | 74 (73-76) | 0.84x | 411093292 |
|
|
51
|
+
| churn | 10000 | 840 (837-845) | 718 (706-722) | 881 (877-887) | 732 (731-736) | 733 (727-734) | 730 (728-732) | 0.83x | 850449880 |
|
|
52
|
+
| draw | 100 | 15 (15-15) | 16 (15-16) | 14 (14-14) | 10 (10-10) | 10 (10-11) | 10 (10-10) | 0.74x | 211827 |
|
|
53
|
+
| draw | 1000 | 152 (150-153) | 155 (154-156) | 135 (134-136) | 94 (93-95) | 92 (91-94) | 93 (92-93) | 0.70x | 2145249 |
|
|
54
|
+
| draw | 10000 | 1499 (1498-1504) | 1544 (1536-1557) | 1318 (1313-1323) | 926 (917-945) | 925 (922-948) | 941 (924-954) | 0.70x | 21533361 |
|
|
55
|
+
| hit | 100 | 14 (14-14) | 14 (14-15) | 14 (14-14) | 6.0 (6.0-6.0) | 6.0 (6.0-7.0) | 7.0 (6.0-7.0) | 0.42x | 462421202 |
|
|
56
|
+
| hit | 1000 | 122 (122-123) | 124 (124-124) | 122 (121-123) | 52 (51-52) | 51 (51-51) | 53 (53-54) | 0.43x | 649980221 |
|
|
57
|
+
| hit | 10000 | 1036 (1031-1039) | 1051 (1037-1052) | 1024 (1023-1030) | 472 (471-473) | 472 (471-473) | 487 (485-488) | 0.46x | 241346959 |
|
|
58
|
+
| step, no transforms | 100 | 1.8 (1.8-2.0) | 4.0 (3.9-4.1) | 5.9 (5.9-6.0) | 6.0 (5.0-6.0) | 6.0 (5.0-6.0) | 6.0 (5.0-6.0) | 1.02x | 809683012 |
|
|
59
|
+
| step, no transforms | 1000 | 18 (18-18) | 40 (40-40) | 53 (53-53) | 51 (51-52) | 51 (51-52) | 52 (51-54) | 0.96x | 459572469 |
|
|
60
|
+
| step, no transforms | 10000 | 170 (170-171) | 389 (388-390) | 529 (527-530) | 508 (507-511) | 507 (505-510) | 510 (508-513) | 0.96x | 77718206 |
|
|
61
|
+
| step, eager transforms | 100 | 12 (12-12) | 17 (17-17) | 19 (19-19) | 18 (17-19) | 18 (17-18) | 17 (17-18) | 0.97x | 809683012 |
|
|
62
|
+
| step, eager transforms | 1000 | 127 (126-128) | 166 (165-168) | 176 (174-176) | 165 (165-168) | 165 (165-166) | 166 (166-167) | 0.94x | 459572469 |
|
|
63
|
+
| step, eager transforms | 10000 | 1259 (1251-1284) | 1642 (1641-1649) | 1762 (1758-1764) | 1662 (1658-1664) | 1657 (1655-1665) | 1666 (1663-1668) | 0.94x | 77718206 |
|
|
64
|
+
| churn, apply after | 100 | 2.0 (2.0-2.1) | 6.2 (6.2-6.4) | 14 (14-14) | 12 (11-12) | 12 (11-12) | 12 (11-12) | 0.85x | 998134459 |
|
|
65
|
+
| churn, apply after | 1000 | 25 (25-25) | 50 (50-51) | 114 (114-115) | 89 (88-90) | 89 (88-89) | 89 (89-90) | 0.78x | 411093292 |
|
|
66
|
+
| churn, apply after | 10000 | 840 (839-848) | 708 (701-745) | 1180 (1177-1181) | 873 (872-876) | 872 (871-876) | 873 (871-876) | 0.74x | 850449880 |
|
|
67
|
+
| step, 60 kept | 100 | - | - | 8.9 (8.8-9.4) | 7.0 (7.0-8.0) | 7.0 (7.0-8.0) | 7.0 (7.0-7.0) | 0.79x | 809683012 |
|
|
68
|
+
| step, 60 kept | 1000 | - | - | 82 (81-84) | 73 (72-73) | 72 (72-73) | 73 (73-75) | 0.89x | 459572469 |
|
|
69
|
+
| step, 60 kept | 10000 | - | - | 2097 (2085-2119) | 2274 (2264-2310) | 2290 (2269-2320) | 2284 (2271-2315) | 1.08x | 77718206 |
|
|
70
|
+
| churn, 60 kept | 100 | - | - | 11 (11-11) | 9.0 (9.0-9.0) | 9.0 (9.0-9.0) | 9.0 (9.0-10) | 0.81x | 998134459 |
|
|
71
|
+
| churn, 60 kept | 1000 | - | - | 106 (105-107) | 91 (90-92) | 91 (90-91) | 91 (90-92) | 0.86x | 411093292 |
|
|
72
|
+
| churn, 60 kept | 10000 | - | - | 2479 (2466-2492) | 2448 (2443-2469) | 2480 (2471-2488) | 2460 (2444-2477) | 0.99x | 850449880 |
|
|
73
|
+
|
|
74
|
+
Master's three columns agree with master's own "After T30" table within 2.5% in every cell at N = 10000.
|
|
75
|
+
|
|
76
|
+
## What the numbers say
|
|
77
|
+
|
|
78
|
+
**Reuse and fusion change nothing.** The three branch columns agree within their ranges in every row. The default and `--no-fbip` builds compile to the same wasm (same SHA-1), so in this program the in-place proof doesn't fire at all. `plum build --explain-fusion` gives the reasons:
|
|
79
|
+
|
|
80
|
+
- A body's next version is a spread, `Bod(..self, x: nx, ...)`, and it is not written in place: "`self` may be shared by a caller: `b` is rebound by a pattern (call of `step` from stepFlat)". The proof treats every value bound by a `match` pattern as possibly shared, and a tree's bodies can only be reached through `ENode(b, k, n)`.
|
|
81
|
+
- Rebuilding an `ENode` from an `ENode` could reuse the matched cell, but `compiler/core/src/reuse.rs` allows that only in a free, non-generic function whose every caller passes either a freshly built constructor tree or a binder from the same match. A tree that lives across ticks reaches the walk through `stage.root`, a field of a struct built in an earlier tick, so no caller qualifies, and `EStage`'s generic walks are ruled out anyway.
|
|
82
|
+
- `EStage`'s own spreads copy: "`EStage` is generic: its spreads copy".
|
|
83
|
+
|
|
84
|
+
So "with and without the branch's in-place reuse" measures the same program twice. The 60-kept rows, which make every stage shared, cost what they cost because of the garbage collector (next point), not because they turn reuse off: it was never on.
|
|
85
|
+
|
|
86
|
+
**Snapshots cost the collector, on both.** Keeping the last 60 stages is free in work: the old trees are already there. But it keeps about 60 times 2N objects alive, and every collection walks them. At N = 10000 step goes from 638 to 2097 ms on master (3.3x) and from 551 to 2274 on the branch (4.1x); churn from 881 to 2479 and from 732 to 2448. At N = 1000 the extra is small (66 to 82, 57 to 73). Both run on wasm-gc under wasmtime, so this is the same collector with the same live heap; the branch's costs a little more per tick at N = 10000, which we didn't trace.
|
|
87
|
+
|
|
88
|
+
**The branch's tree is faster than master's, for reasons other than reuse:**
|
|
89
|
+
|
|
90
|
+
- **Step and churn, 10 to 25%.** With no transforms the two walks are within 4% (508 against 529 ms), so the walk itself costs about the same. The rest of master's gap is in what master's step does beyond that walk, most likely its `retarget` of a mutable shared context per body and the kill and spawn checks. The branch's context is made once per parent (11 a tick here) and never changed, and its walk builds each new node directly instead of through two scratch lists per chain (`vals`, `kids`) and `etreeLink`. On churn with apply after (7), the gap is largest (873 against 1180), because master's `apply` goes through the same scratch lists a second time.
|
|
91
|
+
- **Draw, 30%, and hit, 55%.** Both are dominated by `local()`, computed for every node visited (10 million times at N = 10000), and the branch's `local()` is cheaper. In a microbenchmark of 10 million `b.rotateTo(r).local()` calls on a plain `EBody` (`plum run`, one run each) it takes 793 ms on the branch and 1182 ms on master. `math.sin` alone takes about the same on both (139 ms on the branch, 127 on master), so the difference is in how `local()` and its `Affine` are compiled, which we didn't trace further. Hit likely gains more than draw because master's `ehitChain` also copies each chain into a `List` to walk it backwards, while the branch's recursion finds the later siblings first on the way back up.
|
|
92
|
+
|
|
93
|
+
**Against the mutable trees, the persistent tree still loses where it allocates.** At N = 10000 the branch's step is 551 ms, std/game_tree's 425 and std/game's 171. Every moving body is a new value each tick, plus a new link node. Its churn (732) is now level with std/game_tree's (718) and ahead of std/game's (840), and its draw and hit beat both mutable trees, which share master's slower `local()`.
|
|
94
|
+
|
|
95
|
+
## Recommendation
|
|
96
|
+
|
|
97
|
+
**The branch's reuse does not make the persistent tree competitive with master's mutable trees: it never runs on it.** The branch's proof is static and syntactic. It writes in place only what it can see is fresh at every call, and a tree that survives from one tick to the next is never that. What fired in the branch's own games (spreads of a state passed straight down a recursion) isn't the shape of a scene tree, whose values are always reached through a pattern on a cell built in an earlier tick. To reuse the cells of a tree held across ticks, the proof would need ownership that follows a value through a struct field and a pattern binder. That means either a much stronger static analysis (uniqueness types, or Koka's borrow inference) or reference counts checked at run time (Perceus: reuse when the count is 1). wasm-gc has no reference counts, so on this runtime the second is out, as the branch's own design notes say.
|
|
98
|
+
|
|
99
|
+
**What to port to master instead**, none of which is FBIP or fusion:
|
|
100
|
+
|
|
101
|
+
1. **Build the new chain directly in the step's walk.** Write `estepChain` and `eapplyChain` as a recursion that returns `ENode(value: nb, firstChild: k2, nextSibling: rest)`, as the branch's `estep` does, instead of collecting the kept nodes into two scratch lists and relinking them with `etreeLink`. Master has recursion, so this is a std change. Together with the next point it is the branch's lead on step, churn and churn with apply after (87, 149 and 307 ms at N = 10000); we didn't split that lead between the scratch lists and the context. The cost is master's sharing of an unchanged chain tail, unless the recursion returns the old node when nothing under it changed, which it can on master, where nothing is lost by using the old node.
|
|
102
|
+
2. **Make one context per parent a value, not a mutable reused one.** The branch's `ECtx` holds the parent's world transform, the body asks with `ctx.worldOf(self.local())`, and the walk makes one per parent that has children. That removes `retarget` and the cache flag, and needs no allocation per body either.
|
|
103
|
+
3. **Find out why `local()` is about 1.5 times slower on master.** It is the biggest single difference (400 ms of draw and 550 ms of hit at N = 10000), and it would speed up std/game and std/game_tree as much as the persistent tree. The suspects are how master compiles the `EBody` subtype's field reads and calls, and the `Affine` constructor. The microbenchmark above is the place to start.
|
|
104
|
+
4. **Walk siblings backwards by recursion in `hit`,** instead of copying each chain into a list.
|
|
105
|
+
|
|
106
|
+
With the first two, master's persistent tree should come close to the branch's (1.3 times std/game_tree on step, level on churn), though that is an expectation, not a measurement. It will stay behind std/game on step, because a persistent tree allocates every moving body every tick, and no reuse on either branch removes that.
|
|
107
|
+
|
|
108
|
+
## How to rerun
|
|
109
|
+
|
|
110
|
+
From this branch's root, with master's compiler built from a copy of master (a `git archive` keeps the build, and the runtime that `plum build` compiles, out of your own master checkout):
|
|
111
|
+
|
|
112
|
+
```sh
|
|
113
|
+
cargo build -p plum-cli
|
|
114
|
+
mkdir -p target/master-src
|
|
115
|
+
git archive e0ccfc7 | tar -x -C target/master-src # from a clone that has master
|
|
116
|
+
(cd target/master-src && cargo build -p plum-cli)
|
|
117
|
+
python3 benchmarks/scene_trees/run.py --master target/master-src # 5 rounds; --runs, --sizes, --workloads 1,...,9
|
|
118
|
+
target/debug/plum test benchmarks/scene_trees # master's checksums at N = 100, all nine workloads
|
|
119
|
+
echo "1 10000" > scene_trees.args && target/debug/plum run benchmarks/scene_trees/game_enum_tree.plum # one cell
|
|
120
|
+
target/debug/plum build --explain-fusion benchmarks/scene_trees/game_enum_tree.plum # the proof's reasons
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
`run.py` builds each version once with `plum build` (a native executable running the wasm under wasmtime, with the runtime built in release), runs every (workload, N, version) once per round in a rotated order, each a fresh process, and fails if two runs of a cell print different checksums. The branch's programs get no command-line arguments, so `run.py` writes the workload and N to `scene_trees.args` in the output folder and runs the branch's executables there. A full run of five rounds took about 12 minutes here.
|