Skip to content

Add combine based benchmark to catch exponential trait selection #614

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions collector/benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,4 @@ programs.
- **wf-projection-stress-65510**: A stress test which showcases [quadratic
behavior](https://github.com/rust-lang/rust/issues/65510) (in the number of
associated type bounds).
- **trait-stress-68666**: A stress test which constructs a parser in the same manner that `combine` does. Showcases [exponential behavior](https://github.com/rust-lang/rust/issues/68666) in trait selection.
6 changes: 6 additions & 0 deletions collector/benchmarks/trait-stress/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions collector/benchmarks/trait-stress/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "combine"
version = "0.1.0"
authors = ["Markus Westerlind <[email protected]>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[workspace]
64 changes: 64 additions & 0 deletions collector/benchmarks/trait-stress/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::marker::PhantomData;

pub trait Parser<Input> {
type Output;

fn or<P2>(self, p: P2) -> Or<Self, P2>
where
Self: Sized,
P2: Parser<Input, Output = Self::Output>,
{
Or(self, p)
}
}

#[macro_export]
macro_rules! choice {
($first : expr) => {
$first
};
($first : expr, $($rest : expr),+) => {
$first.or(choice!($($rest),+))
}
}

pub struct Or<P1, P2>(P1, P2);
impl<Input, P1, P2> Parser<Input> for Or<P1, P2>
where
P1: Parser<Input, Output = P2::Output>,
P2: Parser<Input>,
{
type Output = P2::Output;
}

pub struct P<I, O>(PhantomData<fn(I) -> O>);
impl<Input, O> Parser<Input> for P<Input, O> {
type Output = O;
}

pub fn token<I>(_: u8) -> impl Parser<I, Output = u8> {
P(PhantomData)
}

fn mcc_payload_item<I>() -> impl Parser<I, Output = u8> {
choice!(
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G'),
token(b'G')
)
}

fn main() {}