mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 15:51:12 +00:00
optimizations for known aliases and identity substitutions
This commit is contained in:
parent
fde28033bc
commit
ad6fd18b05
1 changed files with 30 additions and 6 deletions
|
@ -178,8 +178,13 @@ impl Aliases {
|
||||||
let new_type_variables = &subs.variables[alias_variables.type_variables().indices()];
|
let new_type_variables = &subs.variables[alias_variables.type_variables().indices()];
|
||||||
|
|
||||||
for (old, new) in old_type_variables.iter_mut().zip(new_type_variables) {
|
for (old, new) in old_type_variables.iter_mut().zip(new_type_variables) {
|
||||||
substitutions.insert(*old, *new);
|
// if constraint gen duplicated a type these variables could be the same
|
||||||
*old = *new;
|
// (happens very often in practice)
|
||||||
|
if *old != *new {
|
||||||
|
substitutions.insert(*old, *new);
|
||||||
|
|
||||||
|
*old = *new;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let old_lambda_set_variables = delayed_variables.lambda_set_variables(&mut self.variables);
|
let old_lambda_set_variables = delayed_variables.lambda_set_variables(&mut self.variables);
|
||||||
|
@ -190,11 +195,15 @@ impl Aliases {
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.zip(new_lambda_set_variables)
|
.zip(new_lambda_set_variables)
|
||||||
{
|
{
|
||||||
substitutions.insert(*old, *new);
|
if *old != *new {
|
||||||
*old = *new;
|
substitutions.insert(*old, *new);
|
||||||
|
*old = *new;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
typ.substitute_variables(&substitutions);
|
if !substitutions.is_empty() {
|
||||||
|
typ.substitute_variables(&substitutions);
|
||||||
|
}
|
||||||
|
|
||||||
// assumption: an alias does not (transitively) syntactically contain itself
|
// assumption: an alias does not (transitively) syntactically contain itself
|
||||||
// (if it did it would have to be a recursive tag union)
|
// (if it did it would have to be a recursive tag union)
|
||||||
|
@ -202,12 +211,13 @@ impl Aliases {
|
||||||
|
|
||||||
std::mem::swap(typ, &mut t);
|
std::mem::swap(typ, &mut t);
|
||||||
|
|
||||||
let alias_variable = type_to_var(subs, rank, pools, self, &t);
|
let alias_variable = type_to_variable(subs, rank, pools, arena, self, &t);
|
||||||
|
|
||||||
{
|
{
|
||||||
match self.aliases.iter_mut().find(|(s, _, _)| *s == symbol) {
|
match self.aliases.iter_mut().find(|(s, _, _)| *s == symbol) {
|
||||||
None => unreachable!(),
|
None => unreachable!(),
|
||||||
Some((_, typ, _)) => {
|
Some((_, typ, _)) => {
|
||||||
|
// swap typ back
|
||||||
std::mem::swap(typ, &mut t);
|
std::mem::swap(typ, &mut t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1089,6 +1099,20 @@ impl RegisterVariable {
|
||||||
Variable(var) => Direct(*var),
|
Variable(var) => Direct(*var),
|
||||||
EmptyRec => Direct(Variable::EMPTY_RECORD),
|
EmptyRec => Direct(Variable::EMPTY_RECORD),
|
||||||
EmptyTagUnion => Direct(Variable::EMPTY_TAG_UNION),
|
EmptyTagUnion => Direct(Variable::EMPTY_TAG_UNION),
|
||||||
|
Type::DelayedAlias(AliasCommon { symbol, .. }) => {
|
||||||
|
if let Some(reserved) = Variable::get_reserved(*symbol) {
|
||||||
|
if rank.is_none() {
|
||||||
|
// reserved variables are stored with rank NONE
|
||||||
|
return Direct(reserved);
|
||||||
|
} else {
|
||||||
|
// for any other rank, we need to copy; it takes care of adjusting the rank
|
||||||
|
let copied = deep_copy_var_in(subs, rank, pools, reserved, arena);
|
||||||
|
return Direct(copied);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Deferred
|
||||||
|
}
|
||||||
Type::Alias { symbol, .. } => {
|
Type::Alias { symbol, .. } => {
|
||||||
if let Some(reserved) = Variable::get_reserved(*symbol) {
|
if let Some(reserved) = Variable::get_reserved(*symbol) {
|
||||||
if rank.is_none() {
|
if rank.is_none() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue