plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
f60cbdb
— Peter John
2026-11-16T21:13:35+05:30
U4c: in place: a move per match arm, a hand-over through an inline if, read-only field locals
- compiler/core/src/uniqueness.rs +323 -144
- compiler/core/tests/core/uniqueness_fold_test.rs +41 -1
- docs/backlog.md +3 -3
compiler/core/src/uniqueness.rs
CHANGED
|
@@ -1146,6 +1146,15 @@ impl An {
|
|
|
1146
1146
|
}
|
|
1147
1147
|
_ => {}
|
|
1148
1148
|
}
|
|
1149
|
+
// Handed to a parameter every target only reads (`An.roParams`): a read too
|
|
1150
|
+
// (`Cons(h, t) => if o.isKeyDown(h) ..`, a walk's closure inlined).
|
|
1151
|
+
if let Some(targets) = self.callTargets(e) {
|
|
1152
|
+
for y in callArgs(e).into_iter().filter(|y| isVar(y, b)) {
|
|
1153
|
+
if targets.iter().all(|(t, given)| given.iter().enumerate().any(|(i, a)| a.is_some_and(|a| a.id == y.id) && self.roParams.contains(&(*t, i)))) {
|
|
1154
|
+
reads += 1;
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1149
1158
|
}
|
|
1150
1159
|
!bad
|
|
1151
1160
|
};
|
|
@@ -1157,6 +1166,19 @@ impl An {
|
|
|
1157
1166
|
}
|
|
1158
1167
|
}
|
|
1159
1168
|
|
|
1169
|
+
/// The arguments of call `e` (none for another expression or `@copy`).
|
|
1170
|
+
fn callArgs(e: &Expr) -> Vec<&Expr> {
|
|
1171
|
+
match &e.kind {
|
|
1172
|
+
ExprKind::FnCall(c) if c.name == crate::compile_builtins::COPY_FN => vec![],
|
|
1173
|
+
ExprKind::FnCall(c) => c.args.iter().map(argExpr).collect(),
|
|
1174
|
+
ExprKind::Attribute(a) => match &a.attr {
|
|
1175
|
+
AttrKind::Method(c) => c.args.iter().map(argExpr).collect(),
|
|
1176
|
+
_ => vec![],
|
|
1177
|
+
},
|
|
1178
|
+
_ => vec![],
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1160
1182
|
/// A constructor of `c`'s pattern at `k` in tail position of the arm, with every field given and no
|
|
1161
1183
|
/// spread: the matched cell can take its values. The ids of those constructors.
|
|
1162
1184
|
fn reuseTails(an: &An, c: &Case, k: usize) -> Vec<NodeId> {
|
|
@@ -1546,6 +1568,16 @@ impl<'a> Scan<'a> {
|
|
|
1546
1568
|
}
|
|
1547
1569
|
let v = self.v;
|
|
1548
1570
|
let unit = self.unit;
|
|
1571
|
+
// `sized := if c then self.resized(p) else self`: either branch's value, each as a binding
|
|
1572
|
+
// of its own; a branch that is another variable hands it over when that is unique, which
|
|
1573
|
+
// its own scan proves (nothing reads it after the binding, see `stmt`).
|
|
1574
|
+
if let ExprKind::InlineIf(i) = &val.kind {
|
|
1575
|
+
let branch = |e: &Expr| match varName(e) {
|
|
1576
|
+
Some(n) => n != v && self.an.varOk(unit, n).is_ok(),
|
|
1577
|
+
None => self.bindingOk(e, declare),
|
|
1578
|
+
};
|
|
1579
|
+
return branch(&i.then) && branch(&i.else_);
|
|
1580
|
+
}
|
|
1549
1581
|
let same = |a: &Expr| (!declare && isVar(a, v)) || self.an.freshArg(unit, a, Some(v));
|
|
1550
1582
|
match self.an.passDeps(val, &same) {
|
|
1551
1583
|
Some(deps) => deps.iter().all(|&(g, j)| self.an.passParamOk(g, j)),
|
|
@@ -1670,10 +1702,10 @@ impl<'a> Scan<'a> {
|
|
|
1670
1702
|
self.expr(val, false, true);
|
|
1671
1703
|
}
|
|
1672
1704
|
}
|
|
1673
|
-
// `b := match a / Ship as s => .. / _ => a`: a block value
|
|
1705
|
+
// `b := match a / Ship as s => .. / _ => a`: a block value (or an inline
|
|
1674
|
-
// result may be the variable moves it into `n` when nothing
|
|
1706
|
+
// `if`) whose result may be the variable moves it into `n` when nothing
|
|
1675
|
-
// reads it (a downcast in another arm may write it in place).
|
|
1707
|
+
// after reads it (a downcast in another arm may write it in place).
|
|
1676
|
-
AssignTarget::Var(_) if !live && matches!(val.kind, ExprKind::Block(_)) => {
|
|
1708
|
+
AssignTarget::Var(_) if !live && matches!(val.kind, ExprKind::Block(_) | ExprKind::InlineIf(_)) => {
|
|
1677
1709
|
self.expr(val, true, false)
|
|
1678
1710
|
}
|
|
1679
1711
|
AssignTarget::Var(n) => self.operand(val, live, || format!("`{v}` is also held by `{n}`")),
|
|
@@ -3173,176 +3205,322 @@ fn collectMoves(a: &An, _spreads: &[SpreadSite]) -> (HashMap<NodeId, String>, Ve
|
|
|
3173
3205
|
let mut reads: HashMap<NodeId, String> = HashMap::new();
|
|
3174
3206
|
let mut moves: Vec<(String, usize, NodeId)> = vec![];
|
|
3175
3207
|
for (ui, u) in a.units.iter().enumerate().filter(|(ui, _)| a.live[*ui]) {
|
|
3208
|
+
for region in moveRegions(&u.block) {
|
|
3176
|
-
|
|
3209
|
+
let mut order = 0usize;
|
|
3177
|
-
|
|
3210
|
+
let mut fieldReads: HashMap<(String, String), usize> = HashMap::new();
|
|
3178
|
-
|
|
3211
|
+
let mut varCount: HashMap<String, usize> = HashMap::new();
|
|
3179
|
-
|
|
3212
|
+
let mut spreadsOf: HashMap<String, Vec<(usize, NodeId, &ClassCall)>> = HashMap::new();
|
|
3180
|
-
|
|
3213
|
+
// (order, bound name, owner, field, id of the read)
|
|
3181
|
-
|
|
3214
|
+
let mut binds: Vec<(usize, String, String, String, NodeId)> = vec![];
|
|
3182
|
-
|
|
3215
|
+
walkBlock(region, &mut |n| {
|
|
3183
|
-
|
|
3216
|
+
order += 1;
|
|
3184
|
-
|
|
3217
|
+
match n {
|
|
3185
|
-
|
|
3218
|
+
Node::S(Stmt { kind: StmtKind::Assign(asg), .. }) => {
|
|
3186
|
-
|
|
3219
|
+
if let ([AssignTarget::Var(w)], [val], true) = (&asg.targets[..], &asg.values[..], asg.declare) {
|
|
3187
|
-
|
|
3220
|
+
if let ExprKind::Attribute(x) = &val.kind {
|
|
3188
|
-
|
|
3221
|
+
if let (AttrKind::Field(g), Some(o)) = (&x.attr, varName(&x.object)) {
|
|
3189
|
-
|
|
3222
|
+
if o != w {
|
|
3190
|
-
|
|
3223
|
+
binds.push((order, w.clone(), o.to_string(), g.clone(), val.id));
|
|
3224
|
+
}
|
|
3191
3225
|
}
|
|
3192
3226
|
}
|
|
3193
3227
|
}
|
|
3194
3228
|
}
|
|
3195
|
-
}
|
|
3196
|
-
|
|
3229
|
+
Node::E(e) => match &e.kind {
|
|
3197
|
-
|
|
3230
|
+
ExprKind::Var(_) | ExprKind::Self_ => {
|
|
3198
|
-
|
|
3231
|
+
*varCount.entry(varName(e).unwrap().to_string()).or_default() += 1;
|
|
3199
|
-
}
|
|
3200
|
-
ExprKind::Attribute(x) => {
|
|
3201
|
-
if let (AttrKind::Field(g), Some(o)) = (&x.attr, varName(&x.object)) {
|
|
3202
|
-
*fieldReads.entry((o.to_string(), g.clone())).or_default() += 1;
|
|
3203
3232
|
}
|
|
3233
|
+
ExprKind::Attribute(x) => {
|
|
3234
|
+
if let (AttrKind::Field(g), Some(o)) = (&x.attr, varName(&x.object)) {
|
|
3235
|
+
*fieldReads.entry((o.to_string(), g.clone())).or_default() += 1;
|
|
3204
|
-
|
|
3236
|
+
}
|
|
3205
|
-
ExprKind::ClassCall(cc) => {
|
|
3206
|
-
if let Some(o) = cc.spread.as_ref().and_then(|x| varName(x)) {
|
|
3207
|
-
spreadsOf.entry(o.to_string()).or_default().push((order, e.id, cc));
|
|
3208
3237
|
}
|
|
3238
|
+
ExprKind::ClassCall(cc) => {
|
|
3239
|
+
if let Some(o) = cc.spread.as_ref().and_then(|x| varName(x)) {
|
|
3240
|
+
spreadsOf.entry(o.to_string()).or_default().push((order, e.id, cc));
|
|
3209
|
-
|
|
3241
|
+
}
|
|
3242
|
+
}
|
|
3243
|
+
_ => {}
|
|
3244
|
+
},
|
|
3210
3245
|
_ => {}
|
|
3211
|
-
},
|
|
3212
|
-
_ => {}
|
|
3213
|
-
}
|
|
3214
|
-
true
|
|
3215
|
-
});
|
|
3216
|
-
let fieldName = |cc: &ClassCall, k: usize| -> Option<String> {
|
|
3217
|
-
let fa = &cc.fields[k];
|
|
3218
|
-
if !fa.name.is_empty() {
|
|
3219
|
-
Some(fa.name.clone())
|
|
3220
|
-
} else {
|
|
3221
|
-
fieldsOf(&a.enums, &cc.type_name).and_then(|n| n.get(k).cloned())
|
|
3222
|
-
}
|
|
3223
|
-
};
|
|
3224
|
-
let countIn = |e: &Expr, w: &str| -> usize {
|
|
3225
|
-
let mut n = 0;
|
|
3226
|
-
walkExpr(e, &mut |x| {
|
|
3227
|
-
if let Node::E(y) = x {
|
|
3228
|
-
if varName(y) == Some(w) {
|
|
3229
|
-
n += 1;
|
|
3230
|
-
}
|
|
3231
3246
|
}
|
|
3232
3247
|
true
|
|
3233
3248
|
});
|
|
3249
|
+
let fieldName = |cc: &ClassCall, k: usize| -> Option<String> {
|
|
3250
|
+
let fa = &cc.fields[k];
|
|
3251
|
+
if !fa.name.is_empty() {
|
|
3252
|
+
Some(fa.name.clone())
|
|
3253
|
+
} else {
|
|
3254
|
+
fieldsOf(&a.enums, &cc.type_name).and_then(|n| n.get(k).cloned())
|
|
3234
|
-
|
|
3255
|
+
}
|
|
3235
|
-
|
|
3256
|
+
};
|
|
3236
|
-
// Uses of `o` in the field values other than a plain read `o.g`: a method call on `o`
|
|
3237
|
-
// (read-only or not) or `o` passed along may look at the moved field after the in-place
|
|
3238
|
-
// update and see the new object, where a copy would show the old one.
|
|
3239
|
-
|
|
3257
|
+
let countIn = |e: &Expr, w: &str| -> usize {
|
|
3240
|
-
|
|
3258
|
+
let mut n = 0;
|
|
3241
|
-
for fa in &cc.fields {
|
|
3242
|
-
walkExpr(
|
|
3259
|
+
walkExpr(e, &mut |x| {
|
|
3243
|
-
if let Node::E(
|
|
3260
|
+
if let Node::E(y) = x {
|
|
3244
|
-
match &e.kind {
|
|
3245
|
-
ExprKind::Var(_) | ExprKind::Self_ if varName(e) == Some(o) => vars += 1,
|
|
3246
|
-
|
|
3261
|
+
if varName(y) == Some(w) {
|
|
3247
|
-
if matches!(x.attr, AttrKind::Field(_)) && varName(&x.object) == Some(o) =>
|
|
3248
|
-
{
|
|
3249
|
-
|
|
3262
|
+
n += 1;
|
|
3250
|
-
}
|
|
3251
|
-
_ => {}
|
|
3252
3263
|
}
|
|
3253
3264
|
}
|
|
3254
3265
|
true
|
|
3255
3266
|
});
|
|
3256
|
-
|
|
3267
|
+
n
|
|
3257
|
-
vars.saturating_sub(reads)
|
|
3258
|
-
|
|
3268
|
+
};
|
|
3259
|
-
|
|
3269
|
+
// Uses of `o` in the field values other than a plain read `o.g`: a method call on `o`
|
|
3270
|
+
// (read-only or not) or `o` passed along may look at the moved field after the in-place
|
|
3260
|
-
|
|
3271
|
+
// update and see the new object, where a copy would show the old one.
|
|
3261
|
-
let
|
|
3272
|
+
let bareUses = |cc: &ClassCall, o: &str| -> usize {
|
|
3262
|
-
|
|
3273
|
+
let (mut vars, mut reads) = (0usize, 0usize);
|
|
3263
|
-
continue;
|
|
3264
|
-
}
|
|
3265
|
-
|
|
3274
|
+
for fa in &cc.fields {
|
|
3266
|
-
let Some(g) = fieldName(cc, k) else { continue };
|
|
3267
|
-
let total = fieldReads.get(&(o.clone(), g.clone())).copied().unwrap_or(0);
|
|
3268
|
-
if g.contains('.') || total == 0 {
|
|
3269
|
-
continue;
|
|
3270
|
-
}
|
|
3271
|
-
let isRead = |x: &Expr| {
|
|
3272
|
-
matches!(&x.kind, ExprKind::Attribute(y)
|
|
3273
|
-
if y.attr == AttrKind::Field(g.clone()) && varName(&y.object) == Some(o.as_str()))
|
|
3274
|
-
};
|
|
3275
|
-
// The read handed to the call, and the call's receiver and arguments.
|
|
3276
|
-
let mut found: Option<(NodeId, Vec<&Expr>)> = None;
|
|
3277
|
-
|
|
3275
|
+
walkExpr(&fa.value, &mut |n| {
|
|
3278
|
-
|
|
3276
|
+
if let Node::E(e) = n {
|
|
3279
|
-
Node::E(Expr { kind: ExprKind::Closure(_), .. }) => return false,
|
|
3280
|
-
Node::E(Expr { kind: ExprKind::FnCall(c), .. }) => {
|
|
3281
|
-
|
|
3277
|
+
match &e.kind {
|
|
3278
|
+
ExprKind::Var(_) | ExprKind::Self_ if varName(e) == Some(o) => vars += 1,
|
|
3282
|
-
|
|
3279
|
+
ExprKind::Attribute(x)
|
|
3283
|
-
|
|
3280
|
+
if matches!(x.attr, AttrKind::Field(_)) && varName(&x.object) == Some(o) =>
|
|
3281
|
+
{
|
|
3282
|
+
reads += 1
|
|
3284
3283
|
}
|
|
3284
|
+
_ => {}
|
|
3285
3285
|
}
|
|
3286
3286
|
}
|
|
3287
|
-
Node::E(Expr { kind: ExprKind::Attribute(y), .. }) => {
|
|
3288
|
-
if let AttrKind::Method(c) = &y.attr {
|
|
3289
|
-
let operands = || std::iter::once(&y.object).chain(c.args.iter().map(argExpr)).collect();
|
|
3290
|
-
|
|
3287
|
+
true
|
|
3291
|
-
|
|
3288
|
+
});
|
|
3292
|
-
|
|
3289
|
+
}
|
|
3290
|
+
vars.saturating_sub(reads)
|
|
3291
|
+
};
|
|
3292
|
+
// Call form.
|
|
3293
|
+
for (o, sites) in &spreadsOf {
|
|
3294
|
+
let [(_, node, cc)] = &sites[..] else { continue };
|
|
3295
|
+
if bareUses(cc, o) > 0 {
|
|
3296
|
+
continue;
|
|
3297
|
+
}
|
|
3298
|
+
for k in 0..cc.fields.len() {
|
|
3299
|
+
let Some(g) = fieldName(cc, k) else { continue };
|
|
3300
|
+
let total = fieldReads.get(&(o.clone(), g.clone())).copied().unwrap_or(0);
|
|
3301
|
+
if g.contains('.') || total == 0 {
|
|
3302
|
+
continue;
|
|
3303
|
+
}
|
|
3304
|
+
let isRead = |x: &Expr| {
|
|
3305
|
+
matches!(&x.kind, ExprKind::Attribute(y)
|
|
3306
|
+
if y.attr == AttrKind::Field(g.clone()) && varName(&y.object) == Some(o.as_str()))
|
|
3307
|
+
};
|
|
3308
|
+
// The read handed to the call, and the call's receiver and arguments.
|
|
3309
|
+
let mut found: Option<(NodeId, Vec<&Expr>)> = None;
|
|
3310
|
+
walkExpr(&cc.fields[k].value, &mut |n| {
|
|
3311
|
+
match n {
|
|
3312
|
+
Node::E(Expr { kind: ExprKind::Closure(_), .. }) => return false,
|
|
3313
|
+
Node::E(Expr { kind: ExprKind::FnCall(c), .. }) => {
|
|
3293
3314
|
for x in &c.args {
|
|
3294
3315
|
if isRead(argExpr(x)) {
|
|
3316
|
+
found = Some((argExpr(x).id, c.args.iter().map(argExpr).collect()));
|
|
3317
|
+
}
|
|
3318
|
+
}
|
|
3319
|
+
}
|
|
3320
|
+
Node::E(Expr { kind: ExprKind::Attribute(y), .. }) => {
|
|
3321
|
+
if let AttrKind::Method(c) = &y.attr {
|
|
3322
|
+
let operands = || std::iter::once(&y.object).chain(c.args.iter().map(argExpr)).collect();
|
|
3323
|
+
if isRead(&y.object) {
|
|
3324
|
+
found = Some((y.object.id, operands()));
|
|
3325
|
+
}
|
|
3326
|
+
for x in &c.args {
|
|
3327
|
+
if isRead(argExpr(x)) {
|
|
3295
|
-
|
|
3328
|
+
found = Some((argExpr(x).id, operands()));
|
|
3329
|
+
}
|
|
3296
3330
|
}
|
|
3297
3331
|
}
|
|
3298
3332
|
}
|
|
3333
|
+
_ => {}
|
|
3299
3334
|
}
|
|
3335
|
+
true
|
|
3336
|
+
});
|
|
3337
|
+
// Other reads of `o.g` may only read a field of it (`o.g.h`, which holds no
|
|
3338
|
+
// reference to `o.g`) before the call can write it in place: in the call's own
|
|
3339
|
+
// receiver and arguments, or in a statement before the spread's.
|
|
3340
|
+
let found = found.filter(|(_, operands)| {
|
|
3341
|
+
let mut early: Vec<NodeId> = vec![];
|
|
3300
|
-
|
|
3342
|
+
for x in operands {
|
|
3343
|
+
subReads(x, o, &g, &mut early);
|
|
3301
|
-
|
|
3344
|
+
}
|
|
3345
|
+
readsBefore(region, *node, o, &g, &mut early);
|
|
3302
|
-
|
|
3346
|
+
total == 1 + early.len()
|
|
3303
|
-
|
|
3347
|
+
});
|
|
3304
|
-
|
|
3348
|
+
if let Some((id, _)) = found {
|
|
3305
|
-
// reference to `o.g`) before the call can write it in place: in the call's own
|
|
3306
|
-
|
|
3349
|
+
// `o` is spread into `cc`, so `cc`'s type owns the field.
|
|
3350
|
+
let Some(t) = a.owner.get(&cc.type_name) else { continue };
|
|
3307
|
-
|
|
3351
|
+
let key = fieldKey(t, &g);
|
|
3308
|
-
let mut early: Vec<NodeId> = vec![];
|
|
3309
|
-
for x in operands {
|
|
3310
|
-
|
|
3352
|
+
reads.insert(id, key.clone());
|
|
3353
|
+
moves.push((key, ui, *node));
|
|
3311
3354
|
}
|
|
3312
|
-
readsBefore(&u.block, *node, o, &g, &mut early);
|
|
3313
|
-
total == 1 + early.len()
|
|
3314
|
-
});
|
|
3315
|
-
if let Some((id, _)) = found {
|
|
3316
|
-
// `o` is spread into `cc`, so `cc`'s type owns the field.
|
|
3317
|
-
let Some(t) = a.owner.get(&cc.type_name) else { continue };
|
|
3318
|
-
let key = fieldKey(t, &g);
|
|
3319
|
-
reads.insert(id, key.clone());
|
|
3320
|
-
moves.push((key, ui, *node));
|
|
3321
3355
|
}
|
|
3322
3356
|
}
|
|
3357
|
+
// Binding form.
|
|
3358
|
+
for (at, w, o, g, id) in &binds {
|
|
3359
|
+
let Some(sites) = spreadsOf.get(o) else { continue };
|
|
3360
|
+
let [(sAt, node, cc)] = &sites[..] else { continue };
|
|
3361
|
+
if bareUses(cc, o) > 0 {
|
|
3362
|
+
continue;
|
|
3363
|
+
}
|
|
3364
|
+
if sAt < at || fieldReads.get(&(o.clone(), g.clone())) != Some(&1) {
|
|
3365
|
+
continue;
|
|
3366
|
+
}
|
|
3367
|
+
let Some(k) = (0..cc.fields.len()).find(|&k| fieldName(cc, k).as_deref() == Some(g.as_str())) else { continue };
|
|
3368
|
+
let inside = countIn(&cc.fields[k].value, w);
|
|
3369
|
+
if inside == 0 || varCount.get(w) != Some(&inside) {
|
|
3370
|
+
continue;
|
|
3371
|
+
}
|
|
3372
|
+
let Some(t) = a.owner.get(&cc.type_name) else { continue };
|
|
3373
|
+
let key = fieldKey(t, g);
|
|
3374
|
+
reads.insert(*id, key.clone());
|
|
3375
|
+
moves.push((key, ui, *node));
|
|
3376
|
+
}
|
|
3323
3377
|
}
|
|
3378
|
+
}
|
|
3379
|
+
(reads, moves)
|
|
3380
|
+
}
|
|
3381
|
+
|
|
3382
|
+
/// Reads `w := x.f` whose local is only read, and read while nothing can write `x.f` in place:
|
|
3383
|
+
/// every use of `w` is the object of a field read, the receiver of read-only methods or an
|
|
3384
|
+
/// argument of parameters every target only reads (`An.roParams`), never in a closure, and all
|
|
3385
|
+
/// of them come in the statements after the binding and before the first that mentions `x` as
|
|
3386
|
+
/// anything but the object of a field read (`x.g`). A write in place of the object in `x.f` takes
|
|
3387
|
+
/// a unique hold of it, through `x` (a spread of `x` moving `x.f`, `x` handed on) or through `w`,
|
|
3388
|
+
/// and neither happens while `w` is alive. Such a read holds no reference past its reads, like
|
|
3389
|
+
/// `x.f.h` (`ship := self.ship` then `Orb.spawn(orb, roll, ship)` and `ship.body.pos`).
|
|
3390
|
+
fn aliasReads(a: &An) -> Vec<NodeId> {
|
|
3324
|
-
|
|
3391
|
+
let mut out = vec![];
|
|
3392
|
+
for (ui, u) in a.units.iter().enumerate().filter(|(ui, _)| a.live[*ui]) {
|
|
3325
|
-
for
|
|
3393
|
+
for region in moveRegions(&u.block) {
|
|
3326
|
-
|
|
3394
|
+
for (i, st) in region.stmts.iter().enumerate() {
|
|
3327
|
-
|
|
3395
|
+
let StmtKind::Assign(asg) = &st.kind else { continue };
|
|
3396
|
+
let ([AssignTarget::Var(w)], [val], true) = (&asg.targets[..], &asg.values[..], asg.declare) else { continue };
|
|
3328
|
-
|
|
3397
|
+
let ExprKind::Attribute(x) = &val.kind else { continue };
|
|
3398
|
+
let (AttrKind::Field(_), Some(o)) = (&x.attr, varName(&x.object)) else { continue };
|
|
3399
|
+
if o == w || o != "self" && a.units[ui].params.iter().all(|p| p.name != o) {
|
|
3329
|
-
|
|
3400
|
+
continue;
|
|
3401
|
+
}
|
|
3402
|
+
let rest = ®ion.stmts[i + 1..];
|
|
3403
|
+
// The window: the statements before the first that uses `o` otherwise than `o.g`.
|
|
3404
|
+
let end = rest.iter().position(|s| !onlyFieldReadsOf(s, o)).unwrap_or(rest.len());
|
|
3405
|
+
if rest[end..].iter().any(|s| stmtMentions(s, w)) || !rest.iter().all(|s| !rebinds(s, w)) {
|
|
3406
|
+
continue;
|
|
3407
|
+
}
|
|
3408
|
+
if rest[..end].iter().all(|s| usesOnlyRead(a, s, w)) {
|
|
3409
|
+
out.push(nodeKey(val.id));
|
|
3410
|
+
}
|
|
3330
3411
|
}
|
|
3412
|
+
}
|
|
3413
|
+
}
|
|
3414
|
+
out
|
|
3415
|
+
}
|
|
3416
|
+
|
|
3417
|
+
/// Does `s` mention `o` only as the object of field reads (`o.g`), and never assign it?
|
|
3331
|
-
|
|
3418
|
+
fn onlyFieldReadsOf(s: &Stmt, o: &str) -> bool {
|
|
3419
|
+
if let StmtKind::Assign(asg) = &s.kind {
|
|
3420
|
+
if asg.targets.iter().any(|t| matches!(t, AssignTarget::Var(n) if n == o) || matches!(t, AssignTarget::Field(x, _) if varName(x) == Some(o))) {
|
|
3332
|
-
|
|
3421
|
+
return false;
|
|
3422
|
+
}
|
|
3423
|
+
}
|
|
3424
|
+
let (mut uses, mut reads) = (0usize, 0usize);
|
|
3425
|
+
walkStmt(s, &mut |n| {
|
|
3426
|
+
if let Node::E(e) = n {
|
|
3427
|
+
match &e.kind {
|
|
3428
|
+
_ if varName(e) == Some(o) => uses += 1,
|
|
3429
|
+
ExprKind::Attribute(x) if matches!(x.attr, AttrKind::Field(_)) && varName(&x.object) == Some(o) => reads += 1,
|
|
3430
|
+
_ => {}
|
|
3333
3431
|
}
|
|
3432
|
+
}
|
|
3433
|
+
true
|
|
3434
|
+
});
|
|
3435
|
+
uses == reads
|
|
3436
|
+
}
|
|
3437
|
+
|
|
3438
|
+
fn stmtMentions(s: &Stmt, w: &str) -> bool {
|
|
3439
|
+
let mut hit = false;
|
|
3440
|
+
walkStmt(s, &mut |n| {
|
|
3441
|
+
if let Node::E(e) = n {
|
|
3442
|
+
hit |= varName(e) == Some(w);
|
|
3443
|
+
}
|
|
3444
|
+
!hit
|
|
3445
|
+
});
|
|
3446
|
+
hit
|
|
3447
|
+
}
|
|
3448
|
+
|
|
3334
|
-
|
|
3449
|
+
/// Does `s` bind `w` again (an assignment, a pattern, a closure parameter)?
|
|
3450
|
+
fn rebinds(s: &Stmt, w: &str) -> bool {
|
|
3451
|
+
let mut hit = false;
|
|
3335
|
-
|
|
3452
|
+
let binds = |p: &CasePattern| {
|
|
3453
|
+
let mut names = vec![];
|
|
3454
|
+
crate::visit::patternBinders(p, &mut names);
|
|
3455
|
+
names.iter().any(|n| n == w)
|
|
3456
|
+
};
|
|
3457
|
+
walkStmt(s, &mut |n| {
|
|
3458
|
+
match n {
|
|
3459
|
+
Node::S(Stmt { kind: StmtKind::Assign(asg), .. }) => hit |= asg.targets.iter().any(|t| matches!(t, AssignTarget::Var(n) if n == w)),
|
|
3460
|
+
Node::S(Stmt { kind: StmtKind::Match(m), .. }) => hit |= m.cases.iter().any(|c| c.patterns.iter().any(|p| binds(p))),
|
|
3461
|
+
Node::E(Expr { kind: ExprKind::Bind(b), .. }) => hit |= b.case.patterns.iter().any(|p| binds(p)),
|
|
3462
|
+
Node::E(Expr { kind: ExprKind::Closure(c), .. }) => hit |= c.params.iter().any(|p| p == w),
|
|
3463
|
+
_ => {}
|
|
3464
|
+
}
|
|
3465
|
+
!hit
|
|
3466
|
+
});
|
|
3467
|
+
hit
|
|
3468
|
+
}
|
|
3469
|
+
|
|
3470
|
+
/// Is every use of `w` in `s` a read: the object of a field read, the receiver of read-only
|
|
3471
|
+
/// methods, or an argument of parameters every target only reads; none in a closure?
|
|
3472
|
+
fn usesOnlyRead(a: &An, s: &Stmt, w: &str) -> bool {
|
|
3473
|
+
let (mut uses, mut reads, mut bad) = (0usize, 0usize, false);
|
|
3474
|
+
walkStmt(s, &mut |n| {
|
|
3475
|
+
if let Node::E(e) = n {
|
|
3476
|
+
match &e.kind {
|
|
3477
|
+
_ if varName(e) == Some(w) => uses += 1,
|
|
3478
|
+
ExprKind::Attribute(x) if varName(&x.object) == Some(w) => match &x.attr {
|
|
3479
|
+
AttrKind::Field(_) => reads += 1,
|
|
3480
|
+
AttrKind::Method(mc) => {
|
|
3481
|
+
let cands = a.cands(mc);
|
|
3336
|
-
|
|
3482
|
+
if !cands.is_empty() && cands.iter().all(|g| a.ro.contains(g)) {
|
|
3483
|
+
reads += 1;
|
|
3484
|
+
}
|
|
3485
|
+
}
|
|
3486
|
+
},
|
|
3487
|
+
ExprKind::Closure(_) => {
|
|
3488
|
+
let mut f = false;
|
|
3489
|
+
walkExpr(e, &mut |m| {
|
|
3490
|
+
if let Node::E(y) = m {
|
|
3491
|
+
f |= varName(y) == Some(w);
|
|
3492
|
+
}
|
|
3493
|
+
!f
|
|
3494
|
+
});
|
|
3337
|
-
|
|
3495
|
+
bad |= f;
|
|
3496
|
+
}
|
|
3497
|
+
_ => {}
|
|
3338
3498
|
}
|
|
3499
|
+
if let Some(targets) = a.callTargets(e) {
|
|
3500
|
+
for y in callArgs(e).into_iter().filter(|y| varName(y) == Some(w)) {
|
|
3501
|
+
if targets.iter().all(|(t, given)| given.iter().enumerate().any(|(i, g)| g.is_some_and(|g| g.id == y.id) && a.roParams.contains(&(*t, i)))) {
|
|
3502
|
+
reads += 1;
|
|
3503
|
+
}
|
|
3504
|
+
}
|
|
3505
|
+
}
|
|
3506
|
+
}
|
|
3507
|
+
!bad
|
|
3508
|
+
});
|
|
3509
|
+
!bad && uses == reads
|
|
3510
|
+
}
|
|
3511
|
+
|
|
3512
|
+
/// The parts of a unit's body that `collectMoves` looks at one by one: the body, or, when it is
|
|
3513
|
+
/// one `match` on plain variables with no guards (`fun update(self, a) = match a ..`), each arm's
|
|
3514
|
+
/// body. Only one arm runs and nothing runs after the match, so an arm is a whole run of the
|
|
3515
|
+
/// unit: a spread of `self` in each arm is the one spread of its run, and reads of a field in
|
|
3516
|
+
/// the other arms are on other runs.
|
|
3517
|
+
fn moveRegions(b: &Block) -> Vec<&Block> {
|
|
3518
|
+
if let [Stmt { kind: StmtKind::Match(m), .. }] = &b.stmts[..] {
|
|
3519
|
+
if m.subjects.iter().all(|x| varName(x).is_some()) && m.cases.iter().all(|c| c.guard.is_none()) {
|
|
3339
|
-
|
|
3520
|
+
return m.cases.iter().map(|c| &c.body).collect();
|
|
3340
|
-
let key = fieldKey(t, g);
|
|
3341
|
-
reads.insert(*id, key.clone());
|
|
3342
|
-
moves.push((key, ui, *node));
|
|
3343
3521
|
}
|
|
3344
3522
|
}
|
|
3345
|
-
|
|
3523
|
+
vec![b]
|
|
3346
3524
|
}
|
|
3347
3525
|
|
|
3348
3526
|
/// The reads `self.f` of getters (`fun place(self) -> Place = self.at`) that hand out no reference:
|
|
@@ -3568,8 +3746,8 @@ fn keepsNoFn(an: &An, src: &Source) -> HashSet<(usize, usize)> {
|
|
|
3568
3746
|
/// fn-typed parameter it never lets escape (`An.keepsNoFn`): they are dead once the call returns,
|
|
3569
3747
|
/// so a variable they capture is held no longer than that, and their uses of it are judged as
|
|
3570
3748
|
/// uses in place (`list.any(|m| self.ship.touches(m))` only reads `self`). Whether closure
|
|
3571
|
-
/// specialisation made a copy for the call (
|
|
3749
|
+
/// specialisation made a copy for the call (a big callee gets at most
|
|
3572
|
-
/// whatever order the program's calls come) then changes nothing here.
|
|
3750
|
+
/// `closure_spec::MAX_BIG_COPIES`, in whatever order the program's calls come) then changes nothing here.
|
|
3573
3751
|
fn passedClosures(an: &An, b: &Block) -> HashSet<NodeId> {
|
|
3574
3752
|
let mut out = HashSet::new();
|
|
3575
3753
|
walkBlock(b, &mut |n| {
|
|
@@ -4201,6 +4379,7 @@ fn collectFacts(an: &mut An) {
|
|
|
4201
4379
|
let (moveReads, moves) = collectMoves(a, &spreads);
|
|
4202
4380
|
allowed.extend(moveReads.keys().copied());
|
|
4203
4381
|
allowed.extend(getterReads(a, &escaped));
|
|
4382
|
+
allowed.extend(aliasReads(a));
|
|
4204
4383
|
let readsEscape: HashSet<String> =
|
|
4205
4384
|
reads.into_iter().filter(|(p, _)| !allowed.contains(p)).flat_map(|(_, f)| f).collect();
|
|
4206
4385
|
badField.extend(readsEscape);
|
compiler/core/tests/core/uniqueness_fold_test.rs
CHANGED
|
@@ -125,7 +125,7 @@ fn aShadowingClosureParamOrLoopBinderIsNotTheBoundedParam() {
|
|
|
125
125
|
/// `t.look()` where `look` reads `self` only inside a closure handed to a fn that never lets the
|
|
126
126
|
/// closure escape (`Main.count`): the closure is dead once the call returns, so nothing keeps
|
|
127
127
|
/// `t` and the spread after it is in place, whether or not closure specialisation copied
|
|
128
|
-
/// `count` for it (at most
|
|
128
|
+
/// `count` for it (a callee once got at most 8 copies program-wide, so an unrelated closure handed
|
|
129
129
|
/// to the same callee elsewhere used to decide it; U1, std/game's `Looping`). A callee that
|
|
130
130
|
/// stores the closure keeps `t`, and one the compiler supplies (`@native`) may.
|
|
131
131
|
#[test]
|
|
@@ -149,3 +149,43 @@ fn aReceiverReadInsideANonEscapingClosureIsNotKept() {
|
|
|
149
149
|
));
|
|
150
150
|
assert!(!proved(&r, "Main.go:9"), "{r:?}");
|
|
151
151
|
}
|
|
152
|
+
|
|
153
|
+
/// `fun update(self, a) = match a ..` with a spread of `self` in each arm: only one arm runs, so
|
|
154
|
+
/// each arm's spread is the one spread of its run, and `S(..self, ship: self.ship.moved(d))`
|
|
155
|
+
/// moves the ship into `moved`, which writes it in place (U4c). Another arm reading `self.ship`
|
|
156
|
+
/// is on another run.
|
|
157
|
+
#[test]
|
|
158
|
+
fn aFieldMovedInOneArmOfAWholeBodyMatch() {
|
|
159
|
+
let r = run(
|
|
160
|
+
"struct Ship(x: Int, y: Int) =\n fun moved(self, d: Int) -> Ship = Ship(..self, x: self.x + d)\n\nstruct S(ship: Ship, n: Int)\n\nenum A =\n | Go(d: Int)\n | Inc\n\nprimitive Main =\n fun update(s: S, a: A) -> S = match a\n Go(d) => S(..s, ship: s.ship.moved(d))\n Inc => S(..s, n: s.n + s.ship.x)\n\n fun go() -> S = Main.update(S(ship: Ship(x: 0, y: 0), n: 0), Go(d: 1))\n",
|
|
161
|
+
);
|
|
162
|
+
assert!(proved(&r, "Ship.moved:2"), "{r:?}");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/// `sized := if c then s.grown() else s` with `s` read no more: either branch hands `s` over,
|
|
166
|
+
/// so `sized` is unique and the spread of it is in place (U4c, std/game's `Game.run`).
|
|
167
|
+
#[test]
|
|
168
|
+
fn aVariableHandedOverThroughAnInlineIf() {
|
|
169
|
+
let r = run(
|
|
170
|
+
"struct Pt(x: Int, y: Int) =\n fun grown(self) -> Pt = Pt(..self, y: self.y + 1)\n\nprimitive Main =\n fun step(p: Pt, c: Bool) -> Pt =\n sized := if c then p.grown() else p\n Pt(..sized, x: sized.x + 1)\n\n fun go() -> Pt = Main.step(Pt(x: 0, y: 0), true)\n",
|
|
171
|
+
);
|
|
172
|
+
assert!(proved(&r, "Main.step:5"), "{r:?}");
|
|
173
|
+
// Read after the binding, `p` is not handed over.
|
|
174
|
+
let r = run(
|
|
175
|
+
"struct Pt(x: Int, y: Int) =\n fun grown(self) -> Pt = Pt(..self, y: self.y + 1)\n\nprimitive Main =\n fun step(p: Pt, c: Bool) -> Pt =\n sized := if c then p.grown() else p\n Pt(..sized, x: p.x + 1)\n\n fun go() -> Pt = Main.step(Pt(x: 0, y: 0), true)\n",
|
|
176
|
+
);
|
|
177
|
+
assert!(!proved(&r, "Main.step:5"), "{r:?}");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/// `ship := s.ship` whose local is only read (a field read, an argument of a parameter its fn
|
|
181
|
+
/// only reads) before anything could write `s.ship` in place holds no reference past those reads,
|
|
182
|
+
/// like `s.ship.x`: the field stays unique and `step`'s move of it is in place (U4c). A local
|
|
183
|
+
/// that is kept (stored in the result) makes the field shared.
|
|
184
|
+
#[test]
|
|
185
|
+
fn aFieldReadIntoALocalThatIsOnlyRead() {
|
|
186
|
+
let pre = "struct Ship(x: Int, y: Int) =\n fun moved(self, d: Int) -> Ship = Ship(..self, x: self.x + d)\n\nstruct S(ship: Ship, n: Int, last: Ship)\n\nprimitive Main =\n fun look(p: Ship) -> Int = p.x\n\n fun step(s: S) -> S = S(..s, ship: s.ship.moved(1))\n\n fun go() -> S = Main.step(Main.peek(S(ship: Ship(x: 0, y: 0), n: 0, last: Ship(x: 0, y: 0))))\n\n";
|
|
187
|
+
let r = run(&format!("{pre} fun peek(s: S) -> S =\n ship := s.ship\n k := Main.look(ship) + ship.y\n S(..s, n: k)\n"));
|
|
188
|
+
assert!(proved(&r, "Ship.moved:2"), "{r:?}");
|
|
189
|
+
let r = run(&format!("{pre} fun peek(s: S) -> S =\n ship := s.ship\n k := Main.look(ship) + ship.y\n S(..s, n: k, last: ship)\n"));
|
|
190
|
+
assert!(!proved(&r, "Ship.moved:2"), "{r:?}");
|
|
191
|
+
}
|
docs/backlog.md
CHANGED
|
@@ -7,7 +7,7 @@ Open work on `simpler-language`, rewritten 2026-11-11. Only what is still open i
|
|
|
7
7
|
The retained scene tree was replaced by master's screen view in functional form: a `Game(screens: ..)` of `Screen`s, a `ScreenSlice<A>` per screen with rules (master's `Slice<A>`), a `view` each frame that the game draws and hit-tests itself, commands as values for sounds and navigation (`std/game/Cmd`, returned by a slice's `update` beside its `then` actions, since 2026-11-16; one `update` and `subscriptions`, Elm style, since the same day), undo and time travel by re-simulation, and the headless kit (`docs/game.md`, "Beside master", has the table of differences). `SceneTree`, `Ctx`, `Clickable`, `Store` and `ui` are gone, with the examples that used them (`orb_run`, `kids/balloon_pop`, now `examples/games/orbitrunner` and `balloonpop`), `benchmarks/scene_trees` and two web-test stress fixtures (their results stay in `docs/benchmarks` and `compiler/game/web-test/BENCHMARK.md`). Orbit Runner's state checksum over four seeds of 3,600 ticks was master's (925878360) until its rules returned their follow-ups as `then` actions on 2026-11-16; it is 946756219 since. On 2026-11-13 it was brought up to master at e91c40ce (stages 24 and 25 and the review after 24): `View` and `Actor[View]`, actors stepped by the game through the screen's `stepped(dt)`, `Screen.hud`, `steps` and `camera`, one walk over `view`, turned boxes and text, the other mouse buttons and keys, the per-screen backgrounds and dropped pointer events of a transition (the "Beside master" table has what still differs). On 2026-11-13 (branch `sl/game-ports`) master's later framework work followed: the stepping of what a `Group` or an `Overlay` holds, frames on demand in the hosts (`frameIdle`), `plum run --dev` and its time travel keys, the window's size on the tick's input, the `Ticker`, a `Label`'s width from Arial's advance widths (in std and the browser's layout; master still estimates it) and `plum compile <folder>`. Open:
|
|
8
8
|
|
|
9
9
|
- **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).
|
|
10
|
-
- **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
|
|
10
|
+
- **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 made at most 8 per callee, program-wide, in the order it met 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 (since U4c a small callee, every walk of std, gets a copy for every closure). 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.
|
|
11
11
|
- **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.
|
|
12
12
|
- **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).
|
|
13
13
|
- **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()`), and one that pauses doesn't call it (`docs/game.md`, "Stepping"; the `@stepsWhen(cond)` field attribute was removed on 2026-11-16). A `List<View>` field is stepped by `Views.stepIn` since R3 (each item's type tested as it is stepped).
|
|
@@ -27,10 +27,10 @@ The retained scene tree was replaced by master's screen view in functional form:
|
|
|
27
27
|
- **`Map` reuse stops as soon as any map in the program is shared.** A uniquely owned map is now updated in its own cells (the `map` workload went from 479 to 281 ms; the old hash table took about 200), and `Int`, `Byte` and `Float.compare` are native and inlined. But field uniqueness is decided per type over the whole program, so one kept old version of a `Map` anywhere turns deep reuse off for every `Map`: Orb Run gets none. Reuse alongside sharing needs a per-value deep-uniqueness analysis. Also open: `Map.get` allocates an `Option` on a hit (fusion treats `Map.get` as an impure producer).
|
|
28
28
|
- **Closure calls go through the table** (since U4a, 2026-11-16). A `fn(..)` value is a `ref null $Closure` (`struct {i32 table index}`), and a closure that captures values is its own final subtype with the captures as fields: one allocation (none for a closure that captures nothing: a global built at start), no cast at the call (`struct.get` then `call_indirect`), one cast to a final type in the body. Not `call_ref` on a typed funcref field: wasmtime 47 reads and writes a funcref inside a GC object through a host call (`get_interned_func_ref`), which made Orbit Runner's update 2-3x slower when tried, and a cast to a non-final type that the value only subtypes is a host call too (`is_subtype`), so a `fn` read out of a `Slice` (an `anyref` array) still pays one. Release, interleaved, min of 9 (the machine was loaded): orbit update 13.4 to 12.2 ms, Orb Run 40.7 to 38.2, `benchmarks/closures/calls` 179 to 165; orbit paint, Clockwork, Balloon Pop, `cons`, `cons_rec` within noise (their closures are inlined by closure specialisation or not hot). Left: the table bounds and signature check of `call_indirect`, and the env cast in each capturing body.
|
|
29
29
|
- **A value struct inside an enum payload is boxed twice** (`Some(Pos)` allocates two objects). No measurement yet.
|
|
30
|
-
- **In-place updates still miss**
|
|
30
|
+
- **In-place updates still miss** (recounted 2026-11-16 after U4c, `--explain-fusion`, each "not in place" line by its innermost reason): Orb Run 36 in place, 39 not (10 a value still used after the update, all the time travel's `rewindTo`/`undo`/`redo` from a fold; 8 bound to a shared value; 6 rebound by a pattern, `match self.history / Some(h)` and `Shift.after`; 5 a shared value; 3 taken out of `List.head`; 2 fields out of declaration order; 2 a receiver read again; 1 each other). Orbit Runner 31 in place of 96, Clockwork 26 of 88, Menu 20 of 86, Balloon Pop 23 of 86; the biggest reason in the four games is a receiver kept or read again after a method call (19-22, mostly the time travel and `Looping`). No closure reason is left (Balloon Pop's 5 "an argument is passed from inside a closure" went with U4c: its fold's closures are copies now), but the counts are the same as before U4c: each fix moved a spread's reason one step down. U4c added three proofs, each with a test in `uniqueness_fold_test.rs`: a field moved in one arm of a whole-body `match` (`moveRegions`: `Steer` in `PlayingScreen.update` is now a move of `self.ship`), a variable handed over through an inline `if` (`sized := if c then self.resized(p) else self`), and a field read into a local that is only read (`aliasReads`: `ship := self.ship` then `ship.body.pos`); and a pattern binder handed to a read-only parameter counts as a read. What blocks the hot ones now: `PlayingScreen.ship` is shared because the view holds the ship (`if !self.over then self.ship`), so `Ship.steer`/`step`/`hurtFor` copy; `List.head` is shared because `List.find` returns the head and `List.next` pairs it (a field's uniqueness is one fact for every element type), so the actors' steps copy; `Game.run`'s `sized` needs `Game.resized` to return its `self` in place, which `Game.remake` (shared) blocks. The Elm-style `update` regression (Orbit Runner's update 7-10% slower after the Elm screens) was not found in instructions retired here (`screen_views/orbit.plum update` 283M before U4c, 280M after).
|
|
31
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
32
|
- **Static functions are slower than free functions** (0.78 s against 0.57 s on `benchmarks/static_calls`).
|
|
33
|
-
- **
|
|
33
|
+
- **Closures inlined by size** (U4c, 2026-11-16): a callee of at most 64 expression nodes (`closure_spec::SMALL_CALLEE`; std's closure-taking fns are 3-59, `List.map` 3, `fold` 8, `Range.fold` 33, `Slice.mergeRun` 59, the biggest in the repository 68) gets a copy for every closure handed to it; a bigger one keeps a cap of 8 (`MAX_BIG_COPIES`) per callee, in the order its calls are met, so which of its calls are inlined still depends on the rest of the program. Copies made inside copies stop at 4 levels (`MAX_DEPTH`), and 256 copies of one small callee is a safety net. The escape analysis now resolves `x.m(..)` by the checker's receiver type (`FnCall.recv`, stamped before closure specialisation): `List.all` had never been specialised, as `t.all(keep)` resolved to every `all` and `Set.all` captures its closure. Cost: Orbit Runner compiles in 2.7% more instructions (8,722M to 8,957M), its .wasm is 352 bytes bigger (112,515 to 112,867). Run time within 1% in instructions retired on every benchmark (Orb Run 839M to 845M, Orbit Runner update 283M to 280M, Clockwork update 639M to 643M, `closures/calls` 3,084M to 3,083M; whole process, wasmtime's compile included).
|
|
34
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.*`); every struct has its own empty supertype; purity treats every closure-taking function as impure, so pipelines fuse only after closure specialisation.
|
|
35
35
|
- No evidence either way, so not planned: unchecked reads in `for` over a `Slice` (checked `at` showed no cost).
|
|
36
36
|
|