Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: skipper support inlining #969

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions meta/src/optimizer/mod.rs
Original file line number Diff line number Diff line change
@@ -30,21 +30,22 @@ mod unroller;

/// Takes pest's ASTs and optimizes them
pub fn optimize(rules: Vec<Rule>) -> Vec<OptimizedRule> {
let map = to_hash_map(&rules);
let optimized: Vec<OptimizedRule> = rules
.into_iter()
.map(rotater::rotate)
.map(skipper::skip)
.map(|rule| skipper::skip(rule, &map))
.map(unroller::unroll)
.map(concatenator::concatenate)
.map(factorizer::factor)
.map(lister::list)
.map(rule_to_optimized_rule)
.collect();

let rules = to_hash_map(&optimized);
let optimized_map = to_optimized_hash_map(&optimized);
optimized
.into_iter()
.map(|rule| restorer::restore_on_err(rule, &rules))
.map(|rule| restorer::restore_on_err(rule, &optimized_map))
.collect()
}

@@ -87,12 +88,18 @@ fn rule_to_optimized_rule(rule: Rule) -> OptimizedRule {
}
}

fn to_hash_map(rules: &[OptimizedRule]) -> HashMap<String, OptimizedExpr> {
rules
.iter()
.map(|r| (r.name.clone(), r.expr.clone()))
.collect()
macro_rules! to_hash_map {
($func_name:ident, $rule:ty, $expr:ty) => {
fn $func_name(rules: &[$rule]) -> HashMap<String, $expr> {
rules
.iter()
.map(|r| (r.name.clone(), r.expr.clone()))
.collect()
}
};
}
to_hash_map!(to_hash_map, Rule, Expr);
to_hash_map!(to_optimized_hash_map, OptimizedRule, OptimizedExpr);

/// The optimized version of the pest AST's `Rule`.
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -1040,6 +1047,37 @@ mod tests {
);
}

#[test]
fn inline_skip() {
use crate::ast::Expr::*;
let rules = vec![
Rule {
name: "inline".to_owned(),
ty: RuleType::Atomic,
expr: Str("a".to_owned()),
},
Rule {
name: "skip".to_owned(),
ty: RuleType::Atomic,
expr: box_tree!(Rep(Seq(
NegPred(Choice(
Ident(String::from("inline")),
Str(String::from("b"))
)),
Ident("ANY".to_owned())
))),
},
];
let map = to_hash_map(&rules);
let rule = skipper::skip(rules[1].clone(), &map);
assert!(matches!(rule, Rule { expr: Skip(..), .. }));
let choices = match rule.expr {
Skip(choices) => choices,
_ => unreachable!(),
};
assert_eq!(choices, vec!["a".to_owned(), "b".to_owned()]);
}

#[test]
fn push() {
assert_eq!(
6 changes: 3 additions & 3 deletions meta/src/optimizer/restorer.rs
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ mod tests {
}];

assert_eq!(
restore_on_err(rules[0].clone(), &to_hash_map(&rules)),
restore_on_err(rules[0].clone(), &to_optimized_hash_map(&rules)),
rules[0].clone()
);
}
@@ -123,7 +123,7 @@ mod tests {
};

assert_eq!(
restore_on_err(rules[0].clone(), &to_hash_map(&rules)),
restore_on_err(rules[0].clone(), &to_optimized_hash_map(&rules)),
restored
);
}
@@ -146,7 +146,7 @@ mod tests {
};

assert_eq!(
restore_on_err(rules[0].clone(), &to_hash_map(&rules)),
restore_on_err(rules[0].clone(), &to_optimized_hash_map(&rules)),
restored
);
}
29 changes: 25 additions & 4 deletions meta/src/optimizer/skipper.rs
Original file line number Diff line number Diff line change
@@ -7,15 +7,32 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use std::collections::HashMap;

use crate::ast::*;

pub fn skip(rule: Rule) -> Rule {
fn populate_choices(expr: Expr, mut choices: Vec<String>) -> Option<Expr> {
pub fn skip(rule: Rule, map: &HashMap<String, Expr>) -> Rule {
fn populate_choices(
expr: Expr,
map: &HashMap<String, Expr>,
mut choices: Vec<String>,
) -> Option<Expr> {
match expr {
Expr::Choice(lhs, rhs) => {
if let Expr::Str(string) = *lhs {
choices.push(string);
populate_choices(*rhs, choices)
populate_choices(*rhs, map, choices)
} else if let Expr::Ident(name) = *lhs {
// Try inlining rule in choices
if let Some(Expr::Skip(mut inlined_choices)) = map
.get(&name)
.and_then(|expr| populate_choices(expr.clone(), map, vec![]))
{
choices.append(&mut inlined_choices);
populate_choices(*rhs, map, choices)
} else {
None
}
} else {
None
}
@@ -24,6 +41,10 @@ pub fn skip(rule: Rule) -> Rule {
choices.push(string);
Some(Expr::Skip(choices))
}
// Try inlining single rule
Expr::Ident(name) => map
.get(&name)
.and_then(|expr| populate_choices(expr.clone(), map, choices)),
_ => None,
}
}
@@ -38,7 +59,7 @@ pub fn skip(rule: Rule) -> Rule {
if let Expr::Seq(lhs, rhs) = *expr {
if let (Expr::NegPred(expr), Expr::Ident(ident)) = (*lhs, *rhs) {
if ident == "ANY" {
if let Some(expr) = populate_choices(*expr, vec![]) {
if let Some(expr) = populate_choices(*expr, map, vec![]) {
return expr;
}
}