-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework parsed module storage and ordering algorithm.
- Loading branch information
Showing
22 changed files
with
585 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use std::{ | ||
fmt, | ||
sync::{Arc, RwLock}, | ||
}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Inner<T> { | ||
pub items: Vec<Option<Arc<RwLock<T>>>>, | ||
pub free_list: Vec<usize>, | ||
} | ||
|
||
impl<T> Default for Inner<T> { | ||
fn default() -> Self { | ||
Self { | ||
items: Default::default(), | ||
free_list: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct ConcurrentSlabMut<T> { | ||
pub inner: RwLock<Inner<T>>, | ||
} | ||
|
||
impl<T> Clone for ConcurrentSlabMut<T> | ||
where | ||
T: Clone, | ||
{ | ||
fn clone(&self) -> Self { | ||
let inner = self.inner.read().unwrap(); | ||
Self { | ||
inner: RwLock::new(inner.clone()), | ||
} | ||
} | ||
} | ||
|
||
impl<T> Default for ConcurrentSlabMut<T> { | ||
fn default() -> Self { | ||
Self { | ||
inner: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
pub struct ListDisplay<I> { | ||
pub list: I, | ||
} | ||
|
||
impl<I: IntoIterator + Clone> fmt::Display for ListDisplay<I> | ||
where | ||
I::Item: fmt::Display, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let fmt_elems = self | ||
.list | ||
.clone() | ||
.into_iter() | ||
.enumerate() | ||
.map(|(i, value)| format!("{i:<10}\t->\t{value}")) | ||
.collect::<Vec<_>>(); | ||
write!(f, "{}", fmt_elems.join("\n")) | ||
} | ||
} | ||
|
||
impl<T> ConcurrentSlabMut<T> | ||
where | ||
T: Clone, | ||
{ | ||
#[allow(dead_code)] | ||
pub fn len(&self) -> usize { | ||
let inner = self.inner.read().unwrap(); | ||
inner.items.len() | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn values(&self) -> Vec<Arc<RwLock<T>>> { | ||
let inner = self.inner.read().unwrap(); | ||
inner.items.iter().filter_map(|x| x.clone()).collect() | ||
} | ||
|
||
pub fn insert(&self, value: T) -> usize { | ||
self.insert_arc(Arc::new(RwLock::new(value))) | ||
} | ||
|
||
pub fn insert_arc(&self, value: Arc<RwLock<T>>) -> usize { | ||
let mut inner = self.inner.write().unwrap(); | ||
|
||
if let Some(free) = inner.free_list.pop() { | ||
assert!(inner.items[free].is_none()); | ||
inner.items[free] = Some(value); | ||
free | ||
} else { | ||
inner.items.push(Some(value)); | ||
inner.items.len() - 1 | ||
} | ||
} | ||
|
||
pub fn get(&self, index: usize) -> Arc<RwLock<T>> { | ||
let inner = self.inner.read().unwrap(); | ||
inner.items[index] | ||
.as_ref() | ||
.expect("invalid slab index for ConcurrentSlab::get") | ||
.clone() | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn retain(&self, predicate: impl Fn(&usize, &mut Arc<RwLock<T>>) -> bool) { | ||
let mut inner = self.inner.write().unwrap(); | ||
|
||
let Inner { items, free_list } = &mut *inner; | ||
for (idx, item) in items.iter_mut().enumerate() { | ||
if let Some(arc) = item { | ||
if !predicate(&idx, arc) { | ||
free_list.push(idx); | ||
item.take(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn clear(&self) { | ||
let mut inner = self.inner.write().unwrap(); | ||
inner.items.clear(); | ||
inner.items.shrink_to(0); | ||
|
||
inner.free_list.clear(); | ||
inner.free_list.shrink_to(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use std::sync::{Arc, RwLock}; | ||
|
||
use crate::{concurrent_slab_mut::ConcurrentSlabMut, engine_threading::DebugWithEngines}; | ||
|
||
use super::ParseModule; | ||
|
||
/// A identifier to uniquely refer to our parsed modules. | ||
#[derive(Default, PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd, Debug)] | ||
pub struct ParseModuleId(usize); | ||
|
||
impl ParseModuleId { | ||
pub fn new(index: usize) -> Self { | ||
ParseModuleId(index) | ||
} | ||
|
||
/// Returns the index that identifies the type. | ||
pub fn index(&self) -> usize { | ||
self.0 | ||
} | ||
|
||
pub(crate) fn get(&self, engines: &crate::Engines) -> Arc<RwLock<ParseModule>> { | ||
engines.pme().get(self) | ||
} | ||
|
||
pub fn read<R>(&self, engines: &crate::Engines, f: impl Fn(&ParseModule) -> R) -> R { | ||
let value = self.get(engines); | ||
let value = value.read().unwrap(); | ||
f(&value) | ||
} | ||
|
||
pub fn write<R>( | ||
&self, | ||
engines: &crate::Engines, | ||
mut f: impl FnMut(&mut ParseModule) -> R, | ||
) -> R { | ||
let value = self.get(engines); | ||
let mut value = value.write().unwrap(); | ||
f(&mut value) | ||
} | ||
} | ||
|
||
impl DebugWithEngines for ParseModuleId { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, engines: &crate::Engines) -> std::fmt::Result { | ||
let name = self.read(engines, |m| m.name.clone()); | ||
write!(f, "{:?}", name) | ||
} | ||
} | ||
|
||
/// The Parsed Module Engine manages a relationship between module ids and their corresponding | ||
/// parsed module structures. | ||
#[derive(Debug, Default, Clone)] | ||
pub struct ParsedModuleEngine { | ||
slab: ConcurrentSlabMut<ParseModule>, | ||
} | ||
|
||
impl ParsedModuleEngine { | ||
/// This function provides the namespace module corresponding to a specified module ID. | ||
pub fn get(&self, module_id: &ParseModuleId) -> Arc<RwLock<ParseModule>> { | ||
self.slab.get(module_id.index()) | ||
} | ||
|
||
pub fn read<R>(&self, module_id: &ParseModuleId, f: impl Fn(&ParseModule) -> R) -> R { | ||
let value = self.slab.get(module_id.index()); | ||
let value = value.read().unwrap(); | ||
f(&value) | ||
} | ||
|
||
pub fn write<R>(&self, module_id: &ParseModuleId, f: impl Fn(&mut ParseModule) -> R) -> R { | ||
let value = self.slab.get(module_id.index()); | ||
let mut value = value.write().unwrap(); | ||
f(&mut value) | ||
} | ||
|
||
pub fn insert(&self, value: ParseModule) -> ParseModuleId { | ||
let id = ParseModuleId(self.slab.insert(value)); | ||
self.write(&id, |m| { | ||
m.id = id; | ||
}); | ||
id | ||
} | ||
} |
Oops, something went wrong.