restructure how def_types are stored in Constraints

This commit is contained in:
Folkert 2022-03-05 21:47:49 +01:00
parent 6392e42166
commit c7c9a90d65
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
2 changed files with 31 additions and 27 deletions

View file

@ -11,7 +11,7 @@ pub struct Constraints {
pub constraints: Vec<Constraint>, pub constraints: Vec<Constraint>,
pub types: Vec<Type>, pub types: Vec<Type>,
pub variables: Vec<Variable>, pub variables: Vec<Variable>,
pub def_types: Vec<(Symbol, Loc<Index<Type>>)>, pub loc_symbols: Vec<(Symbol, Region)>,
pub let_constraints: Vec<LetConstraint>, pub let_constraints: Vec<LetConstraint>,
pub categories: Vec<Category>, pub categories: Vec<Category>,
pub pattern_categories: Vec<PatternCategory>, pub pattern_categories: Vec<PatternCategory>,
@ -32,7 +32,7 @@ impl Constraints {
let constraints = Vec::new(); let constraints = Vec::new();
let mut types = Vec::new(); let mut types = Vec::new();
let variables = Vec::new(); let variables = Vec::new();
let def_types = Vec::new(); let loc_symbols = Vec::new();
let let_constraints = Vec::new(); let let_constraints = Vec::new();
let mut categories = Vec::with_capacity(16); let mut categories = Vec::with_capacity(16);
let mut pattern_categories = Vec::with_capacity(16); let mut pattern_categories = Vec::with_capacity(16);
@ -78,7 +78,7 @@ impl Constraints {
constraints, constraints,
types, types,
variables, variables,
def_types, loc_symbols,
let_constraints, let_constraints,
categories, categories,
pattern_categories, pattern_categories,
@ -260,28 +260,33 @@ impl Constraints {
Slice::new(start as _, length as _) Slice::new(start as _, length as _)
} }
fn def_types_slice<I>(&mut self, it: I) -> Slice<(Symbol, Loc<Index<Type>>)> fn def_types_slice<I>(&mut self, it: I) -> DefTypes
where where
I: IntoIterator<Item = (Symbol, Loc<Type>)>, I: IntoIterator<Item = (Symbol, Loc<Type>)>,
I::IntoIter: ExactSizeIterator, I::IntoIter: ExactSizeIterator,
{ {
let it = it.into_iter(); let it = it.into_iter();
let start = self.def_types.len(); let types_start = self.types.len();
let loc_symbols_start = self.loc_symbols.len();
// because we have an ExactSizeIterator, we can reserve space here // because we have an ExactSizeIterator, we can reserve space here
self.def_types.reserve(it.len()); let length = it.len();
self.types.reserve(length);
self.loc_symbols.reserve(length);
for (symbol, loc_type) in it { for (symbol, loc_type) in it {
let Loc { region, value } = loc_type; let Loc { region, value } = loc_type;
let type_index = Index::push_new(&mut self.types, value);
self.def_types.push((symbol, Loc::at(region, type_index))); self.types.push(value);
self.loc_symbols.push((symbol, region));
} }
let length = self.def_types.len() - start; DefTypes {
types: Slice::new(types_start as _, length as _),
Slice::new(start as _, length as _) loc_symbols: Slice::new(loc_symbols_start as _, length as _),
}
} }
#[inline(always)] #[inline(always)]
@ -297,7 +302,7 @@ impl Constraints {
let let_contraint = LetConstraint { let let_contraint = LetConstraint {
rigid_vars: Slice::default(), rigid_vars: Slice::default(),
flex_vars: self.variable_slice(flex_vars), flex_vars: self.variable_slice(flex_vars),
def_types: Slice::default(), def_types: DefTypes::default(),
defs_and_ret_constraint, defs_and_ret_constraint,
}; };
@ -323,7 +328,7 @@ impl Constraints {
let let_contraint = LetConstraint { let let_contraint = LetConstraint {
rigid_vars: Slice::default(), rigid_vars: Slice::default(),
flex_vars: self.variable_slice(flex_vars), flex_vars: self.variable_slice(flex_vars),
def_types: Slice::default(), def_types: DefTypes::default(),
defs_and_ret_constraint, defs_and_ret_constraint,
}; };
@ -476,11 +481,17 @@ pub enum Constraint {
), ),
} }
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct DefTypes {
pub types: Slice<Type>,
pub loc_symbols: Slice<(Symbol, Region)>,
}
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct LetConstraint { pub struct LetConstraint {
pub rigid_vars: Slice<Variable>, pub rigid_vars: Slice<Variable>,
pub flex_vars: Slice<Variable>, pub flex_vars: Slice<Variable>,
pub def_types: Slice<(Symbol, Loc<Index<Type>>)>, pub def_types: DefTypes,
pub defs_and_ret_constraint: Index<(Constraint, Constraint)>, pub defs_and_ret_constraint: Index<(Constraint, Constraint)>,
} }

View file

@ -3,7 +3,6 @@ use roc_can::constraint::Constraint::{self, *};
use roc_can::constraint::{Constraints, LetConstraint}; use roc_can::constraint::{Constraints, LetConstraint};
use roc_can::expected::{Expected, PExpected}; use roc_can::expected::{Expected, PExpected};
use roc_collections::all::MutMap; use roc_collections::all::MutMap;
use roc_collections::soa::{Index, Slice};
use roc_module::ident::TagName; use roc_module::ident::TagName;
use roc_module::symbol::Symbol; use roc_module::symbol::Symbol;
use roc_region::all::{Loc, Region}; use roc_region::all::{Loc, Region};
@ -819,23 +818,17 @@ impl LocalDefVarsVec<(Symbol, Loc<Variable>)> {
pools: &mut Pools, pools: &mut Pools,
cached_aliases: &mut MutMap<Symbol, Variable>, cached_aliases: &mut MutMap<Symbol, Variable>,
subs: &mut Subs, subs: &mut Subs,
def_types_slice: Slice<(Symbol, Loc<Index<Type>>)>, def_types_slice: roc_can::constraint::DefTypes,
) -> Self { ) -> Self {
let def_types = &constraints.def_types[def_types_slice.indices()]; let types_slice = &constraints.types[def_types_slice.types.indices()];
let loc_symbols_slice = &constraints.loc_symbols[def_types_slice.loc_symbols.indices()];
let mut local_def_vars = Self::with_length(def_types.len()); let mut local_def_vars = Self::with_length(types_slice.len());
for (symbol, loc_type_index) in def_types.iter() { for ((symbol, region), typ) in loc_symbols_slice.iter().copied().zip(types_slice) {
let typ = &constraints.types[loc_type_index.value.index()];
let var = type_to_var(subs, rank, pools, cached_aliases, typ); let var = type_to_var(subs, rank, pools, cached_aliases, typ);
local_def_vars.push(( local_def_vars.push((symbol, Loc { value: var, region }));
*symbol,
Loc {
value: var,
region: loc_type_index.region,
},
));
} }
local_def_vars local_def_vars