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

Pratt Parser Optimisation #701

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ harness = false
name = "cbor"
harness = false

[[bench]]
name = "pratt"
harness = false
required-features = ["pratt"]

[[example]]
name = "nano_rust"
required-features = ["label"]
Expand Down
66 changes: 66 additions & 0 deletions benches/pratt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use chumsky::{pratt::*, prelude::*};
use criterion::{criterion_group, criterion_main, Criterion};
use std::hint::black_box;

mod utils;

static CORPUS: &str = include_str!("samples/pratt.txt");

fn bench_pratt(c: &mut Criterion) {
c.bench_function("pratt", {
move |b| b.iter(|| black_box(parser().parse(CORPUS)).unwrap())
});
}

criterion_group!(
name = benches;
config = utils::make_criterion();
targets = bench_pratt
);

criterion_main!(benches);

pub enum Expr {
Num(i64),
Neg(Box<Self>),
Fact(Box<Self>),
Pow(Box<Self>, Box<Self>),
Mul(Box<Self>, Box<Self>),
Div(Box<Self>, Box<Self>),
Rem(Box<Self>, Box<Self>),
Add(Box<Self>, Box<Self>),
Sub(Box<Self>, Box<Self>),
}

fn parser<'src>() -> impl Parser<'src, &'src str, Expr> {
recursive(|expr| {
let num = text::int(10).map(|s: &str| Expr::Num(s.parse().unwrap()));

let atom = choice((num, expr.delimited_by(just('('), just(')')))).padded();

let op = |c| just(c).padded();

atom.pratt((
postfix(10, op('!'), |x, _, _| Expr::Fact(Box::new(x))),
prefix(5, op('-'), |_, x, _| Expr::Neg(Box::new(x))),
infix(right(10), op('^'), |x, _, y, _| {
Expr::Pow(Box::new(x), Box::new(y))
}),
infix(left(8), op('*'), |x, _, y, _| {
Expr::Mul(Box::new(x), Box::new(y))
}),
infix(left(8), op('/'), |x, _, y, _| {
Expr::Div(Box::new(x), Box::new(y))
}),
infix(left(8), op('%'), |x, _, y, _| {
Expr::Rem(Box::new(x), Box::new(y))
}),
infix(left(2), op('+'), |x, _, y, _| {
Expr::Add(Box::new(x), Box::new(y))
}),
infix(left(2), op('-'), |x, _, y, _| {
Expr::Sub(Box::new(x), Box::new(y))
}),
))
})
}
Loading
Loading