Skip to content

Commit

Permalink
perf(vm/translate): Avoid one field lookup loop in pattern desugaring
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Jan 7, 2019
1 parent 37fe876 commit 292edb7
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions vm/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ impl<'a, 'e> PatternTranslator<'a, 'e> {
.map(|equation| *equation.patterns.first().unwrap())
};

let (pattern, replacements) = self.pattern_identifiers(first_iter());
let (core_pattern, replacements) = self.pattern_identifiers(first_iter());

// Gather the inner patterns so we can prepend them to equations
let temp = first_iter()
Expand All @@ -1000,22 +1000,41 @@ impl<'a, 'e> PatternTranslator<'a, 'e> {
ref fields,
..
} => {
let record_type = remove_aliases_cow(&self.0.env, typ);

let mut record_type = None;
// Core fields appear in the same order as the normal pattern so we can
// get the types from it cheaply
let core_fields = match &core_pattern {
Pattern::Record(core_fields) => core_fields,
_ => unreachable!(),
};
fields
.iter()
.map(|field| {
.zip(core_fields)
.map(|(field, core_field)| {
field.value.as_ref().map(Cow::Borrowed).unwrap_or_else(|| {
let field_type = record_type
.row_iter()
.find(|f| f.name.name_eq(&field.name.value))
.map(|f| f.typ.clone())
.unwrap_or_else(|| Type::hole());
let typ = if field.name.value == core_field.0.name {
core_field.0.typ.clone()
} else {
// If the field has been renamed we need to go the slo path
// and do a lookup but this should be rare
if record_type.is_none() {
record_type =
Some(remove_aliases_cow(&self.0.env, typ));
}
record_type
.as_ref()
.unwrap()
.row_iter()
.find(|f| f.name.name_eq(&field.name.value))
.map(|f| f.typ.clone())
.unwrap_or_else(Type::hole)
};

Cow::Owned(spanned(
Span::default(),
ast::Pattern::Ident(TypedIdent {
name: field.name.value.clone(),
typ: field_type,
typ,
}),
))
})
Expand Down Expand Up @@ -1045,13 +1064,13 @@ impl<'a, 'e> PatternTranslator<'a, 'e> {
})
.collect::<Vec<_>>();

let new_variables = self.insert_new_variables(&pattern, variables);
let new_variables = self.insert_new_variables(&core_pattern, variables);

let expr = self.translate(default, &new_variables, &new_equations);
let expr = replace_variables(&self.0.allocator, &replacements, expr);

Alternative {
pattern: pattern,
pattern: core_pattern,
expr: expr,
}
};
Expand Down Expand Up @@ -1539,6 +1558,7 @@ impl<'a, 'e> PatternTranslator<'a, 'e> {
..
} => {
let typ = remove_aliases_cow(&self.0.env, typ);

for (i, field) in fields.iter().enumerate() {
if !add_duplicate_ident(
&mut replacements,
Expand Down

0 comments on commit 292edb7

Please sign in to comment.