1 //! This module add real world mbe example for benchmark tests
3 use rustc_hash::FxHashMap;
8 use test_utils::{bench, bench_fixture, skip_slow_tests};
11 parser::{Op, RepeatKind, Separator},
12 syntax_node_to_token_tree, DeclarativeMacro,
16 fn benchmark_parse_macro_rules() {
17 if skip_slow_tests() {
20 let rules = macro_rules_fixtures_tt();
22 let _pt = bench("mbe parse macro rules");
23 rules.values().map(|it| DeclarativeMacro::parse_macro_rules(it).unwrap().rules.len()).sum()
25 assert_eq!(hash, 1144);
29 fn benchmark_expand_macro_rules() {
30 if skip_slow_tests() {
33 let rules = macro_rules_fixtures();
34 let invocations = invocation_fixtures(&rules);
37 let _pt = bench("mbe expand macro rules");
41 let res = rules[&id].expand(&tt);
42 assert!(res.err.is_none());
43 res.value.token_trees.len()
47 assert_eq!(hash, 69413);
50 fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro> {
51 macro_rules_fixtures_tt()
53 .map(|(id, tt)| (id, DeclarativeMacro::parse_macro_rules(&tt).unwrap()))
57 fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree> {
58 let fixture = bench_fixture::numerous_macro_rules();
59 let source_file = ast::SourceFile::parse(&fixture).ok().unwrap();
64 .filter_map(ast::MacroRules::cast)
66 let id = rule.name().unwrap().to_string();
67 let (def_tt, _) = syntax_node_to_token_tree(rule.token_tree().unwrap().syntax());
73 /// Generate random invocation fixtures from rules
74 fn invocation_fixtures(rules: &FxHashMap<String, DeclarativeMacro>) -> Vec<(String, tt::Subtree)> {
75 let mut seed = 123456789;
76 let mut res = Vec::new();
78 for (name, it) in rules {
79 for rule in &it.rules {
82 // The input are generated by filling the `Op` randomly.
83 // However, there are some cases generated are ambiguous for expanding, for example:
86 // ($($t:ident),* as $ty:ident) => {}
88 // m!(as u32); // error: local ambiguity: multiple parsing options: built-in NTs ident ('t') or 1 other option.
91 // So we just skip any error cases and try again
94 let mut subtree = tt::Subtree::default();
95 for op in rule.lhs.iter() {
96 collect_from_op(op, &mut subtree, &mut seed);
98 if it.expand(&subtree).err.is_none() {
99 res.push((name.clone(), subtree));
104 panic!("invocaton fixture {} cannot be generated.\n", name);
112 fn collect_from_op(op: &Op, parent: &mut tt::Subtree, seed: &mut usize) {
114 Op::Var { kind, .. } => match kind.as_ref().map(|it| it.as_str()) {
115 Some("ident") => parent.token_trees.push(make_ident("foo")),
116 Some("ty") => parent.token_trees.push(make_ident("Foo")),
117 Some("tt") => parent.token_trees.push(make_ident("foo")),
118 Some("vis") => parent.token_trees.push(make_ident("pub")),
119 Some("pat") => parent.token_trees.push(make_ident("foo")),
120 Some("path") => parent.token_trees.push(make_ident("foo")),
121 Some("literal") => parent.token_trees.push(make_literal("1")),
122 Some("expr") => parent.token_trees.push(make_ident("foo")),
123 Some("lifetime") => {
124 parent.token_trees.push(make_punct('\''));
125 parent.token_trees.push(make_ident("a"));
128 parent.token_trees.push(make_subtree(tt::DelimiterKind::Brace, None))
131 parent.token_trees.push(make_ident("fn"));
132 parent.token_trees.push(make_ident("foo"));
133 parent.token_trees.push(make_subtree(tt::DelimiterKind::Parenthesis, None));
134 parent.token_trees.push(make_subtree(tt::DelimiterKind::Brace, None));
137 parent.token_trees.push(make_ident("foo"));
138 parent.token_trees.push(make_subtree(tt::DelimiterKind::Parenthesis, None));
142 Some(kind) => panic!("Unhandled kind {}", kind),
144 Op::Leaf(leaf) => parent.token_trees.push(leaf.clone().into()),
145 Op::Repeat { tokens, kind, separator } => {
147 let cnt = match kind {
148 RepeatKind::ZeroOrMore => rand(seed) % max,
149 RepeatKind::OneOrMore => 1 + rand(seed) % max,
150 RepeatKind::ZeroOrOne => rand(seed) % 2,
153 for it in tokens.iter() {
154 collect_from_op(it, parent, seed);
157 if let Some(sep) = separator {
159 Separator::Literal(it) => {
160 parent.token_trees.push(tt::Leaf::Literal(it.clone()).into())
162 Separator::Ident(it) => {
163 parent.token_trees.push(tt::Leaf::Ident(it.clone()).into())
165 Separator::Puncts(puncts) => {
167 parent.token_trees.push(tt::Leaf::Punct(*it).into())
175 Op::Subtree { tokens, delimiter } => {
176 let mut subtree = tt::Subtree { delimiter: *delimiter, token_trees: Vec::new() };
177 tokens.iter().for_each(|it| {
178 collect_from_op(it, &mut subtree, seed);
180 parent.token_trees.push(subtree.into());
182 Op::Ignore { .. } | Op::Index { .. } => {}
185 // Simple linear congruential generator for determistic result
186 fn rand(seed: &mut usize) -> usize {
189 *seed = usize::wrapping_add(usize::wrapping_mul(*seed, a), c);
192 fn make_ident(ident: &str) -> tt::TokenTree {
193 tt::Leaf::Ident(tt::Ident { id: tt::TokenId::unspecified(), text: SmolStr::new(ident) })
196 fn make_punct(char: char) -> tt::TokenTree {
197 tt::Leaf::Punct(tt::Punct {
198 id: tt::TokenId::unspecified(),
200 spacing: tt::Spacing::Alone,
204 fn make_literal(lit: &str) -> tt::TokenTree {
205 tt::Leaf::Literal(tt::Literal {
206 id: tt::TokenId::unspecified(),
207 text: SmolStr::new(lit),
212 kind: tt::DelimiterKind,
213 token_trees: Option<Vec<tt::TokenTree>>,
216 delimiter: Some(tt::Delimiter { id: tt::TokenId::unspecified(), kind }),
217 token_trees: token_trees.unwrap_or_default(),