1// $ cargo bench --features full,test --bench file
2
3#![feature(rustc_private, test)]
4#![recursion_limit = "1024"]
5#![allow(
6 clippy::items_after_statements,
7 clippy::missing_panics_doc,
8 clippy::must_use_candidate
9)]
10
11extern crate test;
12
13#[macro_use]
14#[path = "../tests/macros/mod.rs"]
15mod macros;
16
17#[path = "../tests/common/mod.rs"]
18mod common;
19#[path = "../tests/repo/mod.rs"]
20pub mod repo;
21
22use proc_macro2::{Span, TokenStream};
23use std::fs;
24use std::str::FromStr;
25use syn::parse::{ParseStream, Parser};
26use test::Bencher;
27
28const FILE: &str = "tests/rust/library/core/src/str/mod.rs";
29
30fn get_tokens() -> TokenStream {
31 repo::clone_rust();
32 let content = fs::read_to_string(FILE).unwrap();
33 TokenStream::from_str(&content).unwrap()
34}
35
36#[bench]
37fn baseline(b: &mut Bencher) {
38 let tokens = get_tokens();
39 b.iter(|| drop(tokens.clone()));
40}
41
42#[bench]
43fn create_token_buffer(b: &mut Bencher) {
44 let tokens = get_tokens();
45 fn immediate_fail(_input: ParseStream) -> syn::Result<()> {
46 Err(syn::Error::new(Span::call_site(), ""))
47 }
48 b.iter(|| immediate_fail.parse2(tokens.clone()));
49}
50
51#[bench]
52fn parse_file(b: &mut Bencher) {
53 let tokens = get_tokens();
54 b.iter(|| syn::parse2::<syn::File>(tokens.clone()));
55}
56