1
Fork 0

Inline a hot closure in from_lit_token.

The change looks big because `rustfmt` rearranges things, but the only
real change is the inlining annotation.
This commit is contained in:
Nicholas Nethercote 2022-02-24 16:49:37 +11:00
parent 37d9ea745b
commit 44308dc348
2 changed files with 23 additions and 17 deletions

View file

@ -16,6 +16,7 @@
#![feature(min_specialization)] #![feature(min_specialization)]
#![recursion_limit = "256"] #![recursion_limit = "256"]
#![feature(slice_internals)] #![feature(slice_internals)]
#![feature(stmt_expr_attributes)]
#[macro_use] #[macro_use]
extern crate rustc_macros; extern crate rustc_macros;

View file

@ -56,25 +56,30 @@ impl LitKind {
// new symbol because the string in the LitKind is different to the // new symbol because the string in the LitKind is different to the
// string in the token. // string in the token.
let s = symbol.as_str(); let s = symbol.as_str();
let symbol = let symbol = if s.contains(&['\\', '\r']) {
if s.contains(&['\\', '\r']) { let mut buf = String::with_capacity(s.len());
let mut buf = String::with_capacity(s.len()); let mut error = Ok(());
let mut error = Ok(()); // Force-inlining here is aggressive but the closure is
unescape_literal(&s, Mode::Str, &mut |_, unescaped_char| { // called on every char in the string, so it can be
match unescaped_char { // hot in programs with many long strings.
Ok(c) => buf.push(c), unescape_literal(
Err(err) => { &s,
if err.is_fatal() { Mode::Str,
error = Err(LitError::LexerError); &mut #[inline(always)]
} |_, unescaped_char| match unescaped_char {
Ok(c) => buf.push(c),
Err(err) => {
if err.is_fatal() {
error = Err(LitError::LexerError);
} }
} }
}); },
error?; );
Symbol::intern(&buf) error?;
} else { Symbol::intern(&buf)
symbol } else {
}; symbol
};
LitKind::Str(symbol, ast::StrStyle::Cooked) LitKind::Str(symbol, ast::StrStyle::Cooked)
} }
token::StrRaw(n) => { token::StrRaw(n) => {