]> git.lizzy.rs Git - rust.git/commitdiff
Avoid using pretty printer if possible
authortopecongiro <seuchida@gmail.com>
Sun, 18 Feb 2018 09:18:07 +0000 (18:18 +0900)
committertopecongiro <seuchida@gmail.com>
Mon, 19 Feb 2018 03:52:40 +0000 (12:52 +0900)
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.

rustfmt-core/src/macros.rs
rustfmt-core/src/types.rs

index a8a8c28c585b28f3b28fbce04a2b939a053f3c6c..2ddac12bce0f1eca382c676f0755c12fea67eb7a 100644 (file)
@@ -114,6 +114,20 @@ macro_rules! parse_macro_arg {
     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
index f6f80ba312be08ce3e8fac43522f3013263aff4f..971717ac253cf60fe6e84bdbe3c03938006a554f 100644 (file)
@@ -14,7 +14,6 @@
 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 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 
 impl Rewrite for ast::Lifetime {
     fn rewrite(&self, _: &RewriteContext, _: Shape) -> Option<String> {
-        Some(pprust::lifetime_to_string(self))
+        Some(self.ident.to_string())
     }
 }