Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions compiler-rust/src/lambda/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ pub struct LamArena {
str_keys: Vec<(u32, u32)>,
text: String,
catches: Vec<CatchPayload>,
/// 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.
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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(&copy, assigning), assigned);
}

#[test]
fn import_export_round_trips() {
let lam = sample();
Expand Down
7 changes: 6 additions & 1 deletion compiler-rust/src/lambda/lambda_traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading