optimize variable substitution in instantiate_rigids

This commit is contained in:
Folkert 2022-03-21 20:18:18 +01:00
parent 4c37b6f5fb
commit dfa5710932
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C

View file

@ -10,7 +10,7 @@ use roc_can::expected::PExpected;
use roc_can::expr::Expr::{self, *}; use roc_can::expr::Expr::{self, *};
use roc_can::expr::{AccessorData, ClosureData, Field, WhenBranch}; use roc_can::expr::{AccessorData, ClosureData, Field, WhenBranch};
use roc_can::pattern::Pattern; use roc_can::pattern::Pattern;
use roc_collections::all::{HumanIndex, ImMap, MutMap, SendMap}; use roc_collections::all::{HumanIndex, MutMap, SendMap};
use roc_module::ident::{Lowercase, TagName}; use roc_module::ident::{Lowercase, TagName};
use roc_module::symbol::{ModuleId, Symbol}; use roc_module::symbol::{ModuleId, Symbol};
use roc_region::all::{Loc, Region}; use roc_region::all::{Loc, Region};
@ -1670,14 +1670,14 @@ fn instantiate_rigids(
let mut annotation = annotation.clone(); let mut annotation = annotation.clone();
let mut new_rigid_variables: Vec<Variable> = Vec::new(); let mut new_rigid_variables: Vec<Variable> = Vec::new();
let mut rigid_substitution: ImMap<Variable, Type> = ImMap::default(); let mut rigid_substitution: MutMap<Variable, Variable> = MutMap::default();
for named in introduced_vars.named.iter() { for named in introduced_vars.named.iter() {
use std::collections::hash_map::Entry::*; use std::collections::hash_map::Entry::*;
match ftv.entry(named.name.clone()) { match ftv.entry(named.name.clone()) {
Occupied(occupied) => { Occupied(occupied) => {
let existing_rigid = occupied.get(); let existing_rigid = occupied.get();
rigid_substitution.insert(named.variable, Type::Variable(*existing_rigid)); rigid_substitution.insert(named.variable, *existing_rigid);
} }
Vacant(vacant) => { Vacant(vacant) => {
// It's possible to use this rigid in nested defs // It's possible to use this rigid in nested defs
@ -1698,7 +1698,7 @@ fn instantiate_rigids(
// Instantiate rigid variables // Instantiate rigid variables
if !rigid_substitution.is_empty() { if !rigid_substitution.is_empty() {
annotation.substitute(&rigid_substitution); annotation.substitute_variables(&rigid_substitution);
} }
// TODO investigate when we can skip this. It seems to only be required for correctness // TODO investigate when we can skip this. It seems to only be required for correctness