From 6dc6386f77c09f5171eeafe30127dbd0d9122cb8 Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Tue, 25 Oct 2022 09:09:02 -0500 Subject: [PATCH] DefTypes store slices into type indeces --- crates/compiler/can/src/constraint.rs | 52 +++++++++++++++++++++++---- crates/compiler/solve/src/solve.rs | 13 +++---- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/crates/compiler/can/src/constraint.rs b/crates/compiler/can/src/constraint.rs index a4a92ec017..3544681b1d 100644 --- a/crates/compiler/can/src/constraint.rs +++ b/crates/compiler/can/src/constraint.rs @@ -13,6 +13,7 @@ use roc_types::types::{Category, PatternCategory, Type}; pub struct Constraints { pub constraints: Vec, pub types: Vec>, + pub type_slices: Vec, pub variables: Vec, pub loc_symbols: Vec<(Symbol, Region)>, pub let_constraints: Vec, @@ -33,6 +34,7 @@ impl std::fmt::Debug for Constraints { f.debug_struct("Constraints") .field("constraints", &self.constraints) .field("types", &"") + .field("type_slices", &self.type_slices) .field("variables", &self.variables) .field("loc_symbols", &self.loc_symbols) .field("let_constraints", &self.let_constraints) @@ -65,6 +67,7 @@ impl Constraints { pub fn new() -> Self { let constraints = Vec::new(); let mut types = Vec::new(); + let type_slices = Vec::with_capacity(16); let variables = Vec::new(); let loc_symbols = Vec::new(); let let_constraints = Vec::new(); @@ -119,6 +122,7 @@ impl Constraints { Self { constraints, types, + type_slices, variables, loc_symbols, let_constraints, @@ -372,24 +376,24 @@ impl Constraints { fn def_types_slice(&mut self, it: I) -> DefTypes where - I: IntoIterator)>, + I: IntoIterator)>, I::IntoIter: ExactSizeIterator, { let it = it.into_iter(); - let types_start = self.types.len(); + let types_start = self.type_slices.len(); let loc_symbols_start = self.loc_symbols.len(); // because we have an ExactSizeIterator, we can reserve space here let length = it.len(); - self.types.reserve(length); + self.type_slices.reserve(length); self.loc_symbols.reserve(length); for (symbol, loc_type) in it { let Loc { region, value } = loc_type; - self.types.push(Cell::new(value)); + self.type_slices.push(value); self.loc_symbols.push((symbol, region)); } @@ -469,10 +473,27 @@ impl Constraints { self.constraints.push(defs_constraint); self.constraints.push(ret_constraint); + let def_types = { + let types = def_types + .into_iter() + .map(|(sym, Loc { region, value })| { + let type_index = self.push_type(value); + ( + sym, + Loc { + region, + value: type_index, + }, + ) + }) + .collect::>(); + self.def_types_slice(types) + }; + let let_constraint = LetConstraint { rigid_vars: self.variable_slice(rigid_vars), flex_vars: self.variable_slice(flex_vars), - def_types: self.def_types_slice(def_types), + def_types, defs_and_ret_constraint, }; @@ -515,10 +536,27 @@ impl Constraints { self.constraints.push(Constraint::True); self.constraints.push(module_constraint); + let def_types = { + let types = def_types + .into_iter() + .map(|(sym, Loc { region, value })| { + let type_index = self.push_type(value); + ( + sym, + Loc { + region, + value: type_index, + }, + ) + }) + .collect::>(); + self.def_types_slice(types) + }; + let let_contraint = LetConstraint { rigid_vars: self.variable_slice(rigid_vars), flex_vars: Slice::default(), - def_types: self.def_types_slice(def_types), + def_types, defs_and_ret_constraint, }; @@ -756,7 +794,7 @@ pub enum Constraint { #[derive(Debug, Clone, Copy, Default)] pub struct DefTypes { - pub types: Slice, + pub types: Slice, pub loc_symbols: Slice<(Symbol, Region)>, } diff --git a/crates/compiler/solve/src/solve.rs b/crates/compiler/solve/src/solve.rs index 700b718b64..a646a7b8eb 100644 --- a/crates/compiler/solve/src/solve.rs +++ b/crates/compiler/solve/src/solve.rs @@ -2247,21 +2247,22 @@ impl LocalDefVarsVec<(Symbol, Loc)> { subs: &mut Subs, def_types_slice: roc_can::constraint::DefTypes, ) -> Self { - let types_slice = &constraints.types[def_types_slice.types.indices()]; + let type_indices_slice = &constraints.type_slices[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(types_slice.len()); + let mut local_def_vars = Self::with_length(type_indices_slice.len()); - for (&(symbol, region), typ_cell) in (loc_symbols_slice.iter()).zip(types_slice) { - let var = type_cell_to_var( + for (&(symbol, region), typ_index) in (loc_symbols_slice.iter()).zip(type_indices_slice) { + let var = either_type_index_to_var( + constraints, subs, rank, + pools, problems, abilities_store, obligation_cache, - pools, aliases, - typ_cell, + *typ_index, ); local_def_vars.push((symbol, Loc { value: var, region }));