aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2021-01-04 16:11:56 +0000
committerEdwin Cheng <[email protected]>2021-01-04 16:11:56 +0000
commitf1ffd14922d2dc885ba6441ca8380f7d4cb75269 (patch)
tree6beb6f77893e4eed93e06cff6dfeb231e6dfe3b4
parent5b86ff3e91838e58397ec39502d85056e46fcfcb (diff)
Reduce string copying
-rw-r--r--crates/mbe/src/syntax_bridge.rs30
1 files changed, 16 insertions, 14 deletions
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 {
414 fn id_alloc(&mut self) -> &mut TokenIdAlloc; 414 fn id_alloc(&mut self) -> &mut TokenIdAlloc;
415} 415}
416 416
417impl<'a> SrcToken for (RawToken, &'a str) { 417impl<'a> SrcToken for (&'a RawToken, &'a str) {
418 fn kind(&self) -> SyntaxKind { 418 fn kind(&self) -> SyntaxKind {
419 self.0.kind 419 self.0.kind
420 } 420 }
@@ -431,7 +431,7 @@ impl<'a> SrcToken for (RawToken, &'a str) {
431impl RawConvertor<'_> {} 431impl RawConvertor<'_> {}
432 432
433impl<'a> TokenConvertor for RawConvertor<'a> { 433impl<'a> TokenConvertor for RawConvertor<'a> {
434 type Token = (RawToken, &'a str); 434 type Token = (&'a RawToken, &'a str);
435 435
436 fn convert_doc_comment(&self, token: &Self::Token) -> Option<Vec<tt::TokenTree>> { 436 fn convert_doc_comment(&self, token: &Self::Token) -> Option<Vec<tt::TokenTree>> {
437 convert_doc_comment(&doc_comment(token.1)) 437 convert_doc_comment(&doc_comment(token.1))
@@ -442,11 +442,11 @@ impl<'a> TokenConvertor for RawConvertor<'a> {
442 let range = TextRange::at(self.offset, token.len); 442 let range = TextRange::at(self.offset, token.len);
443 self.offset += token.len; 443 self.offset += token.len;
444 444
445 Some(((*token, &self.text[range]), range)) 445 Some(((token, &self.text[range]), range))
446 } 446 }
447 447
448 fn peek(&self) -> Option<Self::Token> { 448 fn peek(&self) -> Option<Self::Token> {
449 let token = self.inner.as_slice().get(0).cloned(); 449 let token = self.inner.as_slice().get(0);
450 450
451 token.map(|it| { 451 token.map(|it| {
452 let range = TextRange::at(self.offset, it.len); 452 let range = TextRange::at(self.offset, it.len);
@@ -601,17 +601,16 @@ impl<'a> TtTreeSink<'a> {
601 } 601 }
602} 602}
603 603
604fn delim_to_str(d: Option<tt::DelimiterKind>, closing: bool) -> SmolStr { 604fn delim_to_str(d: Option<tt::DelimiterKind>, closing: bool) -> &'static str {
605 let texts = match d { 605 let texts = match d {
606 Some(tt::DelimiterKind::Parenthesis) => "()", 606 Some(tt::DelimiterKind::Parenthesis) => "()",
607 Some(tt::DelimiterKind::Brace) => "{}", 607 Some(tt::DelimiterKind::Brace) => "{}",
608 Some(tt::DelimiterKind::Bracket) => "[]", 608 Some(tt::DelimiterKind::Bracket) => "[]",
609 None => return "".into(), 609 None => return "",
610 }; 610 };
611 611
612 let idx = closing as usize; 612 let idx = closing as usize;
613 let text = &texts[idx..texts.len() - (1 - idx)]; 613 &texts[idx..texts.len() - (1 - idx)]
614 text.into()
615} 614}
616 615
617impl<'a> TreeSink for TtTreeSink<'a> { 616impl<'a> TreeSink for TtTreeSink<'a> {
@@ -626,22 +625,25 @@ impl<'a> TreeSink for TtTreeSink<'a> {
626 625
627 let mut last = self.cursor; 626 let mut last = self.cursor;
628 for _ in 0..n_tokens { 627 for _ in 0..n_tokens {
628 let tmp_str: SmolStr;
629 if self.cursor.eof() { 629 if self.cursor.eof() {
630 break; 630 break;
631 } 631 }
632 last = self.cursor; 632 last = self.cursor;
633 let text: SmolStr = match self.cursor.token_tree() { 633 let text: &str = match self.cursor.token_tree() {
634 Some(tt::TokenTree::Leaf(leaf)) => { 634 Some(tt::TokenTree::Leaf(leaf)) => {
635 // Mark the range if needed 635 // Mark the range if needed
636 let (text, id) = match leaf { 636 let (text, id) = match leaf {
637 tt::Leaf::Ident(ident) => (ident.text.clone(), ident.id), 637 tt::Leaf::Ident(ident) => (&ident.text, ident.id),
638 tt::Leaf::Punct(punct) => { 638 tt::Leaf::Punct(punct) => {
639 assert!(punct.char.is_ascii()); 639 assert!(punct.char.is_ascii());
640 let char = &(punct.char as u8); 640 let char = &(punct.char as u8);
641 let text = std::str::from_utf8(std::slice::from_ref(char)).unwrap(); 641 tmp_str = SmolStr::new_inline(
642 (SmolStr::new_inline(text), punct.id) 642 std::str::from_utf8(std::slice::from_ref(char)).unwrap(),
643 );
644 (&tmp_str, punct.id)
643 } 645 }
644 tt::Leaf::Literal(lit) => (lit.text.clone(), lit.id), 646 tt::Leaf::Literal(lit) => (&lit.text, lit.id),
645 }; 647 };
646 let range = TextRange::at(self.text_pos, TextSize::of(text.as_str())); 648 let range = TextRange::at(self.text_pos, TextSize::of(text.as_str()));
647 self.token_map.insert(id, range); 649 self.token_map.insert(id, range);
@@ -672,7 +674,7 @@ impl<'a> TreeSink for TtTreeSink<'a> {
672 } 674 }
673 }; 675 };
674 self.buf += &text; 676 self.buf += &text;
675 self.text_pos += TextSize::of(text.as_str()); 677 self.text_pos += TextSize::of(text);
676 } 678 }
677 679
678 let text = SmolStr::new(self.buf.as_str()); 680 let text = SmolStr::new(self.buf.as_str());