use EitherIndex<Type, Variable> to halve number of types stored

This commit is contained in:
Folkert 2022-03-13 01:46:49 +01:00
parent da03b0c2b3
commit b3d9f9c2de
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
3 changed files with 69 additions and 34 deletions

View file

@ -1,5 +1,5 @@
use crate::expected::{Expected, PExpected};
use roc_collections::soa::{Index, Slice};
use roc_collections::soa::{EitherIndex, Index, Slice};
use roc_module::ident::TagName;
use roc_module::symbol::Symbol;
use roc_region::all::{Loc, Region};
@ -120,11 +120,18 @@ impl Constraints {
pub const PCATEGORY_CHARACTER: Index<PatternCategory> = Index::new(10);
#[inline(always)]
pub fn push_type(&mut self, typ: Type) -> Index<Type> {
pub fn push_type(&mut self, typ: Type) -> EitherIndex<Type, Variable> {
match typ {
Type::EmptyRec => Self::EMPTY_RECORD,
Type::EmptyTagUnion => Self::EMPTY_TAG_UNION,
other => Index::push_new(&mut self.types, other),
Type::EmptyRec => EitherIndex::from_left(Self::EMPTY_RECORD),
Type::EmptyTagUnion => EitherIndex::from_left(Self::EMPTY_TAG_UNION),
Type::Variable(var) => {
let index: Index<Variable> = Index::push_new(&mut self.variables, var);
EitherIndex::from_right(index)
}
other => {
let index: Index<Type> = Index::push_new(&mut self.types, other);
EitherIndex::from_left(index)
}
}
}
@ -180,7 +187,7 @@ impl Constraints {
category: Category,
region: Region,
) -> Constraint {
let type_index = Index::push_new(&mut self.types, typ);
let type_index = self.push_type(typ);
let expected_index = Index::push_new(&mut self.expectations, expected);
let category_index = Self::push_category(self, category);
@ -194,7 +201,7 @@ impl Constraints {
category: PatternCategory,
region: Region,
) -> Constraint {
let type_index = Index::push_new(&mut self.types, typ);
let type_index = self.push_type(typ);
let expected_index = Index::push_new(&mut self.pattern_expectations, expected);
let category_index = Self::push_pattern_category(self, category);
@ -208,7 +215,7 @@ impl Constraints {
category: PatternCategory,
region: Region,
) -> Constraint {
let type_index = Index::push_new(&mut self.types, typ);
let type_index = self.push_type(typ);
let expected_index = Index::push_new(&mut self.pattern_expectations, expected);
let category_index = Index::push_new(&mut self.pattern_categories, category);
@ -216,7 +223,7 @@ impl Constraints {
}
pub fn is_open_type(&mut self, typ: Type) -> Constraint {
let type_index = Index::push_new(&mut self.types, typ);
let type_index = self.push_type(typ);
Constraint::IsOpenType(type_index)
}
@ -446,7 +453,7 @@ impl Constraints {
filename: &'static str,
line_number: u32,
) -> Constraint {
let type_index = Index::push_new(&mut self.types, typ);
let type_index = self.push_type(typ);
let string_index = Index::push_new(&mut self.strings, filename);
Constraint::Store(type_index, variable, string_index, line_number)
@ -457,11 +464,21 @@ static_assertions::assert_eq_size!([u8; 3 * 8], Constraint);
#[derive(Debug, Clone, PartialEq)]
pub enum Constraint {
Eq(Index<Type>, Index<Expected<Type>>, Index<Category>, Region),
Store(Index<Type>, Variable, Index<&'static str>, u32),
Eq(
EitherIndex<Type, Variable>,
Index<Expected<Type>>,
Index<Category>,
Region,
),
Store(
EitherIndex<Type, Variable>,
Variable,
Index<&'static str>,
u32,
),
Lookup(Symbol, Index<Expected<Type>>, Region),
Pattern(
Index<Type>,
EitherIndex<Type, Variable>,
Index<PExpected<Type>>,
Index<PatternCategory>,
Region,
@ -471,10 +488,10 @@ pub enum Constraint {
Let(Index<LetConstraint>),
And(Slice<Constraint>),
/// Presence constraints
IsOpenType(Index<Type>), // Theory; always applied to a variable? if yes the use that
IsOpenType(EitherIndex<Type, Variable>), // Theory; always applied to a variable? if yes the use that
IncludesTag(Index<IncludesTag>),
PatternPresence(
Index<Type>,
EitherIndex<Type, Variable>,
Index<PExpected<Type>>,
Index<PatternCategory>,
Region,