Skip to content

Commit f8d874b

Browse files
andygroveclaude
authored andcommitted
perf: optimize make_word() to avoid unnecessary allocations (apache#2176)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a71449e commit f8d874b

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

src/tokenizer.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,22 @@ impl Token {
416416
/// When `quote_style` is `None`, the parser attempts a case-insensitive keyword
417417
/// lookup and sets the `Word::keyword` accordingly.
418418
pub fn make_word(word: &str, quote_style: Option<char>) -> Self {
419-
let word_uppercase = word.to_uppercase();
419+
// Only perform keyword lookup for unquoted identifiers.
420+
// Use to_ascii_uppercase() since SQL keywords are ASCII,
421+
// avoiding Unicode case conversion overhead.
422+
let keyword = if quote_style.is_none() {
423+
let word_uppercase = word.to_ascii_uppercase();
424+
ALL_KEYWORDS
425+
.binary_search(&word_uppercase.as_str())
426+
.map_or(Keyword::NoKeyword, |x| ALL_KEYWORDS_INDEX[x])
427+
} else {
428+
Keyword::NoKeyword
429+
};
430+
420431
Token::Word(Word {
421432
value: word.to_string(),
422433
quote_style,
423-
keyword: if quote_style.is_none() {
424-
let keyword = ALL_KEYWORDS.binary_search(&word_uppercase.as_str());
425-
keyword.map_or(Keyword::NoKeyword, |x| ALL_KEYWORDS_INDEX[x])
426-
} else {
427-
Keyword::NoKeyword
428-
},
434+
keyword,
429435
})
430436
}
431437
}

0 commit comments

Comments
 (0)