Skip to content

Commit 6eaa2cf

Browse files
committed
Add benchmark test for mbe
1 parent ac59584 commit 6eaa2cf

File tree

6 files changed

+36669
-0
lines changed

6 files changed

+36669
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/mbe/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ parser = { path = "../parser", version = "0.0.0" }
1919
tt = { path = "../tt", version = "0.0.0" }
2020
test_utils = { path = "../test_utils", version = "0.0.0" }
2121

22+
[dev-dependencies]
23+
profile = { path = "../profile", version = "0.0.0" }
24+

crates/mbe/src/benchmark.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::fs;
2+
3+
use profile::StopWatch;
4+
use rustc_hash::FxHashMap;
5+
use syntax::{
6+
ast::{self, NameOwner},
7+
AstNode,
8+
};
9+
use test_utils::{project_dir, skip_slow_tests};
10+
11+
use crate::{ast_to_token_tree, MacroRules};
12+
13+
#[test]
14+
fn parse_a_lot_macro_rules() {
15+
if skip_slow_tests() {
16+
return;
17+
}
18+
let rules = macro_rules_fixtures_tt();
19+
let mut time = StopWatch::start();
20+
for (_, tt) in rules {
21+
MacroRules::parse(&tt).unwrap();
22+
}
23+
println!("elapsed time : {}", time.elapsed());
24+
}
25+
26+
#[test]
27+
fn expand_a_lot_macro_rules() {
28+
if skip_slow_tests() {
29+
return;
30+
}
31+
let rules = macro_rules_fixtures();
32+
let invocations = invocation_fixtures();
33+
34+
let mut time = StopWatch::start();
35+
for (idx, (id, tt)) in invocations.into_iter().enumerate() {
36+
let res = rules[&id].expand(&tt);
37+
if let Some(err) = res.err {
38+
println!("Faile to expand [{}] {}: error = {}, content = {}", idx, id, err, tt);
39+
}
40+
}
41+
println!("elapsed time : {}", time.elapsed());
42+
}
43+
44+
fn macro_rules_fixtures() -> FxHashMap<String, MacroRules> {
45+
macro_rules_fixtures_tt()
46+
.into_iter()
47+
.map(|(id, tt)| (id, MacroRules::parse(&tt).unwrap()))
48+
.collect()
49+
}
50+
51+
fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree> {
52+
let rules = macro_rules_fixtures_ast();
53+
rules
54+
.into_iter()
55+
.map(|(id, rule)| {
56+
let (def_tt, _) = ast_to_token_tree(&rule.token_tree().unwrap()).unwrap();
57+
(id, def_tt)
58+
})
59+
.collect()
60+
}
61+
62+
fn macro_rules_fixtures_ast() -> FxHashMap<String, ast::MacroRules> {
63+
let file = project_dir().join("crates/mbe/test_data/fixture-rules.rs");
64+
let big_file = fs::read_to_string(file).unwrap();
65+
let source_file = ast::SourceFile::parse(&big_file).ok().unwrap();
66+
67+
source_file
68+
.syntax()
69+
.descendants()
70+
.filter_map(ast::MacroRules::cast)
71+
.map(|it| (it.name().unwrap().to_string(), it))
72+
.collect()
73+
}
74+
75+
fn invocation_fixtures() -> FxHashMap<String, tt::Subtree> {
76+
let file = project_dir().join("crates/mbe/test_data/fixture-invocations.rs");
77+
let big_file = fs::read_to_string(file).unwrap();
78+
// Replace $crate to crate, since $crate may transform to crate name in realilty
79+
let big_file = big_file.replace("$crate", "crate");
80+
81+
let source_file = ast::SourceFile::parse(&big_file).ok().unwrap();
82+
83+
source_file
84+
.syntax()
85+
.descendants()
86+
.filter_map(ast::MacroCall::cast)
87+
.map(|it| {
88+
let id = it.path().unwrap().to_string();
89+
let (invocation_tt, _) = ast_to_token_tree(&it.token_tree().unwrap()).unwrap();
90+
(id, invocation_tt)
91+
})
92+
.collect()
93+
}

crates/mbe/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ mod subtree_source;
1212
#[cfg(test)]
1313
mod tests;
1414

15+
#[cfg(test)]
16+
mod benchmark;
17+
1518
use std::fmt;
1619

1720
use test_utils::mark;

0 commit comments

Comments
 (0)