mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 08:11:12 +00:00
use vector instead of map
This commit is contained in:
parent
5bb66a6b8e
commit
138af15da5
1 changed files with 50 additions and 60 deletions
|
@ -29,7 +29,8 @@ pub fn infer_borrow<'a>(
|
||||||
|
|
||||||
let mut param_map = ParamMap {
|
let mut param_map = ParamMap {
|
||||||
declaration_to_index,
|
declaration_to_index,
|
||||||
items: MutMap::default(),
|
join_points: MutMap::default(),
|
||||||
|
declarations: bumpalo::vec![in arena; &[] as &[_]; procs.len()],
|
||||||
};
|
};
|
||||||
|
|
||||||
for (key, proc) in procs {
|
for (key, proc) in procs {
|
||||||
|
@ -82,14 +83,20 @@ pub enum Key {
|
||||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
|
||||||
pub struct DeclarationId(usize);
|
pub struct DeclarationId(usize);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
impl From<DeclarationId> for usize {
|
||||||
pub struct ParamMap<'a> {
|
fn from(id: DeclarationId) -> Self {
|
||||||
declaration_to_index: MutMap<(Symbol, ProcLayout<'a>), DeclarationId>,
|
id.0 as usize
|
||||||
items: MutMap<Key, &'a [Param<'a>]>,
|
}
|
||||||
// declarations: Vec<'a, &'a [Param<'a>]>,
|
|
||||||
// join_points: Vec<'a, &'a [Param<'a>]>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct ParamMap<'a> {
|
||||||
|
declaration_to_index: MutMap<(Symbol, ProcLayout<'a>), DeclarationId>,
|
||||||
|
declarations: Vec<'a, &'a [Param<'a>]>,
|
||||||
|
join_points: MutMap<JoinPointId, &'a [Param<'a>]>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
impl<'a> IntoIterator for ParamMap<'a> {
|
impl<'a> IntoIterator for ParamMap<'a> {
|
||||||
type Item = (Key, &'a [Param<'a>]);
|
type Item = (Key, &'a [Param<'a>]);
|
||||||
type IntoIter = <std::collections::HashMap<Key, &'a [Param<'a>]> as IntoIterator>::IntoIter;
|
type IntoIter = <std::collections::HashMap<Key, &'a [Param<'a>]> as IntoIterator>::IntoIter;
|
||||||
|
@ -107,18 +114,16 @@ impl<'a> IntoIterator for &'a ParamMap<'a> {
|
||||||
self.items.iter()
|
self.items.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
impl<'a> ParamMap<'a> {
|
impl<'a> ParamMap<'a> {
|
||||||
pub fn get_symbol(&self, symbol: Symbol, layout: ProcLayout<'a>) -> Option<&'a [Param<'a>]> {
|
pub fn get_symbol(&self, symbol: Symbol, layout: ProcLayout<'a>) -> Option<&'a [Param<'a>]> {
|
||||||
let index = self.declaration_to_index[&(symbol, layout)];
|
let index: usize = self.declaration_to_index[&(symbol, layout)].into();
|
||||||
let key = Key::Declaration(index);
|
|
||||||
|
|
||||||
self.items.get(&key).copied()
|
self.declarations.get(index).copied()
|
||||||
}
|
}
|
||||||
pub fn get_join_point(&self, id: JoinPointId) -> &'a [Param<'a>] {
|
pub fn get_join_point(&self, id: JoinPointId) -> &'a [Param<'a>] {
|
||||||
let key = Key::JoinPoint(id);
|
match self.join_points.get(&id) {
|
||||||
|
|
||||||
match self.items.get(&key) {
|
|
||||||
Some(slice) => slice,
|
Some(slice) => slice,
|
||||||
None => unreachable!("join point not in param map: {:?}", id),
|
None => unreachable!("join point not in param map: {:?}", id),
|
||||||
}
|
}
|
||||||
|
@ -175,13 +180,8 @@ impl<'a> ParamMap<'a> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let index = self.declaration_to_index[&key];
|
let index: usize = self.declaration_to_index[&key].into();
|
||||||
|
self.declarations[index] = Self::init_borrow_args(arena, proc.args);
|
||||||
let already_in_there = self.items.insert(
|
|
||||||
Key::Declaration(index),
|
|
||||||
Self::init_borrow_args(arena, proc.args),
|
|
||||||
);
|
|
||||||
debug_assert!(already_in_there.is_none());
|
|
||||||
|
|
||||||
self.visit_stmt(arena, proc.name, &proc.body);
|
self.visit_stmt(arena, proc.name, &proc.body);
|
||||||
}
|
}
|
||||||
|
@ -192,13 +192,8 @@ impl<'a> ParamMap<'a> {
|
||||||
proc: &Proc<'a>,
|
proc: &Proc<'a>,
|
||||||
key: (Symbol, ProcLayout<'a>),
|
key: (Symbol, ProcLayout<'a>),
|
||||||
) {
|
) {
|
||||||
let index = self.declaration_to_index[&key];
|
let index: usize = self.declaration_to_index[&key].into();
|
||||||
|
self.declarations[index] = Self::init_borrow_args_always_owned(arena, proc.args);
|
||||||
let already_in_there = self.items.insert(
|
|
||||||
Key::Declaration(index),
|
|
||||||
Self::init_borrow_args_always_owned(arena, proc.args),
|
|
||||||
);
|
|
||||||
debug_assert!(already_in_there.is_none());
|
|
||||||
|
|
||||||
self.visit_stmt(arena, proc.name, &proc.body);
|
self.visit_stmt(arena, proc.name, &proc.body);
|
||||||
}
|
}
|
||||||
|
@ -216,14 +211,8 @@ impl<'a> ParamMap<'a> {
|
||||||
remainder: v,
|
remainder: v,
|
||||||
body: b,
|
body: b,
|
||||||
} => {
|
} => {
|
||||||
let already_in_there = self
|
self.join_points
|
||||||
.items
|
.insert(*j, Self::init_borrow_params(arena, xs));
|
||||||
.insert(Key::JoinPoint(*j), Self::init_borrow_params(arena, xs));
|
|
||||||
debug_assert!(
|
|
||||||
already_in_there.is_none(),
|
|
||||||
"join point {:?} is already defined!",
|
|
||||||
j
|
|
||||||
);
|
|
||||||
|
|
||||||
stack.push(v);
|
stack.push(v);
|
||||||
stack.push(b);
|
stack.push(b);
|
||||||
|
@ -286,20 +275,11 @@ impl<'a> BorrowInfState<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_param_map_declaration(&mut self, symbol: Symbol, layout: ProcLayout<'a>) {
|
fn update_param_map_help(&mut self, ps: &[Param<'a>]) -> &'a [Param<'a>] {
|
||||||
let index = self.param_map.declaration_to_index[&(symbol, layout)];
|
let mut new_ps = Vec::with_capacity_in(ps.len(), self.arena);
|
||||||
let key = Key::Declaration(index);
|
new_ps.extend(ps.iter().map(|p| {
|
||||||
|
|
||||||
self.update_param_map(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update_param_map(&mut self, k: Key) {
|
|
||||||
let arena = self.arena;
|
|
||||||
if let Some(ps) = self.param_map.items.get(&k) {
|
|
||||||
let ps = Vec::from_iter_in(
|
|
||||||
ps.iter().map(|p| {
|
|
||||||
if !p.borrow {
|
if !p.borrow {
|
||||||
p.clone()
|
*p
|
||||||
} else if self.is_owned(p.symbol) {
|
} else if self.is_owned(p.symbol) {
|
||||||
self.modified = true;
|
self.modified = true;
|
||||||
let mut p = p.clone();
|
let mut p = p.clone();
|
||||||
|
@ -307,14 +287,24 @@ impl<'a> BorrowInfState<'a> {
|
||||||
|
|
||||||
p
|
p
|
||||||
} else {
|
} else {
|
||||||
p.clone()
|
*p
|
||||||
}
|
}
|
||||||
}),
|
}));
|
||||||
arena,
|
|
||||||
);
|
|
||||||
|
|
||||||
self.param_map.items.insert(k, ps.into_bump_slice());
|
new_ps.into_bump_slice()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_param_map_declaration(&mut self, symbol: Symbol, layout: ProcLayout<'a>) {
|
||||||
|
let index: usize = self.param_map.declaration_to_index[&(symbol, layout)].into();
|
||||||
|
|
||||||
|
let ps = self.param_map.declarations[index];
|
||||||
|
self.param_map.declarations[index] = self.update_param_map_help(ps);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_param_map_join_point(&mut self, id: JoinPointId) {
|
||||||
|
let ps = self.param_map.join_points[&id];
|
||||||
|
let new_ps = self.update_param_map_help(ps);
|
||||||
|
self.param_map.join_points.insert(id, new_ps);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This looks at an application `f x1 x2 x3`
|
/// This looks at an application `f x1 x2 x3`
|
||||||
|
@ -697,7 +687,7 @@ impl<'a> BorrowInfState<'a> {
|
||||||
self.update_param_set(ys);
|
self.update_param_set(ys);
|
||||||
self.collect_stmt(v);
|
self.collect_stmt(v);
|
||||||
self.param_set = old;
|
self.param_set = old;
|
||||||
self.update_param_map(Key::JoinPoint(*j));
|
self.update_param_map_join_point(*j);
|
||||||
|
|
||||||
self.collect_stmt(b);
|
self.collect_stmt(b);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue