From f1ffd14922d2dc885ba6441ca8380f7d4cb75269 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 5 Jan 2021 00:11:56 +0800 Subject: Reduce string copying --- crates/mbe/src/syntax_bridge.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'crates/mbe') diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index 265c0d63d..423a4934e 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -414,7 +414,7 @@ trait TokenConvertor { fn id_alloc(&mut self) -> &mut TokenIdAlloc; } -impl<'a> SrcToken for (RawToken, &'a str) { +impl<'a> SrcToken for (&'a RawToken, &'a str) { fn kind(&self) -> SyntaxKind { self.0.kind } @@ -431,7 +431,7 @@ impl<'a> SrcToken for (RawToken, &'a str) { impl RawConvertor<'_> {} impl<'a> TokenConvertor for RawConvertor<'a> { - type Token = (RawToken, &'a str); + type Token = (&'a RawToken, &'a str); fn convert_doc_comment(&self, token: &Self::Token) -> Option> { convert_doc_comment(&doc_comment(token.1)) @@ -442,11 +442,11 @@ impl<'a> TokenConvertor for RawConvertor<'a> { let range = TextRange::at(self.offset, token.len); self.offset += token.len; - Some(((*token, &self.text[range]), range)) + Some(((token, &self.text[range]), range)) } fn peek(&self) -> Option { - let token = self.inner.as_slice().get(0).cloned(); + let token = self.inner.as_slice().get(0); token.map(|it| { let range = TextRange::at(self.offset, it.len); @@ -601,17 +601,16 @@ impl<'a> TtTreeSink<'a> { } } -fn delim_to_str(d: Option, closing: bool) -> SmolStr { +fn delim_to_str(d: Option, closing: bool) -> &'static str { let texts = match d { Some(tt::DelimiterKind::Parenthesis) => "()", Some(tt::DelimiterKind::Brace) => "{}", Some(tt::DelimiterKind::Bracket) => "[]", - None => return "".into(), + None => return "", }; let idx = closing as usize; - let text = &texts[idx..texts.len() - (1 - idx)]; - text.into() + &texts[idx..texts.len() - (1 - idx)] } impl<'a> TreeSink for TtTreeSink<'a> { @@ -626,22 +625,25 @@ impl<'a> TreeSink for TtTreeSink<'a> { let mut last = self.cursor; for _ in 0..n_tokens { + let tmp_str: SmolStr; if self.cursor.eof() { break; } last = self.cursor; - let text: SmolStr = match self.cursor.token_tree() { + let text: &str = match self.cursor.token_tree() { Some(tt::TokenTree::Leaf(leaf)) => { // Mark the range if needed let (text, id) = match leaf { - tt::Leaf::Ident(ident) => (ident.text.clone(), ident.id), + tt::Leaf::Ident(ident) => (&ident.text, ident.id), tt::Leaf::Punct(punct) => { assert!(punct.char.is_ascii()); let char = &(punct.char as u8); - let text = std::str::from_utf8(std::slice::from_ref(char)).unwrap(); - (SmolStr::new_inline(text), punct.id) + tmp_str = SmolStr::new_inline( + std::str::from_utf8(std::slice::from_ref(char)).unwrap(), + ); + (&tmp_str, punct.id) } - tt::Leaf::Literal(lit) => (lit.text.clone(), lit.id), + tt::Leaf::Literal(lit) => (&lit.text, lit.id), }; let range = TextRange::at(self.text_pos, TextSize::of(text.as_str())); self.token_map.insert(id, range); @@ -672,7 +674,7 @@ impl<'a> TreeSink for TtTreeSink<'a> { } }; self.buf += &text; - self.text_pos += TextSize::of(text.as_str()); + self.text_pos += TextSize::of(text); } let text = SmolStr::new(self.buf.as_str()); -- cgit v1.2.3