From 6f45611efe2368f40d79171ac43946dbc28706c0 Mon Sep 17 00:00:00 2001 From: "open-session-submodule[bot]" <341020755+open-session-submodule[bot]@users.noreply.github.com> Date: Wed, 11 Nov 2026 15:22:43 +0100 Subject: [PATCH 1/2] Skip the assigned-variable walk when a Lambda arena holds no assignment ec8f9715ee (evaluate the arguments of an inlined call left to right) asks which variables a term assigns in four places: once per run of the alias and let simplification passes, once before compiling to JS, and once for every inlined call. Each answer walked the whole term and built a hash set, and the set is almost always empty: only a mutable variable that is kept local produces an assignment. The arena now records whether an assignment node was ever added to it, and `arena_assigned_variables` returns the empty set without walking when none was. With an assignment anywhere in the arena the walk runs as before, so the result is the same in every case. Clean build of a 3,735-module application, one-codegen-unit release build, the same binary with and without the walk, alternating pairs, 1 worker: 83.82 -> 83.33 G instructions (paired -0.49 +- 0.14, n=8). The 3,980 generated files are identical. Co-authored-by: Jaap Frolich Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1c9fa-0c5d-7912-a93b-3e7f38e53547 Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com> --- compiler-rust/src/lambda/arena.rs | 41 +++++++++++++++++++++ compiler-rust/src/lambda/lambda_traverse.rs | 7 +++- 2 files changed, 47 insertions(+), 1 deletion(-) 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 } From f0e9d04749794b27dc3d58d4d075daece60674c1 Mon Sep 17 00:00:00 2001 From: "open-session-submodule[bot]" <341020755+open-session-submodule[bot]@users.noreply.github.com> Date: Wed, 11 Nov 2026 15:22:47 +0100 Subject: [PATCH 2/2] Document the skipped assigned-variable walk Co-authored-by: Jaap Frolich Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1c9fa-0c5d-7912-a93b-3e7f38e53547 Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com> --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) 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