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:
@@ -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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::Output;
|
use crate::Output;
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ pub struct PeepholeOptimizations {
|
|||||||
|
|
||||||
/// The underlying automata for matching optimizations' left-hand sides, and
|
/// The underlying automata for matching optimizations' left-hand sides, and
|
||||||
/// building up the corresponding right-hand side.
|
/// building up the corresponding right-hand side.
|
||||||
pub automata: Automaton<MatchResult, MatchOp, Vec<Action>>,
|
pub automata: Automaton<MatchResult, MatchOp, Box<[Action]>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PeepholeOptimizations {
|
impl PeepholeOptimizations {
|
||||||
|
|||||||
@@ -6,10 +6,10 @@ use peepmatic_runtime::linear;
|
|||||||
/// Construct an automaton from a set of linear optimizations.
|
/// Construct an automaton from a set of linear optimizations.
|
||||||
pub fn automatize(
|
pub fn automatize(
|
||||||
opts: &linear::Optimizations,
|
opts: &linear::Optimizations,
|
||||||
) -> Automaton<linear::MatchResult, linear::MatchOp, Vec<linear::Action>> {
|
) -> Automaton<linear::MatchResult, linear::MatchOp, Box<[linear::Action]>> {
|
||||||
debug_assert!(crate::linear_passes::is_sorted_lexicographically(opts));
|
debug_assert!(crate::linear_passes::is_sorted_lexicographically(opts));
|
||||||
|
|
||||||
let mut builder = Builder::<linear::MatchResult, linear::MatchOp, Vec<linear::Action>>::new();
|
let mut builder = Builder::<linear::MatchResult, linear::MatchOp, Box<[linear::Action]>>::new();
|
||||||
|
|
||||||
for opt in &opts.optimizations {
|
for opt in &opts.optimizations {
|
||||||
let mut insertion = builder.insert();
|
let mut insertion = builder.insert();
|
||||||
@@ -22,7 +22,7 @@ pub fn automatize(
|
|||||||
insertion.set_state_data(inc.operation);
|
insertion.set_state_data(inc.operation);
|
||||||
}
|
}
|
||||||
|
|
||||||
insertion.next(inc.expected, inc.actions.clone());
|
insertion.next(inc.expected, inc.actions.clone().into_boxed_slice());
|
||||||
}
|
}
|
||||||
insertion.finish();
|
insertion.finish();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ use std::num::NonZeroU16;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct PeepholeDotFmt<'a>(pub(crate) &'a PathInterner, pub(crate) &'a IntegerInterner);
|
pub(crate) struct PeepholeDotFmt<'a>(pub(crate) &'a PathInterner, pub(crate) &'a IntegerInterner);
|
||||||
|
|
||||||
impl DotFmt<linear::MatchResult, linear::MatchOp, Vec<linear::Action>> for PeepholeDotFmt<'_> {
|
impl DotFmt<linear::MatchResult, linear::MatchOp, Box<[linear::Action]>> for PeepholeDotFmt<'_> {
|
||||||
fn fmt_transition(
|
fn fmt_transition(
|
||||||
&self,
|
&self,
|
||||||
w: &mut impl Write,
|
w: &mut impl Write,
|
||||||
@@ -73,7 +73,7 @@ impl DotFmt<linear::MatchResult, linear::MatchOp, Vec<linear::Action>> for Peeph
|
|||||||
writeln!(w, "</font>")
|
writeln!(w, "</font>")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fmt_output(&self, w: &mut impl Write, actions: &Vec<linear::Action>) -> io::Result<()> {
|
fn fmt_output(&self, w: &mut impl Write, actions: &Box<[linear::Action]>) -> io::Result<()> {
|
||||||
use linear::Action::*;
|
use linear::Action::*;
|
||||||
|
|
||||||
if actions.is_empty() {
|
if actions.is_empty() {
|
||||||
@@ -84,7 +84,7 @@ impl DotFmt<linear::MatchResult, linear::MatchOp, Vec<linear::Action>> for Peeph
|
|||||||
|
|
||||||
let p = p(self.0);
|
let p = p(self.0);
|
||||||
|
|
||||||
for a in actions {
|
for a in actions.iter() {
|
||||||
match a {
|
match a {
|
||||||
GetLhs { path } => write!(w, "get-lhs @ {}<br/>", p(path))?,
|
GetLhs { path } => write!(w, "get-lhs @ {}<br/>", p(path))?,
|
||||||
UnaryUnquote { operator, operand } => {
|
UnaryUnquote { operator, operand } => {
|
||||||
|
|||||||
Reference in New Issue
Block a user