Debian Codegraph @

third_party/rust/syn/benches/rust.rs

raw
1 // $ cargo bench --features full,test --bench rust
2 //
3 // Syn only, useful for profiling:
4 // $ RUSTFLAGS='--cfg syn_only' cargo build --release --features full,test --bench rust
5
6 #![cfg_attr(not(syn_only), feature(rustc_private))]
7 #![recursion_limit = "1024"]
8 #![allow(
9 clippy::arc_with_non_send_sync,
10 clippy::cast_lossless,
11 clippy::let_underscore_untyped,
12 clippy::manual_let_else,
13 clippy::match_like_matches_macro,
14 clippy::needless_lifetimes,
15 clippy::uninlined_format_args,
16 clippy::unnecessary_wraps
17 )]
18
19 #[macro_use]
20 #[path = "../tests/macros/mod.rs"]
21 mod macros;
22
23 #[allow(dead_code)]
24 #[path = "../tests/repo/mod.rs"]
25 mod repo;
26
27 use std::fs;
28 use std::path::Path;
29 use std::time::{Duration, Instant};
30
31 #[cfg(not(syn_only))]
32 mod tokenstream_parse {
33 use proc_macro2::TokenStream;
34 use std::path::Path;
35 use std::str::FromStr;
36
37 pub fn bench(_path: &Path, content: &str) -> Result<(), ()> {
38 TokenStream::from_str(content).map(drop).map_err(drop)
39 }
40 }
41
42 mod syn_parse {
43 use std::path::Path;
44
45 pub fn bench(_path: &Path, content: &str) -> Result<(), ()> {
46 syn::parse_file(content).map(drop).map_err(drop)
47 }
48 }
49
50 #[cfg(not(syn_only))]
51 mod librustc_parse {
52 extern crate rustc_data_structures;
53 extern crate rustc_driver;
54 extern crate rustc_error_messages;
55 extern crate rustc_errors;
56 extern crate rustc_parse;
57 extern crate rustc_session;
58 extern crate rustc_span;
59
60 use crate::repo;
61 use rustc_data_structures::sync::Lrc;
62 use rustc_error_messages::FluentBundle;
63 use rustc_errors::{emitter::Emitter, translation::Translate, DiagCtxt, DiagInner};
64 use rustc_session::parse::ParseSess;
65 use rustc_span::source_map::{FilePathMapping, SourceMap};
66 use rustc_span::FileName;
67 use std::path::Path;
68
69 pub fn bench(path: &Path, content: &str) -> Result<(), ()> {
70 struct SilentEmitter;
71
72 impl Emitter for SilentEmitter {
73 fn emit_diagnostic(&mut self, _diag: DiagInner) {}
74 fn source_map(&self) -> Option<&SourceMap> {
75 None
76 }
77 }
78
79 impl Translate for SilentEmitter {
80 fn fluent_bundle(&self) -> Option<&FluentBundle> {
81 None
82 }
83 fn fallback_fluent_bundle(&self) -> &FluentBundle {
84 panic!("silent emitter attempted to translate a diagnostic");
85 }
86 }
87
88 let edition = repo::edition(path).parse().unwrap();
89 rustc_span::create_session_if_not_set_then(edition, |_| {
90 let source_map = Lrc::new(SourceMap::new(FilePathMapping::empty()));
91 let emitter = Box::new(SilentEmitter);
92 let handler = DiagCtxt::new(emitter);
93 let sess = ParseSess::with_dcx(handler, source_map);
94 let name = FileName::Custom("bench".to_owned());
95 let mut parser =
96 rustc_parse::new_parser_from_source_str(&sess, name, content.to_owned()).unwrap();
97 if let Err(diagnostic) = parser.parse_crate_mod() {
98 diagnostic.cancel();
99 return Err(());
100 };
101 Ok(())
102 })
103 }
104 }
105
106 #[cfg(not(syn_only))]
107 mod read_from_disk {
108 use std::path::Path;
109
110 pub fn bench(_path: &Path, content: &str) -> Result<(), ()> {
111 let _ = content;
112 Ok(())
113 }
114 }
115
116 fn exec(mut codepath: impl FnMut(&Path, &str) -> Result<(), ()>) -> Duration {
117 let begin = Instant::now();
118 let mut success = 0;
119 let mut total = 0;
120
121 ["tests/rust/compiler", "tests/rust/library"]
122 .iter()
123 .flat_map(|dir| {
124 walkdir::WalkDir::new(dir)
125 .into_iter()
126 .filter_entry(repo::base_dir_filter)
127 })
128 .for_each(|entry| {
129 let entry = entry.unwrap();
130 let path = entry.path();
131 if path.is_dir() {
132 return;
133 }
134 let content = fs::read_to_string(path).unwrap();
135 let ok = codepath(path, &content).is_ok();
136 success += ok as usize;
137 total += 1;
138 if !ok {
139 eprintln!("FAIL {}", path.display());
140 }
141 });
142
143 assert_eq!(success, total);
144 begin.elapsed()
145 }
146
147 fn main() {
148 repo::clone_rust();
149
150 macro_rules! testcases {
151 ($($(#[$cfg:meta])* $name:ident,)*) => {
152 [
153 $(
154 $(#[$cfg])*
155 (stringify!($name), $name::bench as fn(&Path, &str) -> Result<(), ()>),
156 )*
157 ]
158 };
159 }
160
161 #[cfg(not(syn_only))]
162 {
163 let mut lines = 0;
164 let mut files = 0;
165 exec(|_path, content| {
166 lines += content.lines().count();
167 files += 1;
168 Ok(())
169 });
170 eprintln!("\n{} lines in {} files", lines, files);
171 }
172
173 for (name, f) in testcases!(
174 #[cfg(not(syn_only))]
175 read_from_disk,
176 #[cfg(not(syn_only))]
177 tokenstream_parse,
178 syn_parse,
179 #[cfg(not(syn_only))]
180 librustc_parse,
181 ) {
182 eprint!("{:20}", format!("{}:", name));
183 let elapsed = exec(f);
184 eprintln!(
185 "elapsed={}.{:03}s",
186 elapsed.as_secs(),
187 elapsed.subsec_millis(),
188 );
189 }
190 eprintln!();
191 }