shrink FlatType further

This commit is contained in:
Folkert 2021-08-12 23:13:32 +02:00
parent 42f04fcc77
commit c8007201b0
2 changed files with 57 additions and 32 deletions

View file

@ -172,7 +172,7 @@ fn find_names_needed(
find_names_needed(*ext_var, subs, roots, root_appearances, names_taken); find_names_needed(*ext_var, subs, roots, root_appearances, names_taken);
} }
Structure(TagUnion(tags, ext_var)) => { Structure(TagUnion(tags, ext_var)) => {
for slice_index in tags.variables { for slice_index in tags.variables() {
let slice = subs[slice_index]; let slice = subs[slice_index];
for var_index in slice { for var_index in slice {
let var = subs[var_index]; let var = subs[var_index];
@ -186,7 +186,7 @@ fn find_names_needed(
find_names_needed(*ext_var, subs, roots, root_appearances, names_taken); find_names_needed(*ext_var, subs, roots, root_appearances, names_taken);
} }
Structure(RecursiveTagUnion(rec_var, tags, ext_var)) => { Structure(RecursiveTagUnion(rec_var, tags, ext_var)) => {
for slice_index in tags.variables { for slice_index in tags.variables() {
let slice = subs[slice_index]; let slice = subs[slice_index];
for var_index in slice { for var_index in slice {
let var = subs[var_index]; let var = subs[var_index];

View file

@ -11,7 +11,7 @@ use ven_ena::unify::{InPlace, Snapshot, UnificationTable, UnifyKey};
// if it went up, maybe check that the change is really required // if it went up, maybe check that the change is really required
static_assertions::assert_eq_size!([u8; 56], Descriptor); static_assertions::assert_eq_size!([u8; 56], Descriptor);
static_assertions::assert_eq_size!([u8; 40], Content); static_assertions::assert_eq_size!([u8; 40], Content);
static_assertions::assert_eq_size!([u8; 32], FlatType); static_assertions::assert_eq_size!([u8; 24], FlatType);
static_assertions::assert_eq_size!([u8; 48], Problem); static_assertions::assert_eq_size!([u8; 48], Problem);
#[derive(Clone, Copy, Hash, PartialEq, Eq)] #[derive(Clone, Copy, Hash, PartialEq, Eq)]
@ -230,7 +230,7 @@ impl<T> SubsSlice<T> {
&mut slice[self.start as usize..][..self.length as usize] &mut slice[self.start as usize..][..self.length as usize]
} }
pub fn len(&self) -> usize { pub const fn len(&self) -> usize {
self.length as usize self.length as usize
} }
@ -238,7 +238,7 @@ impl<T> SubsSlice<T> {
self.len() == 0 self.len() == 0
} }
pub fn new(start: u32, length: u16) -> Self { pub const fn new(start: u32, length: u16) -> Self {
Self { Self {
start, start,
length, length,
@ -948,19 +948,39 @@ impl IntoIterator for VariableSubsSlice {
#[derive(Clone, Copy, Debug, Default)] #[derive(Clone, Copy, Debug, Default)]
pub struct UnionTags { pub struct UnionTags {
pub tag_names: SubsSlice<TagName>, length: u16,
pub variables: SubsSlice<VariableSubsSlice>, tag_names_start: u32,
variables_start: u32,
} }
impl UnionTags { impl UnionTags {
pub fn from_tag_name_index(index: SubsIndex<TagName>) -> Self { pub fn from_tag_name_index(index: SubsIndex<TagName>) -> Self {
Self::from_slices(
SubsSlice::new(index.start, 1),
SubsSlice::new(0, 1), // the first variablesubsslice is the empty slice
)
}
fn from_slices(tag_names: SubsSlice<TagName>, variables: SubsSlice<VariableSubsSlice>) -> Self {
debug_assert_eq!(tag_names.len(), variables.len());
Self { Self {
tag_names: SubsSlice::new(index.start, 1), length: tag_names.len() as u16,
variables: SubsSlice::new(0, 1), // the first variablesubsslice is the empty slice tag_names_start: tag_names.start,
variables_start: variables.start,
} }
} }
pub fn len(&self) -> usize {
self.tag_names.len() const fn tag_names(&self) -> SubsSlice<TagName> {
SubsSlice::new(self.tag_names_start, self.length)
}
pub const fn variables(&self) -> SubsSlice<VariableSubsSlice> {
SubsSlice::new(self.variables_start, self.length)
}
pub const fn len(&self) -> usize {
self.length as usize
} }
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
@ -994,10 +1014,10 @@ impl UnionTags {
length += 1; length += 1;
} }
UnionTags { Self::from_slices(
variables: SubsSlice::new(variables_start, length), SubsSlice::new(tag_names_start, length),
tag_names: SubsSlice::new(tag_names_start, length), SubsSlice::new(variables_start, length),
} )
} }
pub fn insert_slices_into_subs<I>(subs: &mut Subs, input: I) -> Self pub fn insert_slices_into_subs<I>(subs: &mut Subs, input: I) -> Self
@ -1021,16 +1041,19 @@ impl UnionTags {
length += 1; length += 1;
} }
UnionTags { Self {
variables: SubsSlice::new(variables_start, length), length,
tag_names: SubsSlice::new(tag_names_start, length), tag_names_start,
variables_start,
} }
} }
pub fn iter_all( pub fn iter_all(
&self, &self,
) -> impl Iterator<Item = (SubsIndex<TagName>, SubsIndex<VariableSubsSlice>)> { ) -> impl Iterator<Item = (SubsIndex<TagName>, SubsIndex<VariableSubsSlice>)> {
self.tag_names.into_iter().zip(self.variables.into_iter()) self.tag_names()
.into_iter()
.zip(self.variables().into_iter())
} }
#[inline(always)] #[inline(always)]
@ -1162,7 +1185,7 @@ fn first<K: Ord, V>(x: &(K, V), y: &(K, V)) -> std::cmp::Ordering {
pub type SortedIterator<'a> = Box<dyn Iterator<Item = (Lowercase, RecordField<Variable>)> + 'a>; pub type SortedIterator<'a> = Box<dyn Iterator<Item = (Lowercase, RecordField<Variable>)> + 'a>;
impl RecordFields { impl RecordFields {
pub fn len(&self) -> usize { pub const fn len(&self) -> usize {
self.length as usize self.length as usize
} }
@ -1371,7 +1394,7 @@ fn occurs(
short_circuit(subs, root_var, &new_seen, it) short_circuit(subs, root_var, &new_seen, it)
} }
TagUnion(tags, ext_var) => { TagUnion(tags, ext_var) => {
for slice_index in tags.variables { for slice_index in tags.variables() {
let slice = subs[slice_index]; let slice = subs[slice_index];
for var_index in slice { for var_index in slice {
let var = subs[var_index]; let var = subs[var_index];
@ -1387,7 +1410,7 @@ fn occurs(
} }
RecursiveTagUnion(_rec_var, tags, ext_var) => { RecursiveTagUnion(_rec_var, tags, ext_var) => {
// TODO rec_var is excluded here, verify that this is correct // TODO rec_var is excluded here, verify that this is correct
for slice_index in tags.variables { for slice_index in tags.variables() {
let slice = subs[slice_index]; let slice = subs[slice_index];
for var_index in slice { for var_index in slice {
let var = subs[var_index]; let var = subs[var_index];
@ -1492,7 +1515,7 @@ fn explicit_substitute(
let new_ext_var = explicit_substitute(subs, from, to, ext_var, seen); let new_ext_var = explicit_substitute(subs, from, to, ext_var, seen);
let mut new_slices = Vec::new(); let mut new_slices = Vec::new();
for slice_index in tags.variables { for slice_index in tags.variables() {
let slice = subs[slice_index]; let slice = subs[slice_index];
let mut new_variables = Vec::new(); let mut new_variables = Vec::new();
@ -1511,12 +1534,13 @@ fn explicit_substitute(
} }
let start = subs.variable_slices.len() as u32; let start = subs.variable_slices.len() as u32;
let length = new_slices.len() as u16; let length = new_slices.len();
subs.variable_slices.extend(new_slices); subs.variable_slices.extend(new_slices);
let mut union_tags = tags; let mut union_tags = tags;
union_tags.variables = SubsSlice::new(start, length); debug_assert_eq!(length, union_tags.len());
union_tags.variables_start = start;
subs.set_content(in_var, Structure(TagUnion(union_tags, new_ext_var))); subs.set_content(in_var, Structure(TagUnion(union_tags, new_ext_var)));
} }
@ -1532,7 +1556,7 @@ fn explicit_substitute(
let new_ext_var = explicit_substitute(subs, from, to, ext_var, seen); let new_ext_var = explicit_substitute(subs, from, to, ext_var, seen);
let mut new_slices = Vec::new(); let mut new_slices = Vec::new();
for slice_index in tags.variables { for slice_index in tags.variables() {
let slice = subs[slice_index]; let slice = subs[slice_index];
let mut new_variables = Vec::new(); let mut new_variables = Vec::new();
@ -1551,12 +1575,13 @@ fn explicit_substitute(
} }
let start = subs.variable_slices.len() as u32; let start = subs.variable_slices.len() as u32;
let length = new_slices.len() as u16; let length = new_slices.len();
subs.variable_slices.extend(new_slices); subs.variable_slices.extend(new_slices);
let mut union_tags = tags; let mut union_tags = tags;
union_tags.variables = SubsSlice::new(start, length); debug_assert_eq!(length, union_tags.len());
union_tags.variables_start = start;
subs.set_content( subs.set_content(
in_var, in_var,
@ -1679,7 +1704,7 @@ fn get_var_names(
FlatType::TagUnion(tags, ext_var) => { FlatType::TagUnion(tags, ext_var) => {
let mut taken_names = get_var_names(subs, ext_var, taken_names); let mut taken_names = get_var_names(subs, ext_var, taken_names);
for slice_index in tags.variables { for slice_index in tags.variables() {
let slice = subs[slice_index]; let slice = subs[slice_index];
for var_index in slice { for var_index in slice {
let var = subs[var_index]; let var = subs[var_index];
@ -1698,7 +1723,7 @@ fn get_var_names(
let taken_names = get_var_names(subs, ext_var, taken_names); let taken_names = get_var_names(subs, ext_var, taken_names);
let mut taken_names = get_var_names(subs, rec_var, taken_names); let mut taken_names = get_var_names(subs, rec_var, taken_names);
for slice_index in tags.variables { for slice_index in tags.variables() {
let slice = subs[slice_index]; let slice = subs[slice_index];
for var_index in slice { for var_index in slice {
let arg_var = subs[var_index]; let arg_var = subs[var_index];
@ -2084,7 +2109,7 @@ fn restore_content(subs: &mut Subs, content: &Content) {
subs.restore(*ext_var); subs.restore(*ext_var);
} }
TagUnion(tags, ext_var) => { TagUnion(tags, ext_var) => {
for slice_index in tags.variables { for slice_index in tags.variables() {
let slice = subs[slice_index]; let slice = subs[slice_index];
for var_index in slice { for var_index in slice {
let var = subs[var_index]; let var = subs[var_index];
@ -2099,7 +2124,7 @@ fn restore_content(subs: &mut Subs, content: &Content) {
} }
RecursiveTagUnion(rec_var, tags, ext_var) => { RecursiveTagUnion(rec_var, tags, ext_var) => {
for slice_index in tags.variables { for slice_index in tags.variables() {
let slice = subs[slice_index]; let slice = subs[slice_index];
for var_index in slice { for var_index in slice {
let var = subs[var_index]; let var = subs[var_index];