aboutsummaryrefslogtreecommitdiff
path: root/proc-macro2/benches
diff options
context:
space:
mode:
Diffstat (limited to 'proc-macro2/benches')
-rw-r--r--proc-macro2/benches/bench-libproc-macro/Cargo.toml13
-rw-r--r--proc-macro2/benches/bench-libproc-macro/README.md10
-rw-r--r--proc-macro2/benches/bench-libproc-macro/lib.rs49
-rw-r--r--proc-macro2/benches/bench-libproc-macro/main.rs3
4 files changed, 75 insertions, 0 deletions
diff --git a/proc-macro2/benches/bench-libproc-macro/Cargo.toml b/proc-macro2/benches/bench-libproc-macro/Cargo.toml
new file mode 100644
index 0000000..41d106d
--- /dev/null
+++ b/proc-macro2/benches/bench-libproc-macro/Cargo.toml
@@ -0,0 +1,13 @@
+[package]
+name = "bench-libproc-macro"
+version = "0.0.0"
+edition = "2018"
+publish = false
+
+[lib]
+path = "lib.rs"
+proc-macro = true
+
+[[bin]]
+name = "bench-libproc-macro"
+path = "main.rs"
diff --git a/proc-macro2/benches/bench-libproc-macro/README.md b/proc-macro2/benches/bench-libproc-macro/README.md
new file mode 100644
index 0000000..80e4939
--- /dev/null
+++ b/proc-macro2/benches/bench-libproc-macro/README.md
@@ -0,0 +1,10 @@
+Example output:
+
+```console
+$ cargo check --release
+
+ Compiling bench-libproc-macro v0.0.0
+STRING: 8 millis
+TOKENSTREAM: 25721 millis
+ Finished release [optimized] target(s) in 26.15s
+```
diff --git a/proc-macro2/benches/bench-libproc-macro/lib.rs b/proc-macro2/benches/bench-libproc-macro/lib.rs
new file mode 100644
index 0000000..46b3711
--- /dev/null
+++ b/proc-macro2/benches/bench-libproc-macro/lib.rs
@@ -0,0 +1,49 @@
+extern crate proc_macro;
+
+use proc_macro::{Ident, Punct, Spacing, Span, TokenStream, TokenTree};
+use std::iter::once;
+use std::time::Instant;
+
+const N: u32 = 7000;
+
+#[proc_macro]
+pub fn bench(_input: TokenStream) -> TokenStream {
+ let start = Instant::now();
+ let mut string = String::new();
+ for _ in 0..N {
+ string += "core";
+ string += ":";
+ string += ":";
+ string += "option";
+ string += ":";
+ string += ":";
+ string += "Option";
+ string += ":";
+ string += ":";
+ string += "None";
+ string += ",";
+ }
+ string.parse::<TokenStream>().unwrap();
+ eprintln!("STRING: {} millis", start.elapsed().as_millis());
+
+ let start = Instant::now();
+ let span = Span::call_site();
+ let mut tokens = TokenStream::new();
+ for _ in 0..N {
+ // Similar to what is emitted by quote.
+ tokens.extend(once(TokenTree::Ident(Ident::new("core", span))));
+ tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Joint))));
+ tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Alone))));
+ tokens.extend(once(TokenTree::Ident(Ident::new("option", span))));
+ tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Joint))));
+ tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Alone))));
+ tokens.extend(once(TokenTree::Ident(Ident::new("Option", span))));
+ tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Joint))));
+ tokens.extend(once(TokenTree::Punct(Punct::new(':', Spacing::Alone))));
+ tokens.extend(once(TokenTree::Ident(Ident::new("None", span))));
+ tokens.extend(once(TokenTree::Punct(Punct::new(',', Spacing::Joint))));
+ }
+ eprintln!("TOKENSTREAM: {} millis", start.elapsed().as_millis());
+
+ TokenStream::new()
+}
diff --git a/proc-macro2/benches/bench-libproc-macro/main.rs b/proc-macro2/benches/bench-libproc-macro/main.rs
new file mode 100644
index 0000000..34eedf6
--- /dev/null
+++ b/proc-macro2/benches/bench-libproc-macro/main.rs
@@ -0,0 +1,3 @@
+bench_libproc_macro::bench!();
+
+fn main() {}