From c7c9a90d65fc8ad958e0809e43daba8dc0440783 Mon Sep 17 00:00:00 2001 From: Folkert Date: Sat, 5 Mar 2022 21:47:49 +0100 Subject: [PATCH] restructure how def_types are stored in Constraints --- compiler/can/src/constraint.rs | 39 ++++++++++++++++++++++------------ compiler/solve/src/solve.rs | 19 ++++++----------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/compiler/can/src/constraint.rs b/compiler/can/src/constraint.rs index ae24ba78c9..70573d252a 100644 --- a/compiler/can/src/constraint.rs +++ b/compiler/can/src/constraint.rs @@ -11,7 +11,7 @@ pub struct Constraints { pub constraints: Vec, pub types: Vec, pub variables: Vec, - pub def_types: Vec<(Symbol, Loc>)>, + pub loc_symbols: Vec<(Symbol, Region)>, pub let_constraints: Vec, pub categories: Vec, pub pattern_categories: Vec, @@ -32,7 +32,7 @@ impl Constraints { let constraints = Vec::new(); let mut types = Vec::new(); let variables = Vec::new(); - let def_types = Vec::new(); + let loc_symbols = Vec::new(); let let_constraints = Vec::new(); let mut categories = Vec::with_capacity(16); let mut pattern_categories = Vec::with_capacity(16); @@ -78,7 +78,7 @@ impl Constraints { constraints, types, variables, - def_types, + loc_symbols, let_constraints, categories, pattern_categories, @@ -260,28 +260,33 @@ impl Constraints { Slice::new(start as _, length as _) } - fn def_types_slice(&mut self, it: I) -> Slice<(Symbol, Loc>)> + fn def_types_slice(&mut self, it: I) -> DefTypes where I: IntoIterator)>, I::IntoIter: ExactSizeIterator, { 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 - 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 { 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; - - Slice::new(start as _, length as _) + DefTypes { + types: Slice::new(types_start as _, length as _), + loc_symbols: Slice::new(loc_symbols_start as _, length as _), + } } #[inline(always)] @@ -297,7 +302,7 @@ impl Constraints { let let_contraint = LetConstraint { rigid_vars: Slice::default(), flex_vars: self.variable_slice(flex_vars), - def_types: Slice::default(), + def_types: DefTypes::default(), defs_and_ret_constraint, }; @@ -323,7 +328,7 @@ impl Constraints { let let_contraint = LetConstraint { rigid_vars: Slice::default(), flex_vars: self.variable_slice(flex_vars), - def_types: Slice::default(), + def_types: DefTypes::default(), defs_and_ret_constraint, }; @@ -476,11 +481,17 @@ pub enum Constraint { ), } +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct DefTypes { + pub types: Slice, + pub loc_symbols: Slice<(Symbol, Region)>, +} + #[derive(Debug, Clone, PartialEq)] pub struct LetConstraint { pub rigid_vars: Slice, pub flex_vars: Slice, - pub def_types: Slice<(Symbol, Loc>)>, + pub def_types: DefTypes, pub defs_and_ret_constraint: Index<(Constraint, Constraint)>, } diff --git a/compiler/solve/src/solve.rs b/compiler/solve/src/solve.rs index 7a4efb1e48..33dce4ec16 100644 --- a/compiler/solve/src/solve.rs +++ b/compiler/solve/src/solve.rs @@ -3,7 +3,6 @@ use roc_can::constraint::Constraint::{self, *}; use roc_can::constraint::{Constraints, LetConstraint}; use roc_can::expected::{Expected, PExpected}; use roc_collections::all::MutMap; -use roc_collections::soa::{Index, Slice}; use roc_module::ident::TagName; use roc_module::symbol::Symbol; use roc_region::all::{Loc, Region}; @@ -819,23 +818,17 @@ impl LocalDefVarsVec<(Symbol, Loc)> { pools: &mut Pools, cached_aliases: &mut MutMap, subs: &mut Subs, - def_types_slice: Slice<(Symbol, Loc>)>, + def_types_slice: roc_can::constraint::DefTypes, ) -> 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() { - let typ = &constraints.types[loc_type_index.value.index()]; + for ((symbol, region), typ) in loc_symbols_slice.iter().copied().zip(types_slice) { let var = type_to_var(subs, rank, pools, cached_aliases, typ); - local_def_vars.push(( - *symbol, - Loc { - value: var, - region: loc_type_index.region, - }, - )); + local_def_vars.push((symbol, Loc { value: var, region })); } local_def_vars