short-circuit aliases

This commit is contained in:
Folkert 2022-02-28 23:37:33 +01:00
parent f30c07edd0
commit c18befeccf
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
3 changed files with 24 additions and 22 deletions

View file

@ -3102,7 +3102,7 @@ fn run_solve<'a>(
} }
let (solved_subs, solved_env, problems) = let (solved_subs, solved_env, problems) =
roc_solve::module::run_solve(aliases, rigid_variables, constraint, var_store); roc_solve::module::run_solve(rigid_variables, constraint, var_store);
let mut exposed_vars_by_symbol: MutMap<Symbol, Variable> = solved_env.vars_by_symbol.clone(); let mut exposed_vars_by_symbol: MutMap<Symbol, Variable> = solved_env.vars_by_symbol.clone();
exposed_vars_by_symbol.retain(|k, _| exposed_symbols.contains(k)); exposed_vars_by_symbol.retain(|k, _| exposed_symbols.contains(k));
@ -3115,7 +3115,7 @@ fn run_solve<'a>(
exposed_symbols: exposed_symbols.into_iter().collect::<Vec<_>>(), exposed_symbols: exposed_symbols.into_iter().collect::<Vec<_>>(),
solved_types, solved_types,
problems, problems,
aliases: solved_env.aliases, aliases,
}; };
// Record the final timings // Record the final timings

View file

@ -17,14 +17,13 @@ pub struct SolvedModule {
} }
pub fn run_solve( pub fn run_solve(
aliases: MutMap<Symbol, Alias>,
rigid_variables: MutMap<Variable, Lowercase>, rigid_variables: MutMap<Variable, Lowercase>,
constraint: Constraint, constraint: Constraint,
var_store: VarStore, var_store: VarStore,
) -> (Solved<Subs>, solve::Env, Vec<solve::TypeError>) { ) -> (Solved<Subs>, solve::Env, Vec<solve::TypeError>) {
let env = solve::Env { let env = solve::Env {
vars_by_symbol: MutMap::default(), vars_by_symbol: MutMap::default(),
aliases, aliases: MutMap::default(),
}; };
let mut subs = Subs::new_from_varstore(var_store); let mut subs = Subs::new_from_varstore(var_store);

View file

@ -82,6 +82,23 @@ pub struct Env {
pub aliases: MutMap<Symbol, Alias>, pub aliases: MutMap<Symbol, Alias>,
} }
impl Env {
fn get_var_by_symbol(&self, symbol: &Symbol) -> Option<Variable> {
self.vars_by_symbol.get(symbol).copied()
}
fn insert_symbol_var_if_vacant(&mut self, symbol: Symbol, var: Variable) {
match self.vars_by_symbol.entry(symbol) {
Entry::Occupied(_) => {
// keep the existing value
}
Entry::Vacant(vacant) => {
vacant.insert(var);
}
}
}
}
const DEFAULT_POOLS: usize = 8; const DEFAULT_POOLS: usize = 8;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -312,7 +329,7 @@ fn solve(
} }
} }
Lookup(symbol, expectation, region) => { Lookup(symbol, expectation, region) => {
match env.vars_by_symbol.get(symbol) { match env.get_var_by_symbol(symbol) {
Some(var) => { Some(var) => {
// Deep copy the vars associated with this symbol before unifying them. // Deep copy the vars associated with this symbol before unifying them.
// Otherwise, suppose we have this: // Otherwise, suppose we have this:
@ -335,7 +352,7 @@ fn solve(
// then we copy from that module's Subs into our own. If the value // then we copy from that module's Subs into our own. If the value
// is being looked up in this module, then we use our Subs as both // is being looked up in this module, then we use our Subs as both
// the source and destination. // the source and destination.
let actual = deep_copy_var(subs, rank, pools, *var); let actual = deep_copy_var(subs, rank, pools, var);
let expected = type_to_var( let expected = type_to_var(
subs, subs,
rank, rank,
@ -484,14 +501,7 @@ fn solve(
let mut new_env = env.clone(); let mut new_env = env.clone();
for (symbol, loc_var) in local_def_vars.iter() { for (symbol, loc_var) in local_def_vars.iter() {
match new_env.vars_by_symbol.entry(*symbol) { new_env.insert_symbol_var_if_vacant(*symbol, loc_var.value);
Entry::Occupied(_) => {
// keep the existing value
}
Entry::Vacant(vacant) => {
vacant.insert(loc_var.value);
}
}
} }
stack.push(Work::Constraint { stack.push(Work::Constraint {
@ -625,14 +635,7 @@ fn solve(
let mut new_env = env.clone(); let mut new_env = env.clone();
for (symbol, loc_var) in local_def_vars.iter() { for (symbol, loc_var) in local_def_vars.iter() {
match new_env.vars_by_symbol.entry(*symbol) { new_env.insert_symbol_var_if_vacant(*symbol, loc_var.value);
Entry::Occupied(_) => {
// keep the existing value
}
Entry::Vacant(vacant) => {
vacant.insert(loc_var.value);
}
}
} }
// Note that this vars_by_symbol is the one returned by the // Note that this vars_by_symbol is the one returned by the