plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
2f12c15
— Peter John
2026-11-11T01:15:29+05:30
uniqueness: drop dead closure arguments without copying every node
compiler/core/src/uniqueness.rs
CHANGED
|
@@ -2158,12 +2158,12 @@ fn argIsDead(an: &An, dead: &HashSet<(usize, usize)>, e: &Expr, k: usize) -> boo
|
|
|
2158
2158
|
fn stripDeadFnArgs(an: &mut An) {
|
|
2159
2159
|
let dead = deadFnParams(an);
|
|
2160
2160
|
let mut spreads: Vec<Vec<DeadSpread>> = an.units.iter().map(|_| vec![]).collect();
|
|
2161
|
+
// Taken out while they are rewritten (nothing below reads a unit's block), not copied.
|
|
2161
|
-
let mut blocks: Vec<Block> = an.units.
|
|
2162
|
+
let mut blocks: Vec<Block> = an.units.iter_mut().map(|u| std::mem::replace(&mut u.block, Block { stmts: vec![] })).collect();
|
|
2162
2163
|
for (ui, b) in blocks.iter_mut().enumerate() {
|
|
2163
2164
|
let out = &mut spreads[ui];
|
|
2164
2165
|
mutBlock(b, &mut |e| {
|
|
2165
|
-
let view = e.clone();
|
|
2166
|
-
let copies: Vec<usize> = match &
|
|
2166
|
+
let copies: Vec<usize> = match &e.kind {
|
|
2167
2167
|
ExprKind::FnCall(c) => an.isFree(&c.name).into_iter().collect(),
|
|
2168
2168
|
ExprKind::Attribute(a) => match &a.attr {
|
|
2169
2169
|
AttrKind::Method(c) => an.cands(c),
|
|
@@ -2171,6 +2171,29 @@ fn stripDeadFnArgs(an: &mut An) {
|
|
|
2171
2171
|
},
|
|
2172
2172
|
_ => vec![],
|
|
2173
2173
|
};
|
|
2174
|
+
// The arguments to drop, all judged on the call as it is before any is dropped.
|
|
2175
|
+
let drop: Vec<usize> = {
|
|
2176
|
+
let args = match &e.kind {
|
|
2177
|
+
ExprKind::FnCall(c) => &c.args,
|
|
2178
|
+
ExprKind::Attribute(a) => match &a.attr {
|
|
2179
|
+
AttrKind::Method(c) => &c.args,
|
|
2180
|
+
_ => return,
|
|
2181
|
+
},
|
|
2182
|
+
_ => return,
|
|
2183
|
+
};
|
|
2184
|
+
let mut ks = vec![];
|
|
2185
|
+
for (k, arg) in args.iter().enumerate() {
|
|
2186
|
+
let Arg::Positional(x) = arg else { break };
|
|
2187
|
+
// Only a value with nothing to run: another expression has effects and uses of its own.
|
|
2188
|
+
if matches!(x.kind, ExprKind::Closure(_) | ExprKind::Var(_)) && argIsDead(an, &dead, e, k) {
|
|
2189
|
+
ks.push(k);
|
|
2190
|
+
}
|
|
2191
|
+
}
|
|
2192
|
+
ks
|
|
2193
|
+
};
|
|
2194
|
+
if drop.is_empty() {
|
|
2195
|
+
return;
|
|
2196
|
+
}
|
|
2174
2197
|
let args = match &mut e.kind {
|
|
2175
2198
|
ExprKind::FnCall(c) => &mut c.args,
|
|
2176
2199
|
ExprKind::Attribute(a) => match &mut a.attr {
|
|
@@ -2179,12 +2202,8 @@ fn stripDeadFnArgs(an: &mut An) {
|
|
|
2179
2202
|
},
|
|
2180
2203
|
_ => return,
|
|
2181
2204
|
};
|
|
2182
|
-
for
|
|
2205
|
+
for k in drop {
|
|
2183
|
-
let Arg::Positional(x) =
|
|
2206
|
+
let Arg::Positional(x) = &mut args[k] else { continue };
|
|
2184
|
-
// Only a value with nothing to run: another expression has effects and uses of its own.
|
|
2185
|
-
if !matches!(x.kind, ExprKind::Closure(_) | ExprKind::Var(_)) || !argIsDead(an, &dead, &view, k) {
|
|
2186
|
-
continue;
|
|
2187
|
-
}
|
|
2188
2207
|
walkExpr(x, &mut |n| {
|
|
2189
2208
|
if let Node::E(Expr { kind: ExprKind::ClassCall(c), span, .. }) = n {
|
|
2190
2209
|
if c.spread.is_some() {
|