Make ExprReducer faster
use super::prelude::*;
pub(crate) struct ExprReducer<'script, 'registry, 'meta> {
- visits: u64,
helper: &'meta mut Helper<'script, 'registry>,
}
impl<'script, 'registry, 'meta> ExprReducer<'script, 'registry, 'meta> {
pub(crate) fn new(helper: &'meta mut Helper<'script, 'registry>) -> Self {
- Self { helper, visits: 0 }
+ Self { helper }
}
pub(crate) fn reduce(&mut self, expr: &'meta mut ImutExprInt<'script>) -> Result<()> {
- // Counts the number of visits needed to walk the expr.
self.walk_expr(expr)?;
-
- // TODO: This is slow
- let loops = self.visits;
- for _ in 1..loops {
- self.walk_expr(expr)?;
- }
Ok(())
}
}
@@ -41,9 +33,8 @@ impl<'script, 'registry, 'meta> ImutExprWalker<'script> for ExprReducer<'script,
impl<'script, 'registry, 'meta> ImutExprVisitor<'script>
for ExprReducer<'script, 'registry, 'meta>
{
- fn visit_expr(&mut self, e: &mut ImutExprInt<'script>) -> Result<VisitRes> {
- self.visits += 1;
+ fn leave_expr(&mut self, e: &mut ImutExprInt<'script>) -> Result<()> {
*e = e.clone().try_reduce(self.helper)?;
- Ok(VisitRes::Walk)
+ Ok(())
}
}
