diff --git a/CHANGELOG.md b/CHANGELOG.md index eec143420d..9a903f37cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -84,6 +84,8 @@ #### :house: Internal +- Skip the walk that collects a program's assigned variables when it contains no assignment; the walk ran for every optimization pass and every inlined call. https://github.com/jfrolich/rescript-rust/pull/94 + - Load a compiled module's interface into its first dependent without decoding it again, and keep the syntax tree nodes the built-in frontend pass leaves unchanged instead of copying them. https://github.com/jfrolich/rescript-rust/pull/89 https://github.com/jfrolich/rescript-rust/pull/85 - Compute the item digests and item references of a compiled module's interface once, when it is published, instead of decoding the stored interface again for each. https://github.com/jfrolich/rescript-rust/pull/87 diff --git a/compiler-rust/src/lambda/arena.rs b/compiler-rust/src/lambda/arena.rs index 1c86faa209..dbef59ad50 100644 --- a/compiler-rust/src/lambda/arena.rs +++ b/compiler-rust/src/lambda/arena.rs @@ -364,6 +364,10 @@ pub struct LamArena { str_keys: Vec<(u32, u32)>, text: String, catches: Vec, + /// Whether an assignment node was ever added. Most programs have none, + /// and the passes that ask which variables a term assigns then need no + /// walk (see `lambda_traverse::arena_assigned_variables`). + has_assign: bool, } /// A program: an arena and the root of its term. @@ -413,9 +417,16 @@ impl LamArena { str_keys: Vec::new(), text: String::new(), catches: Vec::new(), + has_assign: false, } } + /// False when no term of this arena can contain an assignment. + #[inline] + pub fn may_assign(&self) -> bool { + self.has_assign + } + pub fn node_count(&self) -> usize { self.nodes.len() } @@ -693,6 +704,7 @@ impl LamArena { #[inline] pub fn push(&mut self, node: Node) -> Lam { let id = Lam(checked_u32(self.nodes.len())); + self.has_assign |= matches!(node, Node::Assign { .. }); self.nodes.push(node); id } @@ -2544,6 +2556,35 @@ mod tests { ); } + #[test] + fn an_arena_without_assignments_needs_no_walk_for_assigned_variables() { + use crate::lambda::lambda_traverse::arena_assigned_variables; + let mut a = LamArena::new(); + let x = Ident::create_local("x"); + let y = Ident::create_local("y"); + let read = a.var(x); + let unit = a.unit(); + let pure = a.seq(read, unit); + assert!(!a.may_assign()); + assert!(arena_assigned_variables(&a, pure).is_empty()); + + // An assignment anywhere in the arena brings the walk back, for + // every term: one that contains it and one that does not. + let one = a.unit(); + let assign = a.assign(y, one); + let assigning = a.seq(pure, assign); + assert!(a.may_assign()); + let assigned = arena_assigned_variables(&a, assigning); + assert_eq!(assigned.len(), 1); + assert!(assigned.contains(&y)); + assert!(arena_assigned_variables(&a, pure).is_empty()); + + // A copy of the arena keeps the answer. + let copy = a.clone(); + assert!(copy.may_assign()); + assert_eq!(arena_assigned_variables(©, assigning), assigned); + } + #[test] fn import_export_round_trips() { let lam = sample(); diff --git a/compiler-rust/src/lambda/lambda_traverse.rs b/compiler-rust/src/lambda/lambda_traverse.rs index f092a01afa..6dbdf3d11f 100644 --- a/compiler-rust/src/lambda/lambda_traverse.rs +++ b/compiler-rust/src/lambda/lambda_traverse.rs @@ -46,7 +46,12 @@ pub fn arena_assigned_variables( a.iter_children(lam, |child| walk(a, child, assigned)); } let mut assigned = ahash::AHashSet::new(); - walk(a, lam, &mut assigned); + // Assignments are rare (only a mutable variable kept local makes them), + // and this is asked for every pass and every inlined call: skip the walk + // of the whole term when the arena holds none. + if a.may_assign() { + walk(a, lam, &mut assigned); + } assigned }