plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
7d72c7e
— Peter John
2026-11-16T21:31:30+05:30
U4d part 1: game delivery fast path, held keys as bits
- compiler/core/src/ctors.rs +7 -0
- docs/backlog.md +1 -2
- docs/game.md +2 -2
- std/game/Game.plum +4 -1
- std/game/Input.plum +40 -5
- std/game/ScreenSlice.plum +37 -11
- tests/std/game/Input.plum +16 -0
compiler/core/src/ctors.rs
CHANGED
|
@@ -149,6 +149,13 @@ pub fn lowerCtors(source: &mut Source) -> Vec<String> {
|
|
|
149
149
|
match item {
|
|
150
150
|
Item::Fn(f) => rewriteFn(f, &plans, &mut errors),
|
|
151
151
|
Item::Const(c) => mapExprsIn(&mut c.value, &mut |e| rewriteCall(e, &plans, &mut errors)),
|
|
152
|
+
// A field default that builds such a record (`held: Held = Held()`): the
|
|
153
|
+
// constructions already have it filled in, and the checker still reads it.
|
|
154
|
+
Item::Enum(e) => {
|
|
155
|
+
for d in e.variants.iter_mut().flat_map(|v| v.field_defaults.iter_mut().flatten()) {
|
|
156
|
+
mapExprsIn(d, &mut |e| rewriteCall(e, &plans, &mut errors));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
152
159
|
_ => {}
|
|
153
160
|
}
|
|
154
161
|
}
|
docs/backlog.md
CHANGED
|
@@ -29,9 +29,8 @@ The retained scene tree was replaced by master's screen view in functional form:
|
|
|
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** 36 of Orb Run's 56 spreads. Reasons, by count: a value rebound by a pattern (8), the receiver read again after a method call (5), a shared value (4), a field passed on without being written back (4), an argument passed from inside a closure (4).
|
|
31
31
|
- **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` takes another 20% of instructions off paint and update both, but inlined frames drop out of a crash's `in f (called from g)` (`crash_in_a_loop_names_the_function_not_its_lifted_loop` fails), so it is off; still made each frame: the view's list (spreads upcast and copied) and each item's own list; `List.each`/`map` with a capturing closure is still slow everywhere else (`Views.stepIn`, `drawnIn`).
|
|
32
|
-
- **Static functions are slower than free functions** (0.78 s against 0.57 s on `benchmarks/static_calls`).
|
|
33
32
|
- **Which closures are inlined depends on the whole program**: closure specialisation copies a callee for at most 8 closures program-wide (`closure_spec::MAX_COPIES`), in the order it meets them, so a closure-taking walk added anywhere (std's `List.filter`, `any`) can turn another call of the same walk back into a closure call (`call_indirect`, slower). In-place updates no longer depend on it (U1). A cap per caller, or copies ranked by how hot the caller is, would make it stable.
|
|
34
|
-
- **Smaller codegen items**: match arms test then cast (`br_on_cast` would do one check); the inliner skips leaves that use their locals (`unbox.*`);
|
|
33
|
+
- **Smaller codegen items**: match arms test then cast (`br_on_cast` would do one check); the inliner skips leaves that use their locals (`unbox.*`); purity treats every closure-taking function as impure, so pipelines fuse only after closure specialisation.
|
|
35
34
|
- No evidence either way, so not planned: unchecked reads in `for` over a `Slice` (checked `at` showed no cost).
|
|
36
35
|
|
|
37
36
|
## Bugs
|
docs/game.md
CHANGED
|
@@ -323,7 +323,7 @@ Three files of `std/game` talk to the host. Each host call takes `io` first.
|
|
|
323
323
|
| | What it gives you |
|
|
324
324
|
|---|---|
|
|
325
325
|
| `InputEvent` | `PointerDown(x, y)`, `PointerUp`, `PointerMove` (the left button or a finger, in window pixels), `ButtonDown(button, x, y)`, `ButtonUp(..)` (the right and middle buttons), `KeyDown(key)`, `KeyUp(key)`, `Wheel(dy)` (a turn of the wheel) |
|
|
326
|
-
| `held.isKeyDown(k)` | whether a key is down, with `Key.Space`, `Key.KeyA`, `Key.Up`, `Key.Enter`, `Key.BracketLeft`, `Key.RightShift`, `Key.LeftAlt`, `Key.LeftSuper` (Cmd, or the Windows key), ... |
|
|
326
|
+
| `held.isKeyDown(k)` | whether a key is down, with `Key.Space`, `Key.KeyA`, `Key.Up`, `Key.Enter`, `Key.BracketLeft`, `Key.RightShift`, `Key.LeftAlt`, `Key.LeftSuper` (Cmd, or the Windows key), ... (a `Held` keeps its keys as bits as well as in `keys`, so this is a bit test, and only a code that is no `Key` looks through the list) |
|
|
327
327
|
| `held.axis(low, high)` | -1, 0 or 1: the key `high` held against the key `low`, 0 when both or neither are (`h.axis(Key.Left, Key.Right)`) |
|
|
328
328
|
| `held.isButtonDown(b)`, `held.mouse` | a mouse button held: `Mouse.Left` (also a finger), `Mouse.Right`, `Mouse.Middle` |
|
|
329
329
|
| `held.pointer` | where the pointer is, in window pixels |
|
|
@@ -399,7 +399,7 @@ The list is a list literal made again each tick, so an item is there only while
|
|
|
399
399
|
2. **`OnHeld`**: an action for what is held after the events, every tick, events or none.
|
|
400
400
|
3. **`OnFrame`**: the frame action, last, which steps the actors (`stepped(dt)`) and runs the rules on what has moved.
|
|
401
401
|
|
|
402
|
-
Several subscriptions may answer one event, each in the order listed. Then the transition in progress moves on, and the game does the commands.
|
|
402
|
+
Several subscriptions may answer one event, each in the order listed. Then the transition in progress moves on, and the game does the commands. The `OnHeld` and `OnFrame` subscriptions are walked as they are listed and each one's action stepped as it is made, with no list of their actions in between, and a tick with no events goes straight to them.
|
|
403
403
|
|
|
404
404
|
**What follows from an action** is part of what `update` returns, `Next<S, A>(state: S, then: List<A> = Nil, cmds: List<Cmd> = Nil)`. Each action std delivers is stepped as it is made, and then each action of its `then`, in order, each with its own `then` first; so the next action sees what they did. Orbit Runner's rules return what happened as actions, one for each thing that met the ship: its frame is `Next(state: moved, then: moved.rules())`, and
|
|
405
405
|
|
std/game/Game.plum
CHANGED
|
@@ -190,7 +190,10 @@ struct Game(screens: List<Screen>) =
|
|
|
190
190
|
else
|
|
191
191
|
ran := self.run(io, input, dt, pixels, false)
|
|
192
192
|
user := ran.acted && ran.travel.isEmpty()
|
|
193
|
+
if ran.history.isNone() && ran.travel.isEmpty()
|
|
194
|
+
Game(..ran, liveHeld: input.held)
|
|
195
|
+
else
|
|
193
|
-
|
|
196
|
+
Game(..ran, liveHeld: input.held).noted(HistoryTick(input: input, pixels: pixels, dt: dt, user: user)).travelAsked(io)
|
|
194
197
|
|
|
195
198
|
# `f`, an action stepped into the shown screen outside a tick (std/game/TestRig's
|
|
196
199
|
# `dispatch`), done as a tick's are: the screen it gives kept, its commands done, then
|
std/game/Input.plum
CHANGED
|
@@ -221,21 +221,56 @@ enum InputEvent =
|
|
|
221
221
|
# keys that are down, the other mouse buttons that are down (`Mouse.Right`,
|
|
222
222
|
# `Mouse.Middle`), and where the pointer is, in screen pixels; nothing unless given
|
|
223
223
|
# (`Held()`, `Held(keys: keys)`). A slice's `OnHeld` (std/game/ScreenSlice) maps it.
|
|
224
|
+
#
|
|
225
|
+
# The keys are also kept as bits, made once as the value is made (`init`): `bits` has a
|
|
226
|
+
# bit for each `Key` that is down (`slot`), and its top bit says whether a key that is not
|
|
227
|
+
# a `Key` is down, which only then is looked for in `keys`.
|
|
224
228
|
struct Held(
|
|
225
229
|
mouse: Bool = false, keys: List<Int> = Nil,
|
|
226
230
|
buttons: List<Int> = Nil, pointer: Vec2 = Vec2(x: 0.0, y: 0.0)
|
|
227
231
|
) =
|
|
232
|
+
fun init(self) =
|
|
233
|
+
self.bits: Int = Held.bitsOf(self.keys)
|
|
234
|
+
|
|
235
|
+
# The bit of the key `k` in `bits`: one of 56 for a `Key` (in key-code order), the top
|
|
236
|
+
# one for any other code.
|
|
237
|
+
fun bit(k: Int) -> Int =
|
|
238
|
+
s := Held.slot(k)
|
|
239
|
+
if s >= 0 then 1 << s else 1 << 63
|
|
240
|
+
|
|
241
|
+
# The `Key` code `k` numbered from 0 to 55 in key-code order, -1 for another code.
|
|
242
|
+
fun slot(k: Int) -> Int =
|
|
243
|
+
if k < 32
|
|
244
|
+
-1
|
|
245
|
+
else if k < 94
|
|
246
|
+
if k == 32 then 0 else if k >= 48 && k <= 57 then k - 47 else if k >= 65 then k - 54 else -1
|
|
247
|
+
else if k >= 256 && k <= 259
|
|
248
|
+
k - 216
|
|
249
|
+
else if k >= 262 && k <= 265
|
|
250
|
+
k - 218
|
|
251
|
+
else if k >= 340 && k <= 347
|
|
252
|
+
k - 292
|
|
253
|
+
else
|
|
254
|
+
-1
|
|
255
|
+
|
|
256
|
+
fun bitsOf(keys: List<Int>) -> Int = match keys
|
|
257
|
+
Nil => 0
|
|
258
|
+
Cons(k, rest) => Held.bit(k) | Held.bitsOf(rest)
|
|
259
|
+
|
|
228
260
|
# Whether `o` holds the same: the mouse buttons, and the same keys in any order (where
|
|
229
261
|
# the pointer is aside).
|
|
230
262
|
fun same(self, o: Held) -> Bool =
|
|
231
|
-
self.mouse == o.mouse && self.
|
|
263
|
+
self.mouse == o.mouse && {self.bits & o.bits} == self.bits && self.keys.length() == o.keys.length()
|
|
264
|
+
&& {self.bits >= 0 || self.keys.all(|k| o.isKeyDown(k))}
|
|
232
265
|
&& self.buttons.length() == o.buttons.length() && self.buttons.all(|b| o.isButtonDown(b))
|
|
233
266
|
|
|
234
267
|
# Whether the key `key` (a `Key` code) is down.
|
|
235
|
-
fun isKeyDown(self, key: Int) -> Bool =
|
|
268
|
+
fun isKeyDown(self, key: Int) -> Bool =
|
|
269
|
+
s := Held.slot(key)
|
|
270
|
+
if s >= 0 then {{self.bits >> s} & 1} == 1 else self.bits < 0 && self.keys.any(|k| k == key)
|
|
236
271
|
|
|
237
272
|
# Whether anything is held: a mouse button, a touch or a key.
|
|
238
|
-
fun any(self) -> Bool = self.mouse ||
|
|
273
|
+
fun any(self) -> Bool = self.mouse || self.bits != 0 || !self.buttons.isEmpty()
|
|
239
274
|
|
|
240
275
|
# -1, 0 or 1: the key `high` held against the key `low`, 0 when both or neither are.
|
|
241
276
|
# `held.axis(Key.Left, Key.Right)` is the horizontal steering of a ship.
|
|
@@ -250,8 +285,8 @@ struct Held(
|
|
|
250
285
|
# sets `mouse`, a ButtonDown or ButtonUp adds or removes its button, and each pointer
|
|
251
286
|
# event moves `pointer`; the wheel holds nothing.
|
|
252
287
|
fun after(self, e: InputEvent) -> Held = match e
|
|
253
|
-
KeyDown(key) => if self.isKeyDown(key) then self else Held(
|
|
288
|
+
KeyDown(key) => if self.isKeyDown(key) then self else Held(mouse: self.mouse, keys: self.keys.append([key]), buttons: self.buttons, pointer: self.pointer)
|
|
254
|
-
KeyUp(key) => Held(
|
|
289
|
+
KeyUp(key) => Held(mouse: self.mouse, keys: self.keys.filter(|k| k != key), buttons: self.buttons, pointer: self.pointer)
|
|
255
290
|
PointerDown(x, y) => Held(..self, mouse: true, pointer: Vec2(x: x, y: y))
|
|
256
291
|
PointerUp(x, y) => Held(..self, mouse: false, pointer: Vec2(x: x, y: y))
|
|
257
292
|
PointerMove(x, y) => Held(..self, pointer: Vec2(x: x, y: y))
|
std/game/ScreenSlice.plum
CHANGED
|
@@ -202,22 +202,48 @@ struct Delivery(
|
|
|
202
202
|
# and the frame over the slice, through its `subscriptions()`, read once from `s`. The
|
|
203
203
|
# game keeps `s` (its screens, and the ones a rewind starts from), so the fold steps a
|
|
204
204
|
# copy of it (`@copy`, one per tick delivered): the copy is the fold's alone, and each
|
|
205
|
-
# step may update it in place.
|
|
205
|
+
# step may update it in place. A tick with no events goes straight to what is held.
|
|
206
206
|
fun deliverTo<A, S: ScreenSlice<A>>(s: S, d: Delivery) -> Delivered =
|
|
207
207
|
subs := s.subscriptions()
|
|
208
208
|
none: Trail<A> := Trail()
|
|
209
|
+
if d.input.events.isEmpty()
|
|
210
|
+
d.heldInto<A, S>(@copy(s), subs, none)
|
|
211
|
+
else
|
|
209
|
-
|
|
212
|
+
afterEvents := d.eventsInto<A, S>(@copy(s), subs, none, d.input.events)
|
|
210
|
-
|
|
213
|
+
match afterEvents
|
|
211
|
-
|
|
214
|
+
Folded(cur, trail) => d.heldInto<A, S>(cur, subs, trail)
|
|
212
215
|
|
|
213
|
-
# The slice `s` after what is held and the frame, delivered
|
|
216
|
+
# The slice `s` after what is held and the frame, delivered: each `OnHeld` of `subs`
|
|
217
|
+
# stepped in order (the first marks the tick a user action when what is held differs from
|
|
218
|
+
# `before`), then each `OnFrame`. The subscriptions are walked as they are, with no list
|
|
219
|
+
# of their actions made.
|
|
214
220
|
fun heldInto<A, S: ScreenSlice<A>>(self, s: S, subs: List<Sub<A>>, trail: Trail<A>) -> Delivered =
|
|
215
|
-
h := self.input.held
|
|
216
|
-
|
|
221
|
+
afterHeld := self.heldIn<A, S>(s, trail, subs, false)
|
|
217
|
-
afterHeld := if held.isEmpty() then Folded(state: s, trail: trail) else Delivery.actAll<A, S>(s, trail.user(!self.before.same(h)), held, 0)
|
|
218
|
-
dt := self.dt
|
|
219
222
|
match afterHeld
|
|
220
|
-
Folded(cur, heldTrail) => Delivery.done<A, S>(Delivery.
|
|
223
|
+
Folded(cur, heldTrail) => Delivery.done<A, S>(Delivery.frameIn<A, S>(cur, heldTrail, subs, self.dt))
|
|
224
|
+
|
|
225
|
+
# The slice `s` with the action of each `OnHeld` of `subs` stepped in, in order; `marked`
|
|
226
|
+
# once the first has marked the trail.
|
|
227
|
+
fun heldIn<A, S: ScreenSlice<A>>(self, s: S, trail: Trail<A>, subs: List<Sub<A>>, marked: Bool) -> Folded<S, A> = match subs
|
|
228
|
+
Nil => Folded(state: s, trail: trail)
|
|
229
|
+
Cons(sub, rest) => match sub
|
|
230
|
+
OnHeld(act) =>
|
|
231
|
+
h := self.input.held
|
|
232
|
+
f := if marked then Delivery.actIn<A, S>(s, trail, act(h), 0) else Delivery.actIn<A, S>(s, trail.user(!self.before.same(h)), act(h), 0)
|
|
233
|
+
match f
|
|
234
|
+
Folded(next, after) => self.heldIn<A, S>(next, after, rest, true)
|
|
235
|
+
_ => self.heldIn<A, S>(s, trail, rest, marked)
|
|
236
|
+
|
|
237
|
+
# The slice `s` with the action of each `OnFrame` of `subs` for a frame of `dt` seconds
|
|
238
|
+
# stepped in, in order.
|
|
239
|
+
fun frameIn<A, S: ScreenSlice<A>>(s: S, trail: Trail<A>, subs: List<Sub<A>>, dt: Float) -> Folded<S, A> = match subs
|
|
240
|
+
Nil => Folded(state: s, trail: trail)
|
|
241
|
+
Cons(sub, rest) => match sub
|
|
242
|
+
OnFrame(act) =>
|
|
243
|
+
f := Delivery.actIn<A, S>(s, trail, act(dt), 0)
|
|
244
|
+
match f
|
|
245
|
+
Folded(next, after) => Delivery.frameIn<A, S>(next, after, rest, dt)
|
|
246
|
+
_ => Delivery.frameIn<A, S>(s, trail, rest, dt)
|
|
221
247
|
|
|
222
248
|
# The slice `s` after the tick's events `es`.
|
|
223
249
|
fun eventsInto<A, S: ScreenSlice<A>>(self, s: S, subs: List<Sub<A>>, trail: Trail<A>, es: List<InputEvent>) -> Folded<S, A> = match es
|
|
@@ -272,7 +298,7 @@ struct Delivery(
|
|
|
272
298
|
n := s.update(a)
|
|
273
299
|
match n
|
|
274
300
|
Next(next, then, cmds) =>
|
|
275
|
-
after := Trail(..trail, actions: Cons(a, trail.actions), cmds: cmds.fold(trail.cmds, |acc, c| Cons(c, acc)))
|
|
301
|
+
after := Trail(..trail, actions: Cons(a, trail.actions), cmds: if cmds.isEmpty() then trail.cmds else cmds.fold(trail.cmds, |acc, c| Cons(c, acc)))
|
|
276
302
|
Delivery.actAll<A, S>(next, after, then, depth + 1)
|
|
277
303
|
|
|
278
304
|
# The slice `s` with each of `xs` stepped in, in order, each `depth` `then`s deep.
|
tests/std/game/Input.plum
CHANGED
|
@@ -109,6 +109,22 @@ primitive InputTest[UnitTest] =
|
|
|
109
109
|
assert i([Key.Left, Key.Right]).axis(Key.Left, Key.Right) == 0.0
|
|
110
110
|
assert i(Nil).axis(Key.Left, Key.Right) == 0.0
|
|
111
111
|
|
|
112
|
+
# the keys are kept as bits as well as listed: every `Key`, the codes beside them and
|
|
113
|
+
# codes far from them read the same as the list
|
|
114
|
+
fun heldKeysReadAsTheList() =
|
|
115
|
+
codes := [0, 31, 32, 33, 47, 48, 57, 58, 64, 65, 93, 94, 255, 256, 259, 260, 261, 262, 265, 266, 339, 340, 347, 348, 1000, -1, Int.Min, Int.Max]
|
|
116
|
+
assert codes.all(|k| Held(keys: [k]).isKeyDown(k))
|
|
117
|
+
assert codes.all(|k| !Held(keys: codes.filter(|o| o != k)).isKeyDown(k))
|
|
118
|
+
assert InputEvent.keyCodes().all(|k| Held(keys: [k]).isKeyDown(k) && !Held().isKeyDown(k))
|
|
119
|
+
assert Held(keys: [1000, Key.KeyA]).same(Held(keys: [Key.KeyA, 1000]))
|
|
120
|
+
assert !Held(keys: [1000, Key.KeyA]).same(Held(keys: [Key.KeyA, 1001]))
|
|
121
|
+
assert !Held(keys: [Key.Up]).same(Held(keys: [Key.Down]))
|
|
122
|
+
assert !Held(keys: [Key.Up]).same(Held(keys: [Key.Up, Key.Down]))
|
|
123
|
+
assert Held(keys: [Key.Up, Key.Up]).same(Held(keys: [Key.Up, Key.Down]))
|
|
124
|
+
up := Held(keys: [Key.KeyA, 1000, Key.Left]).after(KeyUp(key: 1000)).after(KeyUp(key: Key.Left))
|
|
125
|
+
assert up.isKeyDown(Key.KeyA) && !up.isKeyDown(1000) && !up.isKeyDown(Key.Left) && up.same(Held(keys: [Key.KeyA]))
|
|
126
|
+
assert Held().after(KeyDown(key: 1000)).isKeyDown(1000) && Held().after(KeyDown(key: 1000)).any()
|
|
127
|
+
|
|
112
128
|
fun keyAndMouseConstantsUseGLFWNumbering() =
|
|
113
129
|
assert Key.KeyW == 87
|
|
114
130
|
assert Key.Left == 263
|