Refactor optimizer utilities

Removes unused `flatten_tuple` function from optimizer utilities. The
functionality was moved to the `Folder` struct, making it more
contextually appropriate. This change streamlines the utility module and
improves code organization.
This commit is contained in:
Michael Schimmel
2026-02-25 21:26:12 +01:00
parent cf87bbeac5
commit 81c805f07e
6 changed files with 456 additions and 460 deletions
+26 -8
View File
@@ -1,6 +1,4 @@
use crate::ast::compiler::bound_nodes::{
Address, AnalyzedNode, BoundKind, GlobalIdx, UpvalueIdx,
};
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, GlobalIdx, UpvalueIdx};
use crate::ast::nodes::Node;
use crate::ast::types::{Purity, Value};
use crate::ast::vm::Closure;
@@ -8,9 +6,9 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use super::substitution_map::SubstitutionMap;
use super::folder::Folder;
use super::inliner::Inliner;
use super::substitution_map::SubstitutionMap;
use super::utils::{PathTracker, UsageInfo};
pub struct Optimizer {
@@ -177,6 +175,9 @@ impl Optimizer {
let args = self.visit_node(*args, sub, path);
if self.enabled {
let mut arg_nodes = Vec::new();
self.flatten_tuple(args.clone(), &mut arg_nodes);
if let BoundKind::Lambda {
params,
body,
@@ -192,7 +193,7 @@ impl Optimizer {
path.inlining_depth += 1;
let mut inner_sub = SubstitutionMap::new();
let collapsed = if inliner
.prepare_beta_reduction(params, &args, body, &mut inner_sub)
.prepare_beta_reduction(params, &arg_nodes, body, &mut inner_sub)
.is_some()
{
Some(self.visit_node((**body).clone(), &mut inner_sub, path))
@@ -231,7 +232,7 @@ impl Optimizer {
path.inlining_stack.insert(*idx);
path.inlining_depth += 1;
let collapsed = if inliner
.prepare_beta_reduction(params, &args, body, &mut inner_sub)
.prepare_beta_reduction(params, &arg_nodes, body, &mut inner_sub)
.is_some()
{
Some(self.visit_node((**body).clone(), &mut inner_sub, path))
@@ -273,7 +274,7 @@ impl Optimizer {
let collapsed = if inliner
.prepare_beta_reduction(
&closure.parameter_node,
&args,
&arg_nodes,
&inlined_body,
&mut closure_sub,
)
@@ -291,7 +292,7 @@ impl Optimizer {
}
}
if let Some(folded) = folder.try_fold_pure(&callee, &args) {
if let Some(folded) = folder.try_fold_pure(&callee, &arg_nodes) {
return folded;
}
}
@@ -533,4 +534,21 @@ impl Optimizer {
ty: metrics,
}
}
fn flatten_tuple(&self, node: AnalyzedNode, into: &mut Vec<AnalyzedNode>) {
match node.kind {
BoundKind::Tuple { elements } => {
for el in elements {
self.flatten_tuple(el, into);
}
}
BoundKind::Record { fields } => {
for (_, v) in fields {
self.flatten_tuple(v, into);
}
}
BoundKind::Nop => {}
_ => into.push(node),
}
}
}