]> git.lizzy.rs Git - rust.git/commitdiff
Reduce string copying
authorEdwin Cheng <edwin0cheng@gmail.com>
Mon, 4 Jan 2021 16:11:56 +0000 (00:11 +0800)
committerEdwin Cheng <edwin0cheng@gmail.com>
Mon, 4 Jan 2021 16:11:56 +0000 (00:11 +0800)
crates/mbe/src/syntax_bridge.rs

index 265c0d63d85c6dbd67b13fc240c4142f94567468..423a4934e82d08028db703d1d3a98db96975421e 100644 (file)
@@ -414,7 +414,7 @@ macro_rules! make_leaf {
     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 @@ fn to_text(&self) -> SmolStr {
 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<Vec<tt::TokenTree>> {
         convert_doc_comment(&doc_comment(token.1))
@@ -442,11 +442,11 @@ fn bump(&mut self) -> Option<(Self::Token, TextRange)> {
         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<Self::Token> {
-        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 @@ fn finish(self) -> (Parse<SyntaxNode>, TokenMap) {
     }
 }
 
-fn delim_to_str(d: Option<tt::DelimiterKind>, closing: bool) -> SmolStr {
+fn delim_to_str(d: Option<tt::DelimiterKind>, 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 @@ fn token(&mut self, kind: SyntaxKind, mut n_tokens: u8) {
 
         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 @@ fn token(&mut self, kind: SyntaxKind, mut n_tokens: u8) {
                 }
             };
             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());