plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
b8b2462
— Peter John
2026-11-16T19:16:53+05:30
U1: a closure handed to a fn that never lets it escape no longer counts as keeping what it captures, so in-place updates don't depend on which closures closure specialisation copied. It copies a callee for at most 8 closures program-wide (`closure_spec::MAX_COPIES`), in the order it meets them; a `filter`/`any` added to std/game's `Looping` took `List.any`'s last copy from Orbit Runner's `PlayingScreen.rules`, whose closure reads `self`, so `rules` was no longer read-only, `Next(state: moved, then: moved.rules())` no longer moved `moved` in, `Next.state` turned shared and `PlayingScreen.update` lost four of its five in-place spreads (and `stepped` its one). `readOnlyParams` now judges the uses inside such a closure like any other use (`passedClosures`, `An.keepsNoFn` from `closure_escape::nonEscapingFnParams`; not for `@native` bodies or a label two units share). `Looping.without`, `stopsOf` and `holds` are `filter`/`map`/`any` again. In-place counts unchanged for Orbit Runner, Balloon Pop, Clockwork and Menu; Orbit Runner checksum 612522477 kept. Tests: uniqueness_fold aReceiverReadInsideANonEscapingClosureIsNotKept, game_in_place closuresElsewhereDoNotDecideASpread. docs/backlog.md: the delivery fold entry, and a performance entry on the copy cap deciding which closures are inlined.
compiler/cli/tests/cli/game_in_place_test.rs
CHANGED
|
@@ -115,3 +115,40 @@ fn menuKeepsItsInPlaceSpreads() {
|
|
|
115
115
|
want.extend([("Game.obey", 4), ("Game.goTo$unique", 1), ("Body.place", 1)]);
|
|
116
116
|
assertKept("examples/games/menu/Menu.plum", &want);
|
|
117
117
|
}
|
|
118
|
+
|
|
119
|
+
/// Closures handed to one callee elsewhere in the program don't decide whether a spread is in
|
|
120
|
+
/// place (U1). Closure specialisation copies a callee for at most `MAX_COPIES` (8) closures,
|
|
121
|
+
/// program-wide, in the order it meets them; past that a closure stays a closure. Here
|
|
122
|
+
/// `Other.many` takes all of `List.any`'s copies first, so `Board.hit`'s closure, which reads
|
|
123
|
+
/// `self`, stays one. It still keeps nothing (`any` never lets it escape), so `hit` only reads
|
|
124
|
+
/// `self` and both of `step`'s spreads are in place. Before, `hit` counted as keeping `self` and
|
|
125
|
+
/// the second spread copied: a `filter` or `any` added to std/game's `Looping` cost Orbit
|
|
126
|
+
/// Runner's `PlayingScreen.update` four of its five spreads the same way.
|
|
127
|
+
#[test]
|
|
128
|
+
fn closuresElsewhereDoNotDecideASpread() {
|
|
129
|
+
let others: String = (0..12).map(|i| format!(" a{i} := xs.any(|x| x == {i} + k)\n")).collect();
|
|
130
|
+
let any: Vec<String> = (0..12).map(|i| format!("a{i}")).collect();
|
|
131
|
+
let src = format!(
|
|
132
|
+
"import std/Int\nimport std/Io\nimport std/Str\nimport std/App\n\
|
|
133
|
+
primitive Other =\n fun many(xs: List<Int>, k: Int) -> Bool =\n{others} {}\n\n\
|
|
134
|
+
struct Board(xs: List<Int>, n: Int, hits: Int) =\n fun hit(self) -> Bool = self.xs.any(|x| x == self.n)\n\n\
|
|
135
|
+
primitive Main[App] =\n fun step(b: Board) -> Board =\n moved := Board(..b, n: b.n + 1)\n h := moved.hit()\n \
|
|
136
|
+
Board(..moved, hits: if h then moved.hits + 1 else moved.hits)\n\n fun main(io: Io) =\n \
|
|
137
|
+
b := Range.until(0, 5).fold(Board(xs: [1, 2, 3], n: 0, hits: 0), |cur, i| Main.step(cur))\n \
|
|
138
|
+
io.printLn(\"{{b.hits}} {{Other.many([1, 2], 0)}}\")\n",
|
|
139
|
+
any.join(" || ")
|
|
140
|
+
);
|
|
141
|
+
let dir = crate::common::Scratch::new("closures_elsewhere");
|
|
142
|
+
let file = dir.join("cap.plum");
|
|
143
|
+
std::fs::write(&file, &src).unwrap();
|
|
144
|
+
let root = crate::common::root();
|
|
145
|
+
let out = Command::new(crate::common::plumBin())
|
|
146
|
+
.args(["run", "--explain-fusion", file.to_str().unwrap(), "--lib-path", root.to_str().unwrap()])
|
|
147
|
+
.output()
|
|
148
|
+
.expect("run plum");
|
|
149
|
+
let err = String::from_utf8_lossy(&out.stderr).to_string();
|
|
150
|
+
assert!(out.status.success(), "plum run failed:\n{err}");
|
|
151
|
+
assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "3 true");
|
|
152
|
+
let steps = err.lines().filter(|l| l.starts_with("fusion: unique spread at Main.step:")).count();
|
|
153
|
+
assert_eq!(steps, 2, "both of `step`'s spreads in place:\n{}", err.lines().filter(|l| l.contains("Main.step")).collect::<Vec<_>>().join("\n"));
|
|
154
|
+
}
|
compiler/core/src/uniqueness.rs
CHANGED
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
//! names both and says which call blocked the original.
|
|
26
26
|
//!
|
|
27
27
|
//! * A method whose `self` is only read (`ro`: field reads, field writes, calls of other `ro`
|
|
28
|
-
//! methods; never returned, stored, passed on or captured
|
|
28
|
+
//! methods; never returned, stored, passed on or captured, but by a closure handed to a fn
|
|
29
|
+
//! that never lets it escape, `passedClosures`) does not block a receiver. What
|
|
29
30
|
//! it returns of `self`'s fields is the field analysis' business, not the receiver's. A
|
|
30
31
|
//! `match self` counts as a read when no arm builds the matched variant in tail position and
|
|
31
32
|
//! no binder is handed on (`selfMatchReads`): `Map.get` is read-only, `Map.with` is not.
|
|
@@ -514,6 +515,10 @@ struct An {
|
|
|
514
515
|
/// Parameters their fn only reads (`readOnlyParams`): `self` of the methods in `ro`, and any
|
|
515
516
|
/// other parameter that is only read, through calls too.
|
|
516
517
|
roParams: HashSet<(usize, usize)>,
|
|
518
|
+
/// The fn-typed parameters (unit, parameter) their fn never lets escape
|
|
519
|
+
/// (`closure_escape::nonEscapingFnParams`): a closure handed to one is dead when the call
|
|
520
|
+
/// returns, so what it captures is held no longer than the call.
|
|
521
|
+
keepsNoFn: HashSet<(usize, usize)>,
|
|
517
522
|
/// Per unit, the spreads of closures passed to a parameter that is never called (see
|
|
518
523
|
/// `stripDeadFnArgs`): each runs only as a spread of its type in the specialised copies given.
|
|
519
524
|
deadSpreads: Vec<Vec<DeadSpread>>,
|
|
@@ -2502,6 +2507,7 @@ fn build(src: &Source, distrust: &HashSet<String>) -> An {
|
|
|
2502
2507
|
spreads: vec![],
|
|
2503
2508
|
ro: HashSet::new(),
|
|
2504
2509
|
roParams: HashSet::new(),
|
|
2510
|
+
keepsNoFn: HashSet::new(),
|
|
2505
2511
|
deadSpreads: vec![],
|
|
2506
2512
|
arrayFresh: HashSet::new(),
|
|
2507
2513
|
live: vec![],
|
|
@@ -2530,6 +2536,7 @@ fn build(src: &Source, distrust: &HashSet<String>) -> An {
|
|
|
2530
2536
|
_ => {}
|
|
2531
2537
|
}
|
|
2532
2538
|
}
|
|
2539
|
+
an.keepsNoFn = keepsNoFn(&an, src);
|
|
2533
2540
|
an.roParams = crate::timing::time("ro params", || readOnlyParams(&an));
|
|
2534
2541
|
an.ro = readOnlySelf(&an, &an.roParams);
|
|
2535
2542
|
an.reuseCands = reuseCandidates(&an);
|
|
@@ -3532,6 +3539,68 @@ fn selfMatchReads(an: &An, ro: &HashSet<usize>, c: &Case, k: usize) -> bool {
|
|
|
3532
3539
|
!handed
|
|
3533
3540
|
}
|
|
3534
3541
|
|
|
3542
|
+
/// `An.keepsNoFn`: the non-escaping fn-typed parameters of the units with a body of their own. A
|
|
3543
|
+
/// label two units share (same-named free fns) is left out, as is a body the compiler or the
|
|
3544
|
+
/// host supplies (`@native`), which may keep anything it is given.
|
|
3545
|
+
fn keepsNoFn(an: &An, src: &Source) -> HashSet<(usize, usize)> {
|
|
3546
|
+
let ne = crate::closure_escape::nonEscapingFnParams(src);
|
|
3547
|
+
let mut labels: HashMap<&str, usize> = HashMap::new();
|
|
3548
|
+
for u in &an.units {
|
|
3549
|
+
*labels.entry(u.label.as_str()).or_default() += 1;
|
|
3550
|
+
}
|
|
3551
|
+
let mut out = HashSet::new();
|
|
3552
|
+
for (g, u) in an.units.iter().enumerate() {
|
|
3553
|
+
let opaque = u.block.stmts.is_empty() || u.block.stmts.iter().any(|s| matches!(s.kind, StmtKind::Native));
|
|
3554
|
+
if u.kind != Kind::Fn || opaque || labels[u.label.as_str()] != 1 {
|
|
3555
|
+
continue;
|
|
3556
|
+
}
|
|
3557
|
+
let shift = usize::from(u.hasSelf);
|
|
3558
|
+
for &j in ne.get(&u.label).into_iter().flatten() {
|
|
3559
|
+
if u.params.get(j + shift).is_some_and(|p| !p.plain) {
|
|
3560
|
+
out.insert((g, j + shift));
|
|
3561
|
+
}
|
|
3562
|
+
}
|
|
3563
|
+
}
|
|
3564
|
+
out
|
|
3565
|
+
}
|
|
3566
|
+
|
|
3567
|
+
/// The closures in `b` that are a direct argument of a call whose every target takes them into a
|
|
3568
|
+
/// fn-typed parameter it never lets escape (`An.keepsNoFn`): they are dead once the call returns,
|
|
3569
|
+
/// so a variable they capture is held no longer than that, and their uses of it are judged as
|
|
3570
|
+
/// uses in place (`list.any(|m| self.ship.touches(m))` only reads `self`). Whether closure
|
|
3571
|
+
/// specialisation made a copy for the call (at most `closure_spec::MAX_COPIES` per callee, in
|
|
3572
|
+
/// whatever order the program's calls come) then changes nothing here.
|
|
3573
|
+
fn passedClosures(an: &An, b: &Block) -> HashSet<NodeId> {
|
|
3574
|
+
let mut out = HashSet::new();
|
|
3575
|
+
walkBlock(b, &mut |n| {
|
|
3576
|
+
if let Node::E(e) = n {
|
|
3577
|
+
let args: Vec<&Expr> = match &e.kind {
|
|
3578
|
+
ExprKind::FnCall(c) => c.args.iter().map(argExpr).collect(),
|
|
3579
|
+
ExprKind::Attribute(a) => match &a.attr {
|
|
3580
|
+
AttrKind::Method(c) => c.args.iter().map(argExpr).collect(),
|
|
3581
|
+
_ => vec![],
|
|
3582
|
+
},
|
|
3583
|
+
_ => vec![],
|
|
3584
|
+
};
|
|
3585
|
+
let closures: Vec<&Expr> = args.into_iter().filter(|x| matches!(x.kind, ExprKind::Closure(_))).collect();
|
|
3586
|
+
if !closures.is_empty() {
|
|
3587
|
+
if let Some(targets) = an.callTargets(e) {
|
|
3588
|
+
for x in closures {
|
|
3589
|
+
let kept = targets.iter().all(|(t, given)| {
|
|
3590
|
+
given.iter().enumerate().any(|(i, a)| a.is_some_and(|a| a.id == x.id) && an.keepsNoFn.contains(&(*t, i)))
|
|
3591
|
+
});
|
|
3592
|
+
if kept {
|
|
3593
|
+
out.insert(x.id);
|
|
3594
|
+
}
|
|
3595
|
+
}
|
|
3596
|
+
}
|
|
3597
|
+
}
|
|
3598
|
+
}
|
|
3599
|
+
true
|
|
3600
|
+
});
|
|
3601
|
+
out
|
|
3602
|
+
}
|
|
3603
|
+
|
|
3535
3604
|
/// The methods whose `self` is only read (see `An.ro`), out of `readOnlyParams`.
|
|
3536
3605
|
fn readOnlySelf(an: &An, params: &HashSet<(usize, usize)>) -> HashSet<usize> {
|
|
3537
3606
|
(0..an.units.len()).filter(|&g| an.units[g].kind == Kind::Fn && an.units[g].method && an.units[g].hasSelf && params.contains(&(g, 0))).collect()
|
|
@@ -3541,7 +3610,9 @@ fn readOnlySelf(an: &An, params: &HashSet<(usize, usize)>) -> HashSet<usize> {
|
|
|
3541
3610
|
/// use is the object of a field read or a field write, the receiver of methods whose `self` is
|
|
3542
3611
|
/// only read, a `match` that only reads it (`selfMatchReads`), or a bare argument of a direct call
|
|
3543
3612
|
/// whose every target only reads that parameter too. Never returned, stored, bound to another
|
|
3544
|
-
/// name, rebound or captured by a closure
|
|
3613
|
+
/// name, rebound or captured by a closure (but one handed to a fn that never lets it escape,
|
|
3614
|
+
/// whose uses are judged like any other: `passedClosures`). A greatest fixpoint, since fns call
|
|
3615
|
+
/// each other. A call
|
|
3545
3616
|
/// that hands such a parameter a unique value leaves it unique: nothing the callee does keeps it.
|
|
3546
3617
|
/// (Fusion turns a closure's captured `self` into an argument of the specialised copy, so a
|
|
3547
3618
|
/// method like a screen's `view`, which maps over a range with a closure reading `self.clock`,
|
|
@@ -3619,6 +3690,7 @@ fn paramOnlyRead(an: &An, ro: &HashSet<(usize, usize)>, selfRo: &HashSet<usize>,
|
|
|
3619
3690
|
let mut allowed: HashSet<NodeId> = HashSet::new();
|
|
3620
3691
|
let mut captured: HashSet<NodeId> = HashSet::new();
|
|
3621
3692
|
let mut rebound = false;
|
|
3693
|
+
let passed = passedClosures(an, &u.block);
|
|
3622
3694
|
// `self` keeps `selfMatchReads` as it was; another parameter's match is a read only when
|
|
3623
3695
|
// every binder is only read too: one whose field leaves the arm (into a constructor, as a
|
|
3624
3696
|
// result) takes the parameter apart, which its callers may want to hand over uniquely.
|
|
@@ -3658,6 +3730,9 @@ fn paramOnlyRead(an: &An, ro: &HashSet<(usize, usize)>, selfRo: &HashSet<usize>,
|
|
|
3658
3730
|
}
|
|
3659
3731
|
}
|
|
3660
3732
|
},
|
|
3733
|
+
ExprKind::Closure(cl) if passed.contains(&e.id) => {
|
|
3734
|
+
rebound |= cl.params.iter().any(|p| p == name);
|
|
3735
|
+
}
|
|
3661
3736
|
ExprKind::Closure(cl) => {
|
|
3662
3737
|
rebound |= cl.params.iter().any(|p| p == name);
|
|
3663
3738
|
walkExpr(e, &mut |m| {
|
compiler/core/tests/core/uniqueness_fold_test.rs
CHANGED
|
@@ -121,3 +121,31 @@ fn aShadowingClosureParamOrLoopBinderIsNotTheBoundedParam() {
|
|
|
121
121
|
let looped = bounds(" fun go<S: Tr>(s: S, ps: List<Pt>) -> Int =\n n := 0\n for s := range ps\n n = n + s.x\n n\n");
|
|
122
122
|
assert!(looped.is_empty(), "{looped:?}");
|
|
123
123
|
}
|
|
124
|
+
|
|
125
|
+
/// `t.look()` where `look` reads `self` only inside a closure handed to a fn that never lets the
|
|
126
|
+
/// closure escape (`Main.count`): the closure is dead once the call returns, so nothing keeps
|
|
127
|
+
/// `t` and the spread after it is in place, whether or not closure specialisation copied
|
|
128
|
+
/// `count` for it (at most `MAX_COPIES` per callee program-wide, so an unrelated closure handed
|
|
129
|
+
/// to the same callee elsewhere used to decide it; U1, std/game's `Looping`). A callee that
|
|
130
|
+
/// stores the closure keeps `t`, and one the compiler supplies (`@native`) may.
|
|
131
|
+
#[test]
|
|
132
|
+
fn aReceiverReadInsideANonEscapingClosureIsNotKept() {
|
|
133
|
+
let go = "\n fun go() -> Pt =\n t := Pt(x: 0, y: 0)\n n := t.look()\n Pt(..t, y: n)\n";
|
|
134
|
+
let r = run(&format!(
|
|
135
|
+
"struct Pt(x: Int, y: Int) =\n fun look(self) -> Int = Main.count(|k| k + self.x)\nprimitive Main =\n fun count(f: fn(Int) -> Int) -> Int = f(1)\n{go}"
|
|
136
|
+
));
|
|
137
|
+
assert!(proved(&r, "Main.go:6"), "{r:?}");
|
|
138
|
+
let r = run(&format!(
|
|
139
|
+
"struct Pt(x: Int, y: Int) =\n fun look(self) -> Int = Main.count(|k| k + self.x).n\nstruct Held(n: Int, f: fn(Int) -> Int)\nprimitive Main =\n fun count(f: fn(Int) -> Int) -> Held = Held(n: f(1), f: f)\n{go}"
|
|
140
|
+
));
|
|
141
|
+
assert!(!proved(&r, "Main.go:7"), "{r:?}");
|
|
142
|
+
let r = run(&format!(
|
|
143
|
+
"struct Pt(x: Int, y: Int) =\n fun look(self) -> Int = Main.count(|k| k + self.x)\nprimitive Main =\n fun count(f: fn(Int) -> Int) -> Int = @native\n{go}"
|
|
144
|
+
));
|
|
145
|
+
assert!(!proved(&r, "Main.go:6"), "{r:?}");
|
|
146
|
+
// The closure hands `self` on whole: kept, though the closure itself is not.
|
|
147
|
+
let r = run(&format!(
|
|
148
|
+
"struct Pt(x: Int, y: Int) =\n fun look(self) -> Int = Main.count(|k| Main.keep(self).p.x + k)\nstruct Box(p: Pt)\nprimitive Main =\n fun count(f: fn(Int) -> Int) -> Int = f(1)\n\n fun keep(p: Pt) -> Box = Box(p: p)\n{go}"
|
|
149
|
+
));
|
|
150
|
+
assert!(!proved(&r, "Main.go:9"), "{r:?}");
|
|
151
|
+
}
|
docs/backlog.md
CHANGED
|
@@ -9,7 +9,7 @@ The retained scene tree was replaced by master's screen view in functional form:
|
|
|
9
9
|
- **A closure field bound by the same name in two arms of a `match` can't be called in the second.** `match self` / `One(f) => f(x)` / `Two(f) => f(x)` on `enum E = One(f: fn(Int) -> Int) | Two(f: fn(Int) -> Int)` is "undefined function 'f'" at the second arm's call; naming the binders apart (`Two(g) => g(x)`) works. std/game's `Sub.onEvent` names each arm's binder after its subscription (`turn`, `down`, `up`, `move`).
|
|
10
10
|
- **A `match` on a nullary variant inside a closure in a function body matches every value.** `xs.fold(0, |s, e| match e` with an arm `B => s + 1` counts every element; the same match in a named function, or in a closure in a test, is right, and a variant with fields (`A(n) =>`) is right. std/game works around it (the matches are in methods: `Game.travelled`, `Game.redone`). Repro: `fun count(xs: List<E>) -> Int = xs.fold(0, |s, e| match e` / `B => s + 1` / `_ => s)` with `enum E = A(n: Int) | B | C`, `count([C])` gives 1.
|
|
11
11
|
- **Pass re-check failures met on the way** (fixed in std, not in the compiler): fusion devirtualising `s.frames().onDemand()` on a `Screen` with one implementer mistypes the fused call (std matches on `frames()` instead); dead-code removal is reverted when it removes every type a downcast to a trait could match (it met `Container as c`, which master has since folded into `view` too).
|
|
12
|
-
- **What std's delivery fold still copies** (the screen is in place since 2026-11-16, `@copy` in `Delivery.deliverTo`; since the Elm-style screens, Orbit Runner's `PlayingScreen.update` 5 (steer, the frame's spread after `stepped`, scored, hit and start over, whose `best` is read before the spread now), `rules`, `deal`, `grown`, `at` and the derived `stepped`: 29 spreads in place, 27 before; a call on a receiver of a bounded type parameter, `s.update(a)` with `S: ScreenSlice<A>`, is stamped with the bound in the analysis' copy of the body and resolves among its implementers, where it resolved to every `update` of the program, `Game.update` and `Viewport.update` among them): an update that reads `self` again after a spread of it copies (Orbit Runner reads its sounds first, `sounds := self.assets`); a screen fold whose result `passes` can't follow (`Range.fold` over the screen; Orbit Runner's `deal` folds a `Rolled<List<Orb>>` instead); Balloon Pop's `update`, one spread in place, the others copies, as its `view` keeps `self` (`s.view()` in `eventInto` then shares the copy) and its frame action spreads inside a fold's closure; an orb's and a mine's `step` (R3: one spread of `self`, the head of the list it is stepped in, `List.head`, which std's read-only walks `List.all`, `filter` and `next` hand to closures and results, and `Ticker.input` binds out of a field read; the screen's `mines` and `orbs` are also handed to the view's upcasts). The proof the fold needed: read-only parameters through calls (`readOnlyParams`: a screen's `view` hands `self` to the fused copies of its closures), a field taken out of a fresh value (rechecked by `analyse` when the field turns out shared), a value stored beside reads of itself (`Next(state: self, then: [if self.over ..])`), a local used once standing for its value in `passes`. The cost: the in-place pass takes about 2.5 times as long (debug, Orbit Runner 183 to 480 ms): 9 version rounds where there were 5, each about 30% dearer.
|
|
12
|
+
- **What std's delivery fold still copies** (the screen is in place since 2026-11-16, `@copy` in `Delivery.deliverTo`; since the Elm-style screens, Orbit Runner's `PlayingScreen.update` 5 (steer, the frame's spread after `stepped`, scored, hit and start over, whose `best` is read before the spread now), `rules`, `deal`, `grown`, `at` and the derived `stepped`: 29 spreads in place, 27 before; a call on a receiver of a bounded type parameter, `s.update(a)` with `S: ScreenSlice<A>`, is stamped with the bound in the analysis' copy of the body and resolves among its implementers, where it resolved to every `update` of the program, `Game.update` and `Viewport.update` among them): an update that reads `self` again after a spread of it copies (Orbit Runner reads its sounds first, `sounds := self.assets`); a screen fold whose result `passes` can't follow (`Range.fold` over the screen; Orbit Runner's `deal` folds a `Rolled<List<Orb>>` instead); Balloon Pop's `update`, one spread in place, the others copies, as its `view` keeps `self` (`s.view()` in `eventInto` then shares the copy) and its frame action spreads inside a fold's closure; an orb's and a mine's `step` (R3: one spread of `self`, the head of the list it is stepped in, `List.head`, which std's read-only walks `List.all`, `filter` and `next` hand to closures and results, and `Ticker.input` binds out of a field read; the screen's `mines` and `orbs` are also handed to the view's upcasts). The proof the fold needed: read-only parameters through calls (`readOnlyParams`: a screen's `view` hands `self` to the fused copies of its closures), a field taken out of a fresh value (rechecked by `analyse` when the field turns out shared), a value stored beside reads of itself (`Next(state: self, then: [if self.over ..])`), a local used once standing for its value in `passes`. Since U1 (2026-11-16) a parameter read only inside a closure handed to a fn that never lets the closure escape is only read (`readOnlyParams` through `An.keepsNoFn`), whether or not closure specialisation made a copy of the callee for that closure. It makes at most 8 (`closure_spec::MAX_COPIES`) per callee, program-wide, in the order it meets them, so a `filter` or `any` added to std's `Looping` took the last copy of `List.any` from `PlayingScreen.rules`, whose closure reads `self`, and cost `PlayingScreen.update` four of its five in-place spreads (`Looping` uses `filter` and `any` again). The cost: the in-place pass takes about 2.5 times as long (debug, Orbit Runner 183 to 480 ms): 9 version rounds where there were 5, each about 30% dearer.
|
|
13
13
|
- **A program with no `Sized` or `Touch` view drops the dead code pass**: std's `Views.touchOf`, `sizedHit` and `missesArea` match `Touch as t` and `Sized as s` on a `View`, and once the pass has dropped every type that is one, the re-check calls each match impossible ("no type is both a View and a Touch"). Every game has such a view, so only a minimal test program meets it. A trait default's copy is told apart since 2026-11-16 (`defaultPositions` reads the trait's declarations), but these are statics.
|
|
14
14
|
- **The view's cost**: each frame makes the view's list and items. `benchmarks/screen_views` (`orbit.plum`, `scale.plum`, master's drivers) times it; not yet run against master. Master's stepping walk makes the view once more a tick and each actor's view; this branch's `stepped` makes neither, so the two `update` timings are not the same work (`scale.plum`'s header says so).
|
|
15
15
|
- **Stepping without the screen naming its actors**: done at compile time on 2026-11-16: every screen, and an actor that lists `Steps` (std/game/View), has its fields holding actors stepped by a derived `stepped` (or after its own `step`); since the Elm-style screens a screen calls it in its frame action (no stepping phase, no `steps()`), one field paused by `@stepsWhen(cond)` (`docs/game.md`, "Stepping"). A `List<View>` field is stepped by `Views.stepIn` since R3 (each item's type tested as it is stepped).
|
|
@@ -31,6 +31,7 @@ The retained scene tree was replaced by master's screen view in functional form:
|
|
|
31
31
|
- **A value struct inside an enum payload is boxed twice** (`Some(Pos)` allocates two objects). No measurement yet.
|
|
32
32
|
- **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).
|
|
33
33
|
- **Static functions are slower than free functions** (0.78 s against 0.57 s on `benchmarks/static_calls`).
|
|
34
|
+
- **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_ref`, 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
35
|
- **Smaller codegen items**: match arms test then cast (`br_on_cast` would do one check); the inliner skips leaves that use their locals (`unbox.*`); every struct has its own empty supertype; purity treats every closure-taking function as impure, so pipelines fuse only after closure specialisation.
|
|
35
36
|
- No evidence either way, so not planned: unchecked reads in `for` over a `Slice` (checked `at` showed no cost).
|
|
36
37
|
|
std/game/Game.plum
CHANGED
|
@@ -593,12 +593,8 @@ struct Looping(sound: Sound, volume: Float) =
|
|
|
593
593
|
Stop(s) => Looping.without(loops, s.id)
|
|
594
594
|
_ => loops
|
|
595
595
|
|
|
596
|
-
# `loops` but the sound `id`'s.
|
|
596
|
+
# `loops` but the sound `id`'s.
|
|
597
|
-
# of a `Looping` list here costs Orbit Runner's `PlayingScreen.update` four of its five
|
|
598
|
-
# in-place spreads (compiler/cli/tests/cli/game_in_place_test.rs).
|
|
599
|
-
fun without(loops: List<Looping>, id: Int) -> List<Looping> =
|
|
597
|
+
fun without(loops: List<Looping>, id: Int) -> List<Looping> = loops.filter(|l| l.sound.id != id)
|
|
600
|
-
Nil => Nil
|
|
601
|
-
Cons(l, rest) => if l.sound.id == id then Looping.without(rest, id) else Cons(l, Looping.without(rest, id))
|
|
602
598
|
|
|
603
599
|
# The commands that take the host from `before` looping to `after`: a `Stop` for each
|
|
604
600
|
# sound that no longer loops, then a `Stop` and a `Loop` for each that loops anew or at
|
|
@@ -606,9 +602,7 @@ struct Looping(sound: Sound, volume: Float) =
|
|
|
606
602
|
fun changes(before: List<Looping>, after: List<Looping>) -> List<Cmd> = Looping.stopsOf(before, after).append(Looping.startsOf(after, before))
|
|
607
603
|
|
|
608
604
|
# A `Stop` for each of `before` that `after` doesn't loop.
|
|
609
|
-
fun stopsOf(before: List<Looping>, after: List<Looping>) -> List<Cmd> =
|
|
605
|
+
fun stopsOf(before: List<Looping>, after: List<Looping>) -> List<Cmd> = before.filter(|b| !Looping.holds(after, b, false)).map(|b| Stop(sound: b.sound))
|
|
610
|
-
Nil => Nil
|
|
611
|
-
Cons(b, rest) => if Looping.holds(after, b, false) then Looping.stopsOf(rest, after) else Cons(Stop(sound: b.sound), Looping.stopsOf(rest, after))
|
|
612
606
|
|
|
613
607
|
# A `Stop` and a `Loop` for each of `after` that `before` doesn't loop at its volume.
|
|
614
608
|
fun startsOf(after: List<Looping>, before: List<Looping>) -> List<Cmd> = match after
|
|
@@ -617,9 +611,7 @@ struct Looping(sound: Sound, volume: Float) =
|
|
|
617
611
|
if Looping.holds(before, a, true) then Looping.startsOf(rest, before) else Cons(Stop(sound: a.sound), Cons(Loop(sound: a.sound, volume: a.volume), Looping.startsOf(rest, before)))
|
|
618
612
|
|
|
619
613
|
# Whether `loops` loops `l`'s sound (at its volume when `sameVolume`).
|
|
620
|
-
fun holds(loops: List<Looping>, l: Looping, sameVolume: Bool) -> Bool =
|
|
614
|
+
fun holds(loops: List<Looping>, l: Looping, sameVolume: Bool) -> Bool = loops.any(|x| x.sound.id == l.sound.id && {!sameVolume || x.volume == l.volume})
|
|
621
|
-
Nil => false
|
|
622
|
-
Cons(x, rest) => {x.sound.id == l.sound.id && {!sameVolume || x.volume == l.volume}} || Looping.holds(rest, l, sameVolume)
|
|
623
615
|
|
|
624
616
|
# A game's ticker: its clock and input between frames: the input not yet given to a tick
|
|
625
617
|
# (`pending`), the wall time not yet ticked (`acc`), the ticks run this frame (`n`) and in
|