Skip to content

Commit a117bba

Browse files
authored
Auto merge of #37318 - nnethercote:html5ever-more, r=nrc,eddyb
Avoid some allocations in the macro parser These three commits reduce the number of heap allocations done when compiling rustc-benchmarks/html5ever-2016-08-25 by 20%, from 16.5M to 13.3M. This speeds up (debug) compilation of it with a stage1 compiler by about 7%.
2 parents 0eb4d46 + b817cf8 commit a117bba

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,22 @@ pub fn nameize(p_s: &ParseSess, ms: &[TokenTree], res: &[Rc<NamedMatch>])
251251

252252
pub enum ParseResult<T> {
253253
Success(T),
254-
/// Arm failed to match
255-
Failure(syntax_pos::Span, String),
254+
/// Arm failed to match. If the second parameter is `token::Eof`, it
255+
/// indicates an unexpected end of macro invocation. Otherwise, it
256+
/// indicates that no rules expected the given token.
257+
Failure(syntax_pos::Span, Token),
256258
/// Fatal error (malformed macro?). Abort compilation.
257259
Error(syntax_pos::Span, String)
258260
}
259261

262+
pub fn parse_failure_msg(tok: Token) -> String {
263+
match tok {
264+
token::Eof => "unexpected end of macro invocation".to_string(),
265+
_ => format!("no rules expected the token `{}`", pprust::token_to_string(&tok)),
266+
}
267+
}
268+
260269
pub type NamedParseResult = ParseResult<HashMap<Ident, Rc<NamedMatch>>>;
261-
pub type PositionalParseResult = ParseResult<Vec<Rc<NamedMatch>>>;
262270

263271
/// Perform a token equality check, ignoring syntax context (that is, an
264272
/// unhygienic comparison)
@@ -425,8 +433,8 @@ pub fn parse(sess: &ParseSess,
425433
cur_eis.push(ei);
426434
}
427435
TokenTree::Token(_, ref t) => {
428-
let mut ei_t = ei.clone();
429436
if token_name_eq(t,&tok) {
437+
let mut ei_t = ei.clone();
430438
ei_t.idx += 1;
431439
next_eis.push(ei_t);
432440
}
@@ -446,7 +454,7 @@ pub fn parse(sess: &ParseSess,
446454
} else if eof_eis.len() > 1 {
447455
return Error(sp, "ambiguity: multiple successful parses".to_string());
448456
} else {
449-
return Failure(sp, "unexpected end of macro invocation".to_string());
457+
return Failure(sp, token::Eof);
450458
}
451459
} else {
452460
if (!bb_eis.is_empty() && !next_eis.is_empty())
@@ -467,8 +475,7 @@ pub fn parse(sess: &ParseSess,
467475
}
468476
))
469477
} else if bb_eis.is_empty() && next_eis.is_empty() {
470-
return Failure(sp, format!("no rules expected the token `{}`",
471-
pprust::token_to_string(&tok)));
478+
return Failure(sp, tok);
472479
} else if !next_eis.is_empty() {
473480
/* Now process the next token */
474481
while !next_eis.is_empty() {

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ext::expand::{Expansion, ExpansionKind};
1616
use ext::placeholders;
1717
use ext::tt::macro_parser::{Success, Error, Failure};
1818
use ext::tt::macro_parser::{MatchedSeq, MatchedNonterminal};
19-
use ext::tt::macro_parser::parse;
19+
use ext::tt::macro_parser::{parse, parse_failure_msg};
2020
use parse::ParseSess;
2121
use parse::lexer::new_tt_reader;
2222
use parse::parser::{Parser, Restrictions};
@@ -97,7 +97,7 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
9797

9898
// Which arm's failure should we report? (the one furthest along)
9999
let mut best_fail_spot = DUMMY_SP;
100-
let mut best_fail_msg = "internal error: ran no matchers".to_string();
100+
let mut best_fail_tok = None;
101101

102102
for (i, lhs) in lhses.iter().enumerate() { // try each arm's matchers
103103
let lhs_tt = match *lhs {
@@ -134,17 +134,18 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
134134
macro_ident: name
135135
})
136136
}
137-
Failure(sp, ref msg) => if sp.lo >= best_fail_spot.lo {
137+
Failure(sp, tok) => if sp.lo >= best_fail_spot.lo {
138138
best_fail_spot = sp;
139-
best_fail_msg = (*msg).clone();
139+
best_fail_tok = Some(tok);
140140
},
141141
Error(err_sp, ref msg) => {
142142
cx.span_fatal(err_sp.substitute_dummy(sp), &msg[..])
143143
}
144144
}
145145
}
146146

147-
cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg[..]);
147+
let best_fail_msg = parse_failure_msg(best_fail_tok.expect("ran no matchers"));
148+
cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg);
148149
}
149150

150151
pub struct MacroRulesExpander;
@@ -222,8 +223,12 @@ pub fn compile(sess: &ParseSess, def: &ast::MacroDef) -> SyntaxExtension {
222223

223224
let argument_map = match parse(sess, &Vec::new(), arg_reader, &argument_gram) {
224225
Success(m) => m,
225-
Failure(sp, str) | Error(sp, str) => {
226-
panic!(sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &str));
226+
Failure(sp, tok) => {
227+
let s = parse_failure_msg(tok);
228+
panic!(sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s));
229+
}
230+
Error(sp, s) => {
231+
panic!(sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s));
227232
}
228233
};
229234

src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
2525
use syntax::ext::build::AstBuilder;
2626
use syntax::ext::tt::macro_parser::{MatchedSeq, MatchedNonterminal};
2727
use syntax::ext::tt::macro_parser::{Success, Failure, Error};
28+
use syntax::ext::tt::macro_parser::parse_failure_msg;
2829
use syntax::ptr::P;
2930
use syntax_pos::Span;
3031
use rustc_plugin::Registry;
@@ -58,8 +59,11 @@ fn expand_mbe_matches(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
5859
_ => unreachable!()
5960
}
6061
}
61-
Failure(_, s) | Error(_, s) => {
62-
panic!("expected Success, but got Error/Failure: {}", s);
62+
Failure(_, tok) => {
63+
panic!("expected Success, but got Failure: {}", parse_failure_msg(tok));
64+
}
65+
Error(_, s) => {
66+
panic!("expected Success, but got Error: {}", s);
6367
}
6468
};
6569

0 commit comments

Comments
 (0)