Refactor AST nodes to use Rc
This commit refactors various AST node types to use `Rc` (Reference Counting) instead of `Box`. This change is primarily driven by the need for shared ownership and more efficient handling of potentially recursive or shared data structures within the AST. The main benefits of this change include: - **Reduced cloning:** `Rc` allows multiple parts of the AST to refer to the same node without needing to clone the entire node. This is particularly beneficial for optimization passes where nodes might be visited multiple times. - **Enabling sharing:** It makes it easier to represent scenarios where a single AST node might be a child of multiple parent nodes. - **Performance improvements:** By avoiding unnecessary deep copies of AST nodes, this change can lead to performance gains, especially during complex compilation phases.
This commit is contained in:
@@ -53,13 +53,13 @@ impl Specializer {
|
||||
let (new_kind, metrics) = match node.kind {
|
||||
BoundKind::Call { callee, args } => {
|
||||
let (new_callee, new_args, _ret_ty) =
|
||||
self.specialize_call_logic(*callee, *args, node.ty.original.ty.clone());
|
||||
self.specialize_call_logic(callee, args, node.ty.original.ty.clone());
|
||||
|
||||
let new_metrics = node.ty.clone();
|
||||
(
|
||||
BoundKind::Call {
|
||||
callee: Box::new(new_callee),
|
||||
args: Box::new(new_args),
|
||||
callee: Rc::new(new_callee),
|
||||
args: Rc::new(new_args),
|
||||
},
|
||||
new_metrics,
|
||||
)
|
||||
@@ -70,9 +70,9 @@ impl Specializer {
|
||||
then_br,
|
||||
else_br,
|
||||
} => {
|
||||
let cond = Box::new(self.visit_node(*cond));
|
||||
let then_br = Box::new(self.visit_node(*then_br));
|
||||
let else_br = else_br.map(|e| Box::new(self.visit_node(*e)));
|
||||
let cond = Rc::new(self.visit_node(cond.as_ref().clone()));
|
||||
let then_br = Rc::new(self.visit_node(then_br.as_ref().clone()));
|
||||
let else_br = else_br.map(|e| Rc::new(self.visit_node(e.as_ref().clone())));
|
||||
(
|
||||
BoundKind::If {
|
||||
cond,
|
||||
@@ -83,7 +83,7 @@ impl Specializer {
|
||||
)
|
||||
}
|
||||
BoundKind::Block { exprs } => {
|
||||
let exprs = exprs.into_iter().map(|e| self.visit_node(e)).collect();
|
||||
let exprs = exprs.into_iter().map(|e| Rc::new(self.visit_node(e.as_ref().clone()))).collect();
|
||||
(BoundKind::Block { exprs }, node.ty.clone())
|
||||
}
|
||||
BoundKind::Lambda {
|
||||
@@ -93,7 +93,7 @@ impl Specializer {
|
||||
positional_count,
|
||||
} => {
|
||||
let params = Rc::new(self.visit_node(params.as_ref().clone()));
|
||||
let body = Rc::new(self.visit_node((*body).clone()));
|
||||
let body = Rc::new(self.visit_node(body.as_ref().clone()));
|
||||
(
|
||||
BoundKind::Lambda {
|
||||
params,
|
||||
@@ -111,7 +111,7 @@ impl Specializer {
|
||||
value,
|
||||
captured_by,
|
||||
} => {
|
||||
let value = Box::new(self.visit_node(*value));
|
||||
let value = Rc::new(self.visit_node(value.as_ref().clone()));
|
||||
(
|
||||
BoundKind::Define {
|
||||
name: name.clone(),
|
||||
@@ -124,22 +124,22 @@ impl Specializer {
|
||||
)
|
||||
}
|
||||
BoundKind::Set { addr, value } => {
|
||||
let value = Box::new(self.visit_node(*value));
|
||||
let value = Rc::new(self.visit_node(value.as_ref().clone()));
|
||||
(BoundKind::Set { addr, value }, node.ty.clone())
|
||||
}
|
||||
BoundKind::Tuple { elements } => {
|
||||
let elements = elements.into_iter().map(|e| self.visit_node(e)).collect();
|
||||
let elements = elements.into_iter().map(|e| Rc::new(self.visit_node(e.as_ref().clone()))).collect();
|
||||
(BoundKind::Tuple { elements }, node.ty.clone())
|
||||
}
|
||||
BoundKind::Record { layout, values } => {
|
||||
let values = values.into_iter().map(|v| self.visit_node(v)).collect();
|
||||
let values = values.into_iter().map(|v| Rc::new(self.visit_node(v.as_ref().clone()))).collect();
|
||||
(BoundKind::Record { layout, values }, node.ty.clone())
|
||||
}
|
||||
BoundKind::Expansion {
|
||||
original_call,
|
||||
bound_expanded,
|
||||
} => {
|
||||
let bound_expanded = Box::new(self.visit_node(*bound_expanded));
|
||||
let bound_expanded = Rc::new(self.visit_node(bound_expanded.as_ref().clone()));
|
||||
(
|
||||
BoundKind::Expansion {
|
||||
original_call,
|
||||
@@ -160,12 +160,12 @@ impl Specializer {
|
||||
|
||||
fn specialize_call_logic(
|
||||
&self,
|
||||
callee: AnalyzedNode,
|
||||
args: AnalyzedNode,
|
||||
callee: Rc<AnalyzedNode>,
|
||||
args: Rc<AnalyzedNode>,
|
||||
original_ty: StaticType,
|
||||
) -> (AnalyzedNode, AnalyzedNode, StaticType) {
|
||||
let new_callee = self.visit_node(callee);
|
||||
let new_args = self.visit_node(args);
|
||||
let new_callee = self.visit_node(callee.as_ref().clone());
|
||||
let new_args = self.visit_node(args.as_ref().clone());
|
||||
|
||||
let address = if let BoundKind::Get { addr, .. } = &new_callee.kind {
|
||||
*addr
|
||||
@@ -233,9 +233,9 @@ impl Specializer {
|
||||
self.cache
|
||||
.borrow_mut()
|
||||
.insert(key, (compiled_val.clone(), ret_ty.clone()));
|
||||
let flat_elements = match new_args.kind {
|
||||
BoundKind::Tuple { elements } => elements,
|
||||
_ => vec![new_args.clone()],
|
||||
let flat_elements = match &new_args.kind {
|
||||
BoundKind::Tuple { elements } => elements.clone(),
|
||||
_ => vec![Rc::new(new_args.clone())],
|
||||
};
|
||||
let flat_types = flat_elements
|
||||
.iter()
|
||||
|
||||
Reference in New Issue
Block a user