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