Skip to content

Commit 5e6168f

Browse files
authored
Rollup merge of rust-lang#37161 - nnethercote:no-cfg-cloning, r=nrc
Avoid many CrateConfig clones. This commit changes `ExtCtx::cfg()` so it returns a `CrateConfig` reference instead of a clone. As a result, it also changes all of the `cfg()` callsites to explicitly clone... except one, because the commit also changes `macro_parser::parse()` to take `&CrateConfig`. This is good, because that function can be hot, and `CrateConfig` is expensive to clone. This change almost halves the number of heap allocations done by rustc for `html5ever` in rustc-benchmarks suite, which makes compilation 1.20x faster. r? @nrc
2 parents ea75c75 + 029dcee commit 5e6168f

File tree

8 files changed

+18
-14
lines changed

8 files changed

+18
-14
lines changed

src/libsyntax/ext/base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,11 @@ impl<'a> ExtCtxt<'a> {
617617

618618
pub fn new_parser_from_tts(&self, tts: &[tokenstream::TokenTree])
619619
-> parser::Parser<'a> {
620-
parse::tts_to_parser(self.parse_sess, tts.to_vec(), self.cfg())
620+
parse::tts_to_parser(self.parse_sess, tts.to_vec(), self.cfg().clone())
621621
}
622622
pub fn codemap(&self) -> &'a CodeMap { self.parse_sess.codemap() }
623623
pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess }
624-
pub fn cfg(&self) -> ast::CrateConfig { self.cfg.clone() }
624+
pub fn cfg(&self) -> &ast::CrateConfig { &self.cfg }
625625
pub fn call_site(&self) -> Span {
626626
self.codemap().with_expn_info(self.backtrace(), |ei| match ei {
627627
Some(expn_info) => expn_info.call_site,

src/libsyntax/ext/quote.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -331,31 +331,31 @@ pub mod rt {
331331
panictry!(parse::parse_item_from_source_str(
332332
"<quote expansion>".to_string(),
333333
s,
334-
self.cfg(),
334+
self.cfg().clone(),
335335
self.parse_sess())).expect("parse error")
336336
}
337337

338338
fn parse_stmt(&self, s: String) -> ast::Stmt {
339339
panictry!(parse::parse_stmt_from_source_str(
340340
"<quote expansion>".to_string(),
341341
s,
342-
self.cfg(),
342+
self.cfg().clone(),
343343
self.parse_sess())).expect("parse error")
344344
}
345345

346346
fn parse_expr(&self, s: String) -> P<ast::Expr> {
347347
panictry!(parse::parse_expr_from_source_str(
348348
"<quote expansion>".to_string(),
349349
s,
350-
self.cfg(),
350+
self.cfg().clone(),
351351
self.parse_sess()))
352352
}
353353

354354
fn parse_tts(&self, s: String) -> Vec<TokenTree> {
355355
panictry!(parse::parse_tts_from_source_str(
356356
"<quote expansion>".to_string(),
357357
s,
358-
self.cfg(),
358+
self.cfg().clone(),
359359
self.parse_sess()))
360360
}
361361
}
@@ -924,14 +924,18 @@ fn expand_parse_call(cx: &ExtCtxt,
924924
sp, cx.expr_ident(sp, id_ext("ext_cx")),
925925
id_ext("cfg"), Vec::new());
926926

927+
let cfg_clone_call = || cx.expr_method_call(
928+
sp, cfg_call(),
929+
id_ext("clone"), Vec::new());
930+
927931
let parse_sess_call = || cx.expr_method_call(
928932
sp, cx.expr_ident(sp, id_ext("ext_cx")),
929933
id_ext("parse_sess"), Vec::new());
930934

931935
let new_parser_call =
932936
cx.expr_call(sp,
933937
cx.expr_ident(sp, id_ext("new_parser_from_tts")),
934-
vec!(parse_sess_call(), cfg_call(), tts_expr));
938+
vec!(parse_sess_call(), cfg_clone_call(), tts_expr));
935939

936940
let path = vec![id_ext("syntax"), id_ext("ext"), id_ext("quote"), id_ext(parse_method)];
937941
let mut args = vec![cx.expr_mut_addr_of(sp, new_parser_call)];

src/libsyntax/ext/source_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[tokenstream::T
9494
// The file will be added to the code map by the parser
9595
let p =
9696
parse::new_sub_parser_from_file(cx.parse_sess(),
97-
cx.cfg(),
97+
cx.cfg().clone(),
9898
&res_rel_file(cx,
9999
sp,
100100
Path::new(&file)),

src/libsyntax/ext/tt/macro_parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ pub fn token_name_eq(t1 : &Token, t2 : &Token) -> bool {
272272
}
273273

274274
pub fn parse(sess: &ParseSess,
275-
cfg: ast::CrateConfig,
275+
cfg: &ast::CrateConfig,
276276
mut rdr: TtReader,
277277
ms: &[TokenTree])
278278
-> NamedParseResult {

src/libsyntax/ext/tt/macro_rules.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
120120
Some(named_matches),
121121
imported_from,
122122
rhs);
123-
let mut p = Parser::new(cx.parse_sess(), cx.cfg(), Box::new(trncbr));
123+
let mut p = Parser::new(cx.parse_sess(), cx.cfg().clone(), Box::new(trncbr));
124124
p.directory = cx.current_expansion.module.directory.clone();
125125
p.restrictions = match cx.current_expansion.no_noninline_mod {
126126
true => Restrictions::NO_NONINLINE_MOD,
@@ -225,7 +225,7 @@ pub fn compile(sess: &ParseSess, def: &ast::MacroDef) -> SyntaxExtension {
225225
// Parse the macro_rules! invocation (`none` is for no interpolations):
226226
let arg_reader = new_tt_reader(&sess.span_diagnostic, None, None, def.body.clone());
227227

228-
let argument_map = match parse(sess, Vec::new(), arg_reader, &argument_gram) {
228+
let argument_map = match parse(sess, &Vec::new(), arg_reader, &argument_gram) {
229229
Success(m) => m,
230230
Failure(sp, str) | Error(sp, str) => {
231231
panic!(sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &str));

src/libsyntax_ext/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt,
107107
if p2.token != token::Eof {
108108
let mut extra_tts = panictry!(p2.parse_all_token_trees());
109109
extra_tts.extend(tts[first_colon..].iter().cloned());
110-
p = parse::tts_to_parser(cx.parse_sess, extra_tts, cx.cfg());
110+
p = parse::tts_to_parser(cx.parse_sess, extra_tts, cx.cfg().clone());
111111
}
112112

113113
asm = s;

src/test/compile-fail-fulldeps/auxiliary/macro_crate_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
5757
-> Box<MacResult+'static> {
5858
// Parse an expression and emit it unchanged.
5959
let mut parser = parse::new_parser_from_tts(cx.parse_sess(),
60-
cx.cfg(), tts.to_vec());
60+
cx.cfg().clone(), tts.to_vec());
6161
let expr = parser.parse_expr().unwrap();
6262
MacEager::expr(quote_expr!(&mut *cx, $expr))
6363
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResu
6060
// See Issue #15750
6161
fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree]) -> Box<MacResult + 'static> {
6262
// Parse an expression and emit it unchanged.
63-
let mut parser = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_vec());
63+
let mut parser = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg().clone(), tts.to_vec());
6464
let expr = parser.parse_expr().unwrap();
6565
MacEager::expr(quote_expr!(&mut *cx, $expr))
6666
}

0 commit comments

Comments
 (0)