Skip to content

Commit 82df64b

Browse files
Merge pull request #516 from nnethercote/add-token-stream-stress
Add `token-stream-stress` benchmark.
2 parents ea95276 + 2e3953f commit 82df64b

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

collector/benchmarks/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ programs.
8181
performance](https://github.com/rust-lang/rust/issues/46449) in the past.
8282
- **regression-31157**: A small program that caused a [large performance
8383
regression](https://github.com/rust-lang/rust/issues/31157) from the past.
84+
- **token-stream-stress**: Constructs a long token stream much like the `quote`
85+
crate does, which caused [quadratic
86+
behavior](https://github.com/rust-lang/rust/issues/65080) in the past.
8487
- **tuple-stress**: Contains a single array of 65,535 nested `(i32, (f64, f64,
8588
f64))` tuples. The data was extracted and reduced from a [program dealing
8689
with grid coordinates](https://github.com/urschrei/ostn15_phf) that was

collector/benchmarks/token-stream-stress/Cargo.lock

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "token-stream-stress"
3+
version = "0.0.0"
4+
edition = "2018"
5+
publish = false
6+
7+
[lib]
8+
path = "src/lib.rs"
9+
proc-macro = true
10+
11+
[[bin]]
12+
name = "token-stream-stress"
13+
path = "src/main.rs"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cargo_opts": "--bin token-stream-stress"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
extern crate proc_macro;
2+
3+
use proc_macro::{Ident, Punct, Spacing, Span, TokenStream, TokenTree};
4+
use std::iter::once;
5+
6+
const N: u32 = 2000;
7+
8+
#[proc_macro]
9+
pub fn bench(_input: TokenStream) -> TokenStream {
10+
let span = Span::call_site();
11+
let mut tokens = TokenStream::new();
12+
for _ in 0..N {
13+
// Similar to what is emitted by `quote`.
14+
tokens.extend(once(TokenTree::Ident(Ident::new("core", span))));
15+
tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Joint))));
16+
tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Alone))));
17+
tokens.extend(once(TokenTree::Ident(Ident::new("option", span))));
18+
tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Joint))));
19+
tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Alone))));
20+
tokens.extend(once(TokenTree::Ident(Ident::new("Option", span))));
21+
tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Joint))));
22+
tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Alone))));
23+
tokens.extend(once(TokenTree::Ident(Ident::new("None", span))));
24+
tokens.extend(once(TokenTree::Punct(Punct::new(',', Spacing::Joint))));
25+
}
26+
27+
TokenStream::new()
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
token_stream_stress::bench!();
2+
3+
fn main() {}

0 commit comments

Comments
 (0)