Reduce string copying

This commit is contained in:
Edwin Cheng 2021-01-05 00:11:56 +08:00
parent 5b86ff3e91
commit f1ffd14922

View file

@ -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<Vec<tt::TokenTree>> {
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<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 @@ impl<'a> TtTreeSink<'a> {
}
}
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 @@ 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());