From 48b946bde16a6f7b89064005f70ea475960e0521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Thu, 8 Apr 2021 17:20:14 +0300 Subject: Reduce allocations in Expand macro --- crates/ide/src/expand_macro.rs | 43 +++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index d5628e3df..be0ee03bf 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs @@ -1,3 +1,5 @@ +use std::iter; + use hir::Semantics; use ide_db::RootDatabase; use syntax::{ @@ -91,27 +93,42 @@ fn insert_whitespaces(syn: SyntaxNode) -> String { let is_last = |f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) }; - res += &match token.kind() { - k if is_text(k) && is_next(|it| !it.is_punct(), true) => token.text().to_string() + " ", + match token.kind() { + k if is_text(k) && is_next(|it| !it.is_punct(), true) => { + res.push_str(token.text()); + res.push(' '); + } L_CURLY if is_next(|it| it != R_CURLY, true) => { indent += 1; - let leading_space = if is_last(is_text, false) { " " } else { "" }; - format!("{}{{\n{}", leading_space, " ".repeat(indent)) + if is_last(is_text, false) { + res.push(' '); + } + res.push_str("{\n"); + res.extend(iter::repeat(" ").take(2 * indent)); } R_CURLY if is_last(|it| it != L_CURLY, true) => { indent = indent.saturating_sub(1); - format!("\n{}}}", " ".repeat(indent)) + res.push('\n'); + res.extend(iter::repeat(" ").take(2 * indent)); + res.push_str("}"); + } + R_CURLY => { + res.push_str("}\n"); + res.extend(iter::repeat(" ").take(2 * indent)); } - R_CURLY => format!("}}\n{}", " ".repeat(indent)), LIFETIME_IDENT if is_next(|it| it == IDENT, true) => { - format!("{} ", token.text().to_string()) + res.push_str(token.text()); + res.push(' '); } - T![;] => format!(";\n{}", " ".repeat(indent)), - T![->] => " -> ".to_string(), - T![=] => " = ".to_string(), - T![=>] => " => ".to_string(), - _ => token.text().to_string(), - }; + T![;] => { + res.push_str(";\n"); + res.extend(iter::repeat(" ").take(2 * indent)); + } + T![->] => res.push_str(" -> "), + T![=] => res.push_str(" = "), + T![=>] => res.push_str(" => "), + _ => res.push_str(token.text()), + } last = Some(token.kind()); } -- cgit v1.2.3