Avoid using pretty printer if possible

Setting a pretty printer adds noticeable overhead.

We can replace the usage in `ast:Lifetime::rewrite` by simply converting `Ident`
to string.
We can do the same thing for a macro path as long as it is not nested, which
should hold for most cases.
This commit is contained in:
topecongiro 2018-02-18 18:18:07 +09:00
parent d7d9850c42
commit 61c6c591e4
2 changed files with 16 additions and 12 deletions

View File

@ -114,6 +114,20 @@ fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
None
}
/// Rewrite macro name without using pretty-printer if possible.
fn rewrite_macro_name(path: &ast::Path, extra_ident: Option<ast::Ident>) -> String {
let name = if path.segments.len() == 1 {
// Avoid using pretty-printer in the common case.
format!("{}!", path.segments[0].identifier)
} else {
format!("{}!", path)
};
match extra_ident {
Some(ident) if ident != symbol::keywords::Invalid.ident() => format!("{} {}", name, ident),
_ => name,
}
}
pub fn rewrite_macro(
mac: &ast::Mac,
extra_ident: Option<ast::Ident>,
@ -132,16 +146,7 @@ pub fn rewrite_macro(
let original_style = macro_style(mac, context);
let macro_name = match extra_ident {
None => format!("{}!", mac.node.path),
Some(ident) => {
if ident == symbol::keywords::Invalid.ident() {
format!("{}!", mac.node.path)
} else {
format!("{}! {}", mac.node.path, ident)
}
}
};
let macro_name = rewrite_macro_name(&mac.node.path, extra_ident);
let style = if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) {
MacroStyle::Brackets

View File

@ -14,7 +14,6 @@ use std::ops::Deref;
use config::lists::*;
use syntax::ast::{self, FunctionRetTy, Mutability};
use syntax::codemap::{self, BytePos, Span};
use syntax::print::pprust;
use syntax::symbol::keywords;
use codemap::SpanUtils;
@ -539,7 +538,7 @@ impl Rewrite for ast::TyParamBound {
impl Rewrite for ast::Lifetime {
fn rewrite(&self, _: &RewriteContext, _: Shape) -> Option<String> {
Some(pprust::lifetime_to_string(self))
Some(self.ident.to_string())
}
}