Use Cow in Token::String.

`Printer::word` takes a `&str` and converts it into a `String`, which
causes an allocation. But that allocation is rarely necessary, because
`&str` is almost always a `&'static str` or a `String` that won't be
used again.

This commit changes `Token::String` so it holds a `Cow<'static, str>`
instead of a `String`, which avoids a lot of allocations.
This commit is contained in:
Nicholas Nethercote 2018-11-29 11:36:58 +11:00
parent deb9195e57
commit 787959c20d
7 changed files with 101 additions and 88 deletions

View file

@ -2795,7 +2795,7 @@ impl<'a> Parser<'a> {
s.print_usize(float.trunc() as usize)?;
s.pclose()?;
s.s.word(".")?;
s.s.word(fstr.splitn(2, ".").last().unwrap())
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
});
err.span_suggestion_with_applicability(
lo.to(self.prev_span),