Skip to content

Commit 824b307

Browse files
committed
avoid the pprust infrastructure in macro expansion
This changes macro expansion to format the path of a macro directly instead of usng the pprust infrastructure. The pprust infrastructure tries to perform line-breaking in a slow fashion, which is undesired when formatting the path of a macro. This should to speed up expansion by a fair amount (I saw 20% on a profiler on `rustc_mir`, and 50% of the time marked as "expansion" in the profiler/time-passes is actually spent loading dependencies).
1 parent aca22a8 commit 824b307

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use std_inject;
2929
use symbol::Symbol;
3030
use symbol::keywords;
3131
use syntax_pos::{Span, DUMMY_SP};
32+
use syntax_pos::hygiene::ExpnFormat;
3233
use tokenstream::{TokenStream, TokenTree};
3334
use util::small_vector::SmallVector;
3435
use visit::Visitor;
@@ -151,6 +152,26 @@ impl ExpansionKind {
151152
}
152153
}
153154

155+
fn macro_bang_format(path: &ast::Path) -> ExpnFormat {
156+
// We don't want to format a path using pretty-printing,
157+
// `format!("{}", path)`, because that tries to insert
158+
// line-breaks and is slow.
159+
let mut path_str = String::with_capacity(64);
160+
for (i, segment) in path.segments.iter().enumerate() {
161+
if i != 0 {
162+
path_str.push_str("::");
163+
}
164+
165+
if segment.identifier.name != keywords::CrateRoot.name() &&
166+
segment.identifier.name != keywords::DollarCrate.name()
167+
{
168+
path_str.push_str(&segment.identifier.name.as_str())
169+
}
170+
}
171+
172+
MacroBang(Symbol::intern(&path_str))
173+
}
174+
154175
pub struct Invocation {
155176
pub kind: InvocationKind,
156177
expansion_kind: ExpansionKind,
@@ -517,7 +538,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
517538
mark.set_expn_info(ExpnInfo {
518539
call_site: span,
519540
callee: NameAndSpan {
520-
format: MacroBang(Symbol::intern(&format!("{}", path))),
541+
format: macro_bang_format(path),
521542
span: def_site_span,
522543
allow_internal_unstable,
523544
allow_internal_unsafe,
@@ -564,7 +585,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
564585
invoc.expansion_data.mark.set_expn_info(ExpnInfo {
565586
call_site: span,
566587
callee: NameAndSpan {
567-
format: MacroBang(Symbol::intern(&format!("{}", path))),
588+
format: macro_bang_format(path),
568589
span: tt_span,
569590
allow_internal_unstable,
570591
allow_internal_unsafe: false,
@@ -600,7 +621,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
600621
invoc.expansion_data.mark.set_expn_info(ExpnInfo {
601622
call_site: span,
602623
callee: NameAndSpan {
603-
format: MacroBang(Symbol::intern(&format!("{}", path))),
624+
format: macro_bang_format(path),
604625
// FIXME procedural macros do not have proper span info
605626
// yet, when they do, we should use it here.
606627
span: None,

0 commit comments

Comments
 (0)