Make collections::soa use the soa crate

This commit is contained in:
Richard Feldman 2024-10-13 11:08:28 -04:00
parent 44d00e1f13
commit e589923ae8
No known key found for this signature in database
GPG key ID: DAC334802F365236
6 changed files with 59 additions and 244 deletions

View file

@ -5,7 +5,7 @@ use std::sync::Arc;
use crate::abilities::SpecializationId;
use crate::exhaustive::{ExhaustiveContext, SketchedRows};
use crate::expected::{Expected, PExpected};
use roc_collections::soa::{EitherIndex, Index, Slice};
use roc_collections::soa::{index_push_new, slice_extend_new, EitherIndex, Index, Slice};
use roc_module::ident::TagName;
use roc_module::symbol::{ModuleId, Symbol};
use roc_region::all::{Loc, Region};
@ -205,11 +205,11 @@ impl Constraints {
}
pub fn push_expected_type(&mut self, expected: Expected<TypeOrVar>) -> ExpectedTypeIndex {
Index::push_new(&mut self.expectations, expected)
index_push_new(&mut self.expectations, expected)
}
pub fn push_pat_expected_type(&mut self, expected: PExpected<TypeOrVar>) -> PExpectedTypeIndex {
Index::push_new(&mut self.pattern_expectations, expected)
index_push_new(&mut self.pattern_expectations, expected)
}
#[inline(always)]
@ -229,7 +229,7 @@ impl Constraints {
Category::List => Self::CATEGORY_LIST,
Category::Str => Self::CATEGORY_STR,
Category::Character => Self::CATEGORY_CHARACTER,
other => Index::push_new(&mut self.categories, other),
other => index_push_new(&mut self.categories, other),
}
}
@ -247,7 +247,7 @@ impl Constraints {
PatternCategory::Int => Self::PCATEGORY_INT,
PatternCategory::Float => Self::PCATEGORY_FLOAT,
PatternCategory::Character => Self::PCATEGORY_CHARACTER,
other => Index::push_new(&mut self.pattern_categories, other),
other => index_push_new(&mut self.pattern_categories, other),
}
}
@ -320,7 +320,7 @@ impl Constraints {
category: PatternCategory,
region: Region,
) -> Constraint {
let category_index = Index::push_new(&mut self.pattern_categories, category);
let category_index = index_push_new(&mut self.pattern_categories, category);
Constraint::PatternPresence(type_index, expected_index, category_index, region)
}
@ -337,7 +337,7 @@ impl Constraints {
category: PatternCategory,
region: Region,
) -> Constraint {
let category_index = Index::push_new(&mut self.pattern_categories, category);
let category_index = index_push_new(&mut self.pattern_categories, category);
let includes_tag = IncludesTag {
type_index,
@ -347,7 +347,7 @@ impl Constraints {
region,
};
let includes_tag_index = Index::push_new(&mut self.includes_tags, includes_tag);
let includes_tag_index = index_push_new(&mut self.includes_tags, includes_tag);
Constraint::IncludesTag(includes_tag_index)
}
@ -614,7 +614,7 @@ impl Constraints {
filename: &'static str,
line_number: u32,
) -> Constraint {
let string_index = Index::push_new(&mut self.strings, filename);
let string_index = index_push_new(&mut self.strings, filename);
Constraint::Store(type_index, variable, string_index, line_number)
}
@ -632,19 +632,19 @@ impl Constraints {
exhaustive: ExhaustiveMark,
) -> Constraint {
let real_var = Self::push_type_variable(real_var);
let sketched_rows = Index::push_new(&mut self.sketched_rows, sketched_rows);
let sketched_rows = index_push_new(&mut self.sketched_rows, sketched_rows);
let equality = match category_and_expectation {
Ok((category, expected)) => {
let category = Index::push_new(&mut self.categories, category);
let category = index_push_new(&mut self.categories, category);
let equality = Eq(real_var, expected, category, real_region);
let equality = Index::push_new(&mut self.eq, equality);
let equality = index_push_new(&mut self.eq, equality);
Ok(equality)
}
Err((category, expected)) => {
let category = Index::push_new(&mut self.pattern_categories, category);
let category = index_push_new(&mut self.pattern_categories, category);
let equality = PatternEq(real_var, expected, category, real_region);
let equality = Index::push_new(&mut self.pattern_eq, equality);
let equality = index_push_new(&mut self.pattern_eq, equality);
Err(equality)
}
};
@ -662,18 +662,18 @@ impl Constraints {
I: IntoIterator<Item = (Symbol, Region)>,
I1: IntoIterator<Item = Region>,
{
let def_names = Slice::extend_new(&mut self.loc_symbols, loc_symbols);
let def_names = slice_extend_new(&mut self.loc_symbols, loc_symbols);
// we add a dummy symbol to these regions, so we can store the data in the loc_symbols vec
let it = expr_regions.into_iter().map(|r| (Symbol::ATTR_ATTR, r));
let expr_regions = Slice::extend_new(&mut self.loc_symbols, it);
let expr_regions = slice_extend_new(&mut self.loc_symbols, it);
let expr_regions = Slice::new(expr_regions.start() as _, expr_regions.len() as _);
let cycle = Cycle {
def_names,
expr_regions,
};
let cycle_index = Index::push_new(&mut self.cycles, cycle);
let cycle_index = index_push_new(&mut self.cycles, cycle);
Constraint::CheckCycle(cycle_index, cycle_mark)
}

View file

@ -12,7 +12,7 @@ use crate::pattern::{canonicalize_pattern, BindingsFromPattern, Pattern, PermitS
use crate::procedure::{QualifiedReference, References};
use crate::scope::{Scope, SymbolLookup};
use crate::traverse::{walk_expr, Visitor};
use roc_collections::soa::Index;
use roc_collections::soa::{index_push_new, Index};
use roc_collections::{SendMap, VecMap, VecSet};
use roc_error_macros::internal_error;
use roc_module::called_via::CalledVia;
@ -2850,7 +2850,7 @@ impl Declarations {
let loc_function_def = Loc::at(loc_closure_data.region, function_def);
let function_def_index = Index::push_new(&mut self.function_bodies, loc_function_def);
let function_def_index = index_push_new(&mut self.function_bodies, loc_function_def);
let tag = match loc_closure_data.value.recursive {
Recursive::NotRecursive | Recursive::Recursive => {
@ -2901,7 +2901,7 @@ impl Declarations {
let loc_function_def = Loc::at(loc_closure_data.region, function_def);
let function_def_index = Index::push_new(&mut self.function_bodies, loc_function_def);
let function_def_index = index_push_new(&mut self.function_bodies, loc_function_def);
if let Some(annotation) = host_annotation {
self.host_exposed_annotations
@ -3007,7 +3007,7 @@ impl Declarations {
pattern_vars,
};
let destructure_def_index = Index::push_new(&mut self.destructs, destruct_def);
let destructure_def_index = index_push_new(&mut self.destructs, destruct_def);
self.declarations
.push(DeclarationTag::Destructure(destructure_def_index));
@ -3079,7 +3079,7 @@ impl Declarations {
let loc_function_def = Loc::at(def.loc_expr.region, function_def);
let function_def_index =
Index::push_new(&mut self.function_bodies, loc_function_def);
index_push_new(&mut self.function_bodies, loc_function_def);
self.declarations[index] = DeclarationTag::Function(function_def_index);
self.expressions[index] = *closure_data.loc_body;
@ -3131,7 +3131,7 @@ impl Declarations {
let loc_function_def = Loc::at(region, function_def);
let function_def_index =
Index::push_new(&mut self.function_bodies, loc_function_def);
index_push_new(&mut self.function_bodies, loc_function_def);
if let Some(annotation) = &mut self.annotations[index] {
annotation.convert_to_fn(new_args_len, var_store);

View file

@ -1,196 +1,11 @@
use std::usize;
/// DEPRECATED - use soa::Index directly instead!
pub type Index<T> = soa::Index<T>;
/// DEPRECATED - use crates/soa instead!
/// DEPRECATED - use soa::Slice directly instead!
pub type Slice<T> = soa::Slice<T>;
pub struct Index<T> {
index: u32,
_marker: std::marker::PhantomData<T>,
}
impl<T> Eq for Index<T> {}
impl<T> PartialEq for Index<T> {
fn eq(&self, other: &Self) -> bool {
self.index == other.index
}
}
impl<T> std::hash::Hash for Index<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.index.hash(state);
}
}
impl<T> Clone for Index<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for Index<T> {}
impl<T> std::fmt::Debug for Index<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Index({})", self.index)
}
}
impl<T> Index<T> {
pub const fn new(index: u32) -> Self {
Self {
index,
_marker: std::marker::PhantomData,
}
}
pub const fn index(self) -> usize {
self.index as usize
}
pub fn push_new(vector: &mut Vec<T>, value: T) -> Index<T> {
let index = Self::new(vector.len() as _);
vector.push(value);
index
}
pub const fn as_slice(self) -> Slice<T> {
Slice::new(self.index, 1)
}
}
#[derive(PartialEq, Eq)]
pub struct Slice<T> {
length: u16,
start: u32,
_marker: std::marker::PhantomData<T>,
}
impl<T> Clone for Slice<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for Slice<T> {}
impl<T> std::fmt::Debug for Slice<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Slice(start = {}, length = {})", self.start, self.length)
}
}
impl<T> Default for Slice<T> {
fn default() -> Self {
Self::empty()
}
}
impl<T> Slice<T> {
pub const fn empty() -> Self {
Self::new(0, 0)
}
pub const fn new(start: u32, length: u16) -> Self {
Self {
start,
length,
_marker: std::marker::PhantomData,
}
}
pub fn extend_new<I>(vector: &mut Vec<T>, values: I) -> Slice<T>
where
I: IntoIterator<Item = T>,
{
let start = vector.len() as u32;
vector.extend(values);
let end = vector.len() as u32;
Self::new(start, (end - start) as u16)
}
pub const fn len(&self) -> usize {
self.length as _
}
pub const fn start(&self) -> usize {
self.start as _
}
pub const fn is_empty(&self) -> bool {
self.length == 0
}
pub const fn indices(&self) -> std::ops::Range<usize> {
self.start as usize..(self.start as usize + self.length as usize)
}
pub fn into_iter(&self) -> impl Iterator<Item = Index<T>> {
self.indices().map(|i| Index::new(i as _))
}
pub const fn at(&self, i: usize) -> Index<T> {
Index::new(self.start + i as u32)
}
}
#[derive(PartialEq, Eq)]
pub struct EitherIndex<T, U> {
index: u32,
_marker: std::marker::PhantomData<(T, U)>,
}
impl<T, U> Clone for EitherIndex<T, U> {
fn clone(&self) -> Self {
*self
}
}
impl<T, U> Copy for EitherIndex<T, U> {}
impl<T, U> std::fmt::Debug for EitherIndex<T, U> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Index({})", self.index)
}
}
impl<T, U> EitherIndex<T, U> {
const MASK: u32 = 1 << 31;
pub const fn from_left(input: Index<T>) -> Self {
assert!(input.index & Self::MASK == 0);
Self {
index: input.index,
_marker: std::marker::PhantomData,
}
}
pub const fn from_right(input: Index<U>) -> Self {
assert!(input.index & Self::MASK == 0);
Self {
index: input.index | Self::MASK,
_marker: std::marker::PhantomData,
}
}
pub const fn split(self) -> Result<Index<T>, Index<U>> {
if self.index & Self::MASK == 0 {
Ok(Index::new(self.index))
} else {
Err(Index::new(self.index ^ Self::MASK))
}
}
pub fn decrement_index(&mut self) {
self.index = self.index.saturating_sub(1);
}
}
/// DEPRECATED - use soa::EitherIndex directly instead!
pub type EitherIndex<T, U> = soa::EitherIndex<T, U>;
/// Push to a std::vec::Vec<T> and then return an index to that new element's
/// position in the Vec.

View file

@ -8,7 +8,7 @@ use crate::ident::Accessor;
use crate::parser::ESingleQuote;
use bumpalo::collections::{String, Vec};
use bumpalo::Bump;
use roc_collections::soa::{EitherIndex, Index, Slice};
use roc_collections::soa::{index_push_new, slice_extend_new, EitherIndex, Slice};
use roc_error_macros::internal_error;
use roc_module::called_via::{BinOp, CalledVia, UnaryOp};
use roc_module::ident::QualifiedModuleName;
@ -1311,10 +1311,10 @@ impl<'a> Defs<'a> {
self.regions.push(region);
let before = Slice::extend_new(&mut self.spaces, spaces_before.iter().copied());
let before = slice_extend_new(&mut self.spaces, spaces_before.iter().copied());
self.space_before.push(before);
let after = Slice::extend_new(&mut self.spaces, spaces_after.iter().copied());
let after = slice_extend_new(&mut self.spaces, spaces_after.iter().copied());
self.space_after.push(after);
}
@ -1325,7 +1325,7 @@ impl<'a> Defs<'a> {
spaces_before: &[CommentOrNewline<'a>],
spaces_after: &[CommentOrNewline<'a>],
) {
let value_def_index = Index::push_new(&mut self.value_defs, value_def);
let value_def_index = index_push_new(&mut self.value_defs, value_def);
let tag = EitherIndex::from_right(value_def_index);
self.push_def_help(tag, region, spaces_before, spaces_after)
}
@ -1359,7 +1359,7 @@ impl<'a> Defs<'a> {
spaces_before: &[CommentOrNewline<'a>],
spaces_after: &[CommentOrNewline<'a>],
) {
let type_def_index = Index::push_new(&mut self.type_defs, type_def);
let type_def_index = index_push_new(&mut self.type_defs, type_def);
let tag = EitherIndex::from_left(type_def_index);
self.push_def_help(tag, region, spaces_before, spaces_after)
}
@ -1374,13 +1374,13 @@ impl<'a> Defs<'a> {
for (tag_index, tag) in self.tags.iter().enumerate() {
let region = self.regions[tag_index];
let space_before = {
let start = self.space_before[tag_index].start();
let start = self.space_before[tag_index].start() as usize;
let len = self.space_before[tag_index].len();
&self.spaces[start..(start + len)]
};
let space_after = {
let start = self.space_after[tag_index].start();
let start = self.space_after[tag_index].start() as usize;
let len = self.space_after[tag_index].len();
&self.spaces[start..(start + len)]
@ -1393,13 +1393,13 @@ impl<'a> Defs<'a> {
match tag_index.cmp(&target) {
std::cmp::Ordering::Less => {
// before
let type_def_index = Index::push_new(&mut before.type_defs, type_def);
let type_def_index = index_push_new(&mut before.type_defs, type_def);
let tag = EitherIndex::from_left(type_def_index);
before.push_def_help(tag, region, space_before, space_after);
}
std::cmp::Ordering::Greater => {
// after
let type_def_index = Index::push_new(&mut after.type_defs, type_def);
let type_def_index = index_push_new(&mut after.type_defs, type_def);
let tag = EitherIndex::from_left(type_def_index);
after.push_def_help(tag, region, space_before, space_after);
}
@ -1415,14 +1415,14 @@ impl<'a> Defs<'a> {
std::cmp::Ordering::Less => {
// before
let new_value_def_index =
Index::push_new(&mut before.value_defs, value_def);
index_push_new(&mut before.value_defs, value_def);
let tag = EitherIndex::from_right(new_value_def_index);
before.push_def_help(tag, region, space_before, space_after);
}
std::cmp::Ordering::Greater => {
// after
let new_value_def_index =
Index::push_new(&mut after.value_defs, value_def);
index_push_new(&mut after.value_defs, value_def);
let tag = EitherIndex::from_right(new_value_def_index);
after.push_def_help(tag, region, space_before, space_after);
}

View file

@ -28,7 +28,7 @@ use crate::type_annotation;
use crate::{header, keyword};
use bumpalo::collections::Vec;
use bumpalo::Bump;
use roc_collections::soa::Slice;
use roc_collections::soa::slice_extend_new;
use roc_error_macros::internal_error;
use roc_module::called_via::{BinOp, CalledVia, UnaryOp};
use roc_region::all::{Loc, Position, Region};
@ -2286,7 +2286,7 @@ pub fn parse_top_level_defs<'a>(
}
if output.tags.len() > existing_len {
let after = Slice::extend_new(&mut output.spaces, last_space.iter().copied());
let after = slice_extend_new(&mut output.spaces, last_space.iter().copied());
let last = output.tags.len() - 1;
debug_assert!(output.space_after[last].is_empty() || after.is_empty());
output.space_after[last] = after;

View file

@ -5,7 +5,7 @@ use crate::subs::{
VariableSubsSlice,
};
use roc_collections::all::{HumanIndex, ImMap, ImSet, MutMap, MutSet, SendMap};
use roc_collections::soa::{Index, Slice};
use roc_collections::soa::{index_push_new, slice_extend_new, Index, Slice};
use roc_collections::VecMap;
use roc_error_macros::internal_error;
use roc_module::called_via::CalledVia;
@ -602,7 +602,7 @@ impl Types {
self.tags_slices
.extend(repeat(Slice::default()).take(length));
Slice::extend_new(&mut self.tags, repeat(TypeTag::EmptyRecord).take(length))
slice_extend_new(&mut self.tags, repeat(TypeTag::EmptyRecord).take(length))
}
fn reserve_type_tag(&mut self) -> Index<TypeTag> {
@ -610,7 +610,7 @@ impl Types {
self.tags_slices.push(Slice::default());
Index::push_new(&mut self.tags, TypeTag::EmptyRecord)
index_push_new(&mut self.tags, TypeTag::EmptyRecord)
}
fn set_type_tag(&mut self, index: Index<TypeTag>, tag: TypeTag, type_slice: Slice<TypeTag>) {
@ -644,10 +644,10 @@ impl Types {
extension: &TypeExtension,
) -> (UnionTags, Slice<TypeTag>) {
let tag_names_slice =
Slice::extend_new(&mut self.tag_names, tags.iter().map(|(n, _)| n.clone()));
slice_extend_new(&mut self.tag_names, tags.iter().map(|(n, _)| n.clone()));
// Store the payload slices in the aside buffer
let type_slices = Slice::extend_new(
let type_slices = slice_extend_new(
&mut self.aside_types_slices,
std::iter::repeat(Slice::default()).take(tags.len()),
);
@ -698,7 +698,7 @@ impl Types {
Slice::new(slice.start() as _, slice.len() as _)
};
let type_argument_abilities = Slice::extend_new(
let type_argument_abilities = slice_extend_new(
&mut self.type_arg_abilities,
type_arguments
.iter()
@ -706,7 +706,7 @@ impl Types {
);
// TODO: populate correctly
let type_argument_regions = Slice::extend_new(
let type_argument_regions = slice_extend_new(
&mut self.regions,
std::iter::repeat(Region::zero()).take(type_arguments.len()),
);
@ -759,7 +759,7 @@ impl Types {
}
Type::Apply(symbol, arguments, region) => {
let type_argument_regions =
Slice::extend_new(&mut self.regions, arguments.iter().map(|t| t.region));
slice_extend_new(&mut self.regions, arguments.iter().map(|t| t.region));
let type_slice = {
let slice = self.reserve_type_tags(arguments.len());
@ -835,12 +835,12 @@ impl Types {
slice
};
let field_types = Slice::extend_new(
let field_types = slice_extend_new(
&mut self.field_types,
fields.values().map(|f| f.map(|_| ())),
);
let field_names = Slice::extend_new(&mut self.field_names, fields.keys().cloned());
let field_names = slice_extend_new(&mut self.field_names, fields.keys().cloned());
let record_fields = RecordFields {
length: fields.len() as u16,
@ -870,7 +870,7 @@ impl Types {
};
let elem_index_slice =
Slice::extend_new(&mut self.tuple_elem_indices, elems.iter().map(|(i, _)| *i));
slice_extend_new(&mut self.tuple_elem_indices, elems.iter().map(|(i, _)| *i));
let tuple_elems = TupleElems {
length: elems.len() as u16,
@ -902,7 +902,7 @@ impl Types {
infer_ext_in_output_types,
}) => {
let type_argument_regions =
Slice::extend_new(&mut self.regions, type_arguments.iter().map(|t| t.region));
slice_extend_new(&mut self.regions, type_arguments.iter().map(|t| t.region));
let type_arguments_slice = {
let slice = self.reserve_type_tags(type_arguments.len());
@ -936,7 +936,7 @@ impl Types {
Slice::new(slice.start() as _, slice.len() as _)
};
let type_argument_abilities = Slice::extend_new(
let type_argument_abilities = slice_extend_new(
&mut self.type_arg_abilities,
type_arguments
.iter()
@ -951,7 +951,7 @@ impl Types {
infer_ext_in_output_variables: infer_ext_in_output_slice,
};
let shared = Index::push_new(&mut self.aliases, alias_shared);
let shared = index_push_new(&mut self.aliases, alias_shared);
let tag = TypeTag::DelayedAlias { shared };
@ -982,7 +982,7 @@ impl Types {
infer_ext_in_output_types,
);
let shared = Index::push_new(&mut self.aliases, alias_shared);
let shared = index_push_new(&mut self.aliases, alias_shared);
let actual = self.from_old_type(actual);
let tag = match kind {
@ -1056,7 +1056,7 @@ impl Types {
lambda_set_variables: new_lambda_set_variables,
infer_ext_in_output_variables: new_infer_ext_in_output_variables,
};
Index::push_new(&mut self.aliases, new_shared)
index_push_new(&mut self.aliases, new_shared)
}};
}
@ -1064,7 +1064,7 @@ impl Types {
($union_tags:expr) => {{
let (tags, payload_slices) = self.union_tag_slices($union_tags);
let new_payload_slices = Slice::extend_new(
let new_payload_slices = slice_extend_new(
&mut self.aside_types_slices,
std::iter::repeat(Slice::default()).take(payload_slices.len()),
);