Refactor Record's internal representation
This commit refactors the internal representation of `BoundKind::Record` to store a `RecordLayout` and a `Vec` of values, rather than a `Vec` of key-value pairs. This change simplifies the representation and improves efficiency by decoupling the record's structure from its specific values during compilation and analysis. The `RecordLayout` now defines the structure of the record, and the values are stored in a separate vector, ordered according to the layout. This allows for better optimization and type checking, as the record's shape is explicitly defined and immutable once created.
This commit is contained in:
@@ -50,27 +50,21 @@ impl<'a> Folder<'a> {
|
||||
|
||||
pub fn try_fold_record(
|
||||
&self,
|
||||
fields: &[(AnalyzedNode, AnalyzedNode)],
|
||||
layout: &std::sync::Arc<RecordLayout>,
|
||||
values: &[AnalyzedNode],
|
||||
template: &AnalyzedNode,
|
||||
) -> Option<AnalyzedNode> {
|
||||
let mut layout_fields = Vec::with_capacity(fields.len());
|
||||
let mut values = Vec::with_capacity(fields.len());
|
||||
let mut constant_values = Vec::with_capacity(values.len());
|
||||
|
||||
for (k_node, v_node) in fields {
|
||||
if let BoundKind::Constant(Value::Keyword(kw)) = &k_node.kind {
|
||||
if let BoundKind::Constant(val) = &v_node.kind {
|
||||
layout_fields.push((*kw, val.static_type()));
|
||||
values.push(val.clone());
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
for v_node in values {
|
||||
if let BoundKind::Constant(val) = &v_node.kind {
|
||||
constant_values.push(val.clone());
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
let layout = RecordLayout::get_or_create(layout_fields);
|
||||
let record_val = Value::Record(layout, Rc::new(values));
|
||||
let record_val = Value::Record(layout.clone(), Rc::new(constant_values));
|
||||
Some(self.make_constant_node(record_val, template))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user