|
| 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 | +} |
0 commit comments