peepmatic: Save RHS actions as a boxed slice, not vec

A boxed slice is only two words, while a vec is three words. This should cut
down on the memory size of our automata and improve cache usage.
This commit is contained in:
Nick Fitzgerald
2020-05-07 14:14:21 -07:00
parent 210b036320
commit eb2dab0aa4
4 changed files with 44 additions and 7 deletions

View File

@@ -57,6 +57,43 @@ where
}
}
impl<T> Output for Box<[T]>
where
T: Clone + Eq + Hash,
{
fn empty() -> Self {
vec![].into_boxed_slice()
}
fn is_empty(&self) -> bool {
self.len() == 0
}
fn prefix(a: &Self, b: &Self) -> Self {
a.iter()
.cloned()
.zip(b.iter().cloned())
.take_while(|(a, b)| a == b)
.map(|(a, _)| a)
.collect()
}
fn difference(a: &Self, b: &Self) -> Self {
let i = a
.iter()
.zip(b.iter())
.position(|(a, b)| a != b)
.unwrap_or(cmp::min(a.len(), b.len()));
a[i..].to_vec().into_boxed_slice()
}
fn concat(a: &Self, b: &Self) -> Self {
let mut c = a.clone().to_vec();
c.extend(b.iter().cloned());
c.into_boxed_slice()
}
}
#[cfg(test)]
mod tests {
use crate::Output;