interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / syn / benches / rust.rs

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