From bd33b4972da88f55dbeaca8be43350e35021d2ea Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Sat, 19 Oct 2024 13:07:15 +0200 Subject: [PATCH] Short circuit `lex_identifier` if the name is longer or shorter than any known keyword (#13815) --- crates/ruff_python_parser/src/lexer.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/ruff_python_parser/src/lexer.rs b/crates/ruff_python_parser/src/lexer.rs index d407ab357e..4bc4bb4319 100644 --- a/crates/ruff_python_parser/src/lexer.rs +++ b/crates/ruff_python_parser/src/lexer.rs @@ -648,6 +648,14 @@ impl<'src> Lexer<'src> { return TokenKind::Name; } + // Short circuit for names that are longer than any known keyword. + // It helps Rust to predict that the Name::new call in the keyword match's default branch + // is guaranteed to fit into a stack allocated (inline) Name. + if text.len() > 8 { + self.current_value = TokenValue::Name(Name::new(text)); + return TokenKind::Name; + } + match text { "False" => TokenKind::False, "None" => TokenKind::None,