aboutsummaryrefslogtreecommitdiff
path: root/proc-macro2/benches/bench-libproc-macro/lib.rs
blob: 46b3711ee45ea8fc482d5ca41de177a7277598e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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()
}