Skip to content

Commit 12bac84

Browse files
committed
Add basic benchmark of libproc_macro
1 parent a0fa7dd commit 12bac84

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ nightly = []
4242
[badges]
4343
travis-ci = { repository = "alexcrichton/proc-macro2" }
4444

45+
[workspace]
46+
members = ["benches/bench-libproc-macro"]
47+
4548
[patch.crates-io]
4649
# Our doc tests depend on quote which depends on proc-macro2. Without this line,
4750
# the proc-macro2 dependency of quote would be the released version of
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "bench-libproc-macro"
3+
version = "0.0.0"
4+
edition = "2018"
5+
publish = false
6+
7+
[lib]
8+
path = "lib.rs"
9+
proc-macro = true
10+
11+
[[bin]]
12+
name = "bench-libproc-macro"
13+
path = "main.rs"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Example output:
2+
3+
```console
4+
$ cargo check --release
5+
6+
Compiling bench-libproc-macro v0.0.0
7+
STRING: 8 millis
8+
TOKENSTREAM: 25721 millis
9+
Finished release [optimized] target(s) in 26.15s
10+
```

benches/bench-libproc-macro/lib.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
extern crate proc_macro;
2+
3+
use proc_macro::{Ident, Punct, Spacing, Span, TokenStream, TokenTree};
4+
use std::iter::once;
5+
use std::time::Instant;
6+
7+
const N: u32 = 7000;
8+
9+
#[proc_macro]
10+
pub fn bench(_input: TokenStream) -> TokenStream {
11+
let start = Instant::now();
12+
let mut string = String::new();
13+
for _ in 0..N {
14+
string += "core";
15+
string += ":";
16+
string += ":";
17+
string += "option";
18+
string += ":";
19+
string += ":";
20+
string += "Option";
21+
string += ":";
22+
string += ":";
23+
string += "None";
24+
string += ",";
25+
}
26+
string.parse::<TokenStream>().unwrap();
27+
eprintln!("STRING: {} millis", start.elapsed().as_millis());
28+
29+
let start = Instant::now();
30+
let span = Span::call_site();
31+
let mut tokens = TokenStream::new();
32+
for _ in 0..N {
33+
// Similar to what is emitted by quote.
34+
tokens.extend(once(TokenTree::Ident(Ident::new("core", span))));
35+
tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Joint))));
36+
tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Alone))));
37+
tokens.extend(once(TokenTree::Ident(Ident::new("option", span))));
38+
tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Joint))));
39+
tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Alone))));
40+
tokens.extend(once(TokenTree::Ident(Ident::new("Option", span))));
41+
tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Joint))));
42+
tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Alone))));
43+
tokens.extend(once(TokenTree::Ident(Ident::new("None", span))));
44+
tokens.extend(once(TokenTree::Punct(Punct::new(',', Spacing::Joint))));
45+
}
46+
eprintln!("TOKENSTREAM: {} millis", start.elapsed().as_millis());
47+
48+
TokenStream::new()
49+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bench_libproc_macro::bench!();
2+
3+
fn main() {}

0 commit comments

Comments
 (0)