about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2016-11-04 16:49:31 -0700
committerAlex Crichton <alex@alexcrichton.com>2016-11-05 10:50:24 -0700
commitb9cbbe184a7699e2a308ad89f41dfa88d0fbb790 (patch)
tree137bfa09aaf68e50ac36de4aa272e68d3f6150a1 /src/test
parent9f882b997eaff8def0b31db22fe250776d38af60 (diff)
parent51104e5ca60177b9f646f0c906eac358050664b7 (diff)
Rollup merge of #37569 - jseyfried:improve_expansion_perf, r=eddyb
macros: improve expansion performance

This PR fixes that regression, further improves performance on recursive, `tt`-heavy workloads, and makes a variety of other improvements to parsing and expansion performance.

Expansion performance improvements:

| Test case      | Run-time | Memory usage |
| -------------- | -------- | ------------ |
| libsyntax      | 8%       | 10%          |
| librustc       | 15%      | 6%           |
| librustc_trans | 30%      | 6%           |
| #37074         | 20%      | 15%          |
| #34630         | 40%      | 8%           |

r? @eddyb
Diffstat (limited to 'src/test')
-rw-r--r--src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs61
1 files changed, 33 insertions, 28 deletions
diff --git a/src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs b/src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs
index 5229d42f1fd..6ac0d5ad1a3 100644
--- a/src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs
+++ b/src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs
@@ -18,10 +18,10 @@ extern crate syntax_pos;
 extern crate rustc;
 extern crate rustc_plugin;
 
-use syntax::parse::token::{self, str_to_ident, NtExpr, NtPat};
+use syntax::parse::token::{str_to_ident, NtExpr, NtPat};
 use syntax::ast::{Pat};
 use syntax::tokenstream::{TokenTree};
-use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
+use syntax::ext::base::{ExtCtxt, MacResult, MacEager};
 use syntax::ext::build::AstBuilder;
 use syntax::ext::tt::macro_parser::{MatchedSeq, MatchedNonterminal};
 use syntax::ext::tt::macro_parser::{Success, Failure, Error};
@@ -30,35 +30,12 @@ use syntax::ptr::P;
 use syntax_pos::Span;
 use rustc_plugin::Registry;
 
-fn expand_mbe_matches(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
+fn expand_mbe_matches(cx: &mut ExtCtxt, _: Span, args: &[TokenTree])
         -> Box<MacResult + 'static> {
 
     let mbe_matcher = quote_matcher!(cx, $matched:expr, $($pat:pat)|+);
-
-    let mac_expr = match TokenTree::parse(cx, &mbe_matcher[..], args) {
-        Success(map) => {
-            match (&*map[&str_to_ident("matched")], &*map[&str_to_ident("pat")]) {
-                (&MatchedNonterminal(NtExpr(ref matched_expr)),
-                 &MatchedSeq(ref pats, seq_sp)) => {
-                    let pats: Vec<P<Pat>> = pats.iter().map(|pat_nt|
-                        if let &MatchedNonterminal(NtPat(ref pat)) = &**pat_nt {
-                            pat.clone()
-                        } else {
-                            unreachable!()
-                        }
-                    ).collect();
-                    let arm = cx.arm(seq_sp, pats, cx.expr_bool(seq_sp, true));
-
-                    quote_expr!(cx,
-                        match $matched_expr {
-                            $arm
-                            _ => false
-                        }
-                    )
-                }
-                _ => unreachable!()
-            }
-        }
+    let map = match TokenTree::parse(cx, &mbe_matcher, args) {
+        Success(map) => map,
         Failure(_, tok) => {
             panic!("expected Success, but got Failure: {}", parse_failure_msg(tok));
         }
@@ -67,6 +44,34 @@ fn expand_mbe_matches(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
         }
     };
 
+    let matched_nt = match *map[&str_to_ident("matched")] {
+        MatchedNonterminal(ref nt) => nt.clone(),
+        _ => unreachable!(),
+    };
+
+    let mac_expr = match (&*matched_nt, &*map[&str_to_ident("pat")]) {
+        (&NtExpr(ref matched_expr), &MatchedSeq(ref pats, seq_sp)) => {
+            let pats: Vec<P<Pat>> = pats.iter().map(|pat_nt| {
+                match **pat_nt {
+                    MatchedNonterminal(ref nt) => match **nt {
+                        NtPat(ref pat) => pat.clone(),
+                        _ => unreachable!(),
+                    },
+                    _ => unreachable!(),
+                }
+            }).collect();
+            let arm = cx.arm(seq_sp, pats, cx.expr_bool(seq_sp, true));
+
+            quote_expr!(cx,
+                match $matched_expr {
+                    $arm
+                    _ => false
+                }
+            )
+        }
+        _ => unreachable!()
+    };
+
     MacEager::expr(mac_expr)
 }