plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


9d8b25a — Peter John 2026-11-11T01:10:37+05:30
drive: share callee fns instead of copying one per call candidate
compiler/core/src/drive.rs CHANGED
@@ -12,10 +12,11 @@ use crate::ast::*;
12
12
  use crate::generics::Scope;
13
13
  use crate::rename::{renameStmts, substPT, substStmts, substType, Name};
14
14
  use crate::simplify::{boundNames, calledNames, mentions, pinExprWith, simplifyBlockInWith, simplifyBlockWith};
15
- use crate::visit::{argRef, innerBlockRefs, isTag, walkBlockRef, walkRef};
15
+ use crate::visit::{argRef, innerBlockRefs, isTag, walkBlockRef, walkRef, walkStmtsRef};
16
16
  use crate::visit::{childRefs, stmtBlockRefs, stmtBlocks, stmtExprRefs, visitBlock, visitExpr};
17
17
  use crate::whistle::embeds;
18
18
  use std::collections::{HashMap, HashSet};
19
+ use std::rc::Rc;
19
20
 
20
21
  const MAX_FUSED: usize = 64;
21
22
  const GROWTH: usize = 8;
@@ -55,7 +56,8 @@ impl From<&str> for Unfold {
55
56
  #[derive(Clone)]
56
57
  struct Call {
57
58
  key: String,
59
+ /// Shared with `Driver.fns`: a call site may have many candidate callees.
58
- f: Fn,
60
+ f: Rc<Fn>,
59
61
  args: Vec<Expr>,
60
62
  /// The call's explicit type arguments (`t.kidsAs<Dot>(0)`): `f`'s own generics, in order.
61
63
  targs: Vec<Type>,
@@ -64,7 +66,7 @@ struct Call {
64
66
  struct Driver {
65
67
  /// The simplifier's tables of the program being driven (it is read, never changed).
66
68
  simp: crate::simplify::Tables,
67
- fns: HashMap<String, Fn>,
69
+ fns: HashMap<String, Rc<Fn>>,
68
70
  free: HashSet<String>,
69
71
  /// names the fn being driven binds: a call of one is not a call of the free fn of that name
70
72
  bound: HashSet<String>,
@@ -568,7 +570,7 @@ impl Driver {
568
570
  } else if f.has_self {
569
571
  d.methods.entry(f.name.clone()).or_default().push(k.clone());
570
572
  }
571
- d.fns.insert(k, f.clone());
573
+ d.fns.insert(k, Rc::new(f.clone()));
572
574
  }
573
575
  Item::Enum(e) => {
574
576
  d.enumGens.insert(e.name.clone(), e.generics.iter().map(|g| g.name.clone()).collect());
@@ -636,7 +638,7 @@ impl Driver {
636
638
  }
637
639
  let name = self.fused[i].name.clone();
638
640
  if let Some(f) = self.fns.get_mut(&name) {
639
- f.body = body.clone();
641
+ Rc::make_mut(f).body = body.clone();
640
642
  }
641
643
  self.fused[i].body = body;
642
644
  i += 1;
@@ -851,7 +853,7 @@ impl Driver {
851
853
  ExprKind::Attribute(a) => {
852
854
  let AttrKind::Method(c) = &a.attr else { return vec![] };
853
855
  let Some(rest) = positional(&c.args) else { return vec![] };
854
- if let ExprKind::TypeName(t) = &a.object.clone().kind {
856
+ if let ExprKind::TypeName(t) = &a.object.kind {
855
857
  if t.contains('.') {
856
858
  return vec![]; // a method on a constant (`Color.White.withAlpha(..)`)
857
859
  }
@@ -1921,7 +1923,7 @@ impl Driver {
1921
1923
  }
1922
1924
  let pscope = sc.merge(&Scope::named(&fresh));
1923
1925
  let pin = |e: Expr| pinExprWith(e, &pret, &self.simp, &self.pure, &pscope).unwrap_or_else(|e| e);
1924
- let tcx = Tcx { pin: &pin, cases, callee: c.f.clone(), k, cn: cn.clone(), ctors: self.ctors.clone(), built: Default::default(), called: Default::default() };
1926
+ let tcx = Tcx { pin: &pin, cases, callee: (*c.f).clone(), k, cn: cn.clone(), ctors: self.ctors.clone(), built: Default::default(), called: Default::default() };
1925
1927
  let mut body = tcx
1926
1928
  .tailMap(&pbody)
1927
1929
  .ok_or(format!("producer {} has a result outside a tail position", p.key))?;
@@ -1989,7 +1991,7 @@ impl Driver {
1989
1991
  .unwrap_or(0)
1990
1992
  .max(self.depth.get(&p.f.name).copied().unwrap_or(0));
1991
1993
  self.depth.insert(name.clone(), nest);
1992
- self.fns.insert(name.clone(), f.clone());
1994
+ self.fns.insert(name.clone(), Rc::new(f.clone()));
1993
1995
  self.free.insert(name.clone());
1994
1996
  // the producer is pure, so the fused fn has exactly the consumer's effects
1995
1997
  if self.pure.contains(&c.key) {
@@ -2048,10 +2050,10 @@ impl Driver {
2048
2050
  // a just-built value with pure fields moves to its use like a pure producer call,
2049
2051
  // so a consumer applied to it can pick its arm (`layers := Cons(..); draw(layers)`)
2050
2052
  let isBuilt = self.ctorType(&v).is_some() && self.argPure(&v);
2051
- let rest: Vec<Stmt> = b.stmts[i + 1..].to_vec();
2052
- if !(isProducer || isBuilt) || cntStmts(&rest, &p, true) == 0 || rebinds(&rest, &p) {
2053
+ if !(isProducer || isBuilt) || cntStmts(&b.stmts[i + 1..], &p, true) == 0 || rebinds(&b.stmts[i + 1..], &p) {
2053
2054
  continue;
2054
2055
  }
2056
+ let rest: Vec<Stmt> = b.stmts[i + 1..].to_vec();
2055
2057
  let total = pathStmts(&rest, &p);
2056
2058
  let mut trial = Block { stmts: rest.clone() };
2057
2059
  visitBlock(&mut trial, &mut |x| {
@@ -2149,7 +2151,7 @@ impl Driver {
2149
2151
  /// mutates (pure or read-only)?
2150
2152
  fn consumedOnly(&self, stmts: &[Stmt], p: &str) -> usize {
2151
2153
  let mut good = 0;
2152
- walkBlockRef(&Block { stmts: stmts.to_vec() }, &mut |e| {
2154
+ walkStmtsRef(stmts, &mut |e| {
2153
2155
  for c in self.calls(e) {
2154
2156
  let Some(k) = self.subj(&c.f) else { continue };
2155
2157
  let hit = matches!(c.args.get(k), Some(Expr { kind: ExprKind::Var(n), .. }) if n == p);
@@ -2188,7 +2190,7 @@ impl Driver {
2188
2190
  return false;
2189
2191
  }
2190
2192
  }
2191
- walkBlockRef(&Block { stmts: stmts.to_vec() }, &mut |e| match &e.kind {
2193
+ walkStmtsRef(stmts, &mut |e| match &e.kind {
2192
2194
  ExprKind::FnCall(c) => {
2193
2195
  let known = !self.bound.contains(&c.name)
2194
2196
  && (self.ctors.contains(&c.name)
compiler/core/src/visit.rs CHANGED
@@ -319,7 +319,12 @@ pub fn isTag(n: &str) -> bool {
319
319
 
320
320
  /// Pre-order callback over every expression of `b`, closure bodies and catch blocks included.
321
321
  pub fn walkBlockRef(b: &Block, f: &mut dyn FnMut(&Expr)) {
322
+ walkStmtsRef(&b.stmts, f)
323
+ }
324
+
325
+ /// `walkBlockRef` over statements that are not a whole block (no copy into one needed).
326
+ pub fn walkStmtsRef(stmts: &[Stmt], f: &mut dyn FnMut(&Expr)) {
322
- for s in &b.stmts {
327
+ for s in stmts {
323
328
  for e in stmtExprRefs(s) {
324
329
  walkRef(e, f);
325
330
  }