Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly faster keyword lookups #1591

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,3 +973,61 @@ pub const RESERVED_FOR_IDENTIFIER: &[Keyword] = &[
Keyword::STRUCT,
Keyword::TRIM,
];

pub const NA: usize = usize::MAX;

#[rustfmt::skip]
pub const KEYWORD_LOOKUP_INDEX_ROOT: &[usize; 26] = &[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not super maintainable (needs to be updated manually when adding keywords).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree - it we go with this type of table driven approach we should have some sort of update script (or build.rs) that builds the table from the enum.

0, 42, 67, 148, 198, 241, 281, 294, 305, 350, 357, 360, 390,
430, 465, 497, 539, 543, 605, 683, 728, 761, 780, 793, 795, 796,
];

pub fn lookup(word: &str) -> Keyword {
if word.len() < 2 {
return Keyword::NoKeyword;
}

let word = word.to_uppercase();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we can figure out how to remove this to_uppercase call I think this approach will likely be very fast.

I dug around in the rust API docs and it seems like we could change the comparison to ignore ascii case. See comment below 🤔

let byte1 = word.as_bytes()[0];
if !byte1.is_ascii_uppercase() {
return Keyword::NoKeyword;
}

let start = KEYWORD_LOOKUP_INDEX_ROOT[(byte1 - b'A') as usize];

let end = if (byte1 + 1) <= b'Z' {
KEYWORD_LOOKUP_INDEX_ROOT[(byte1 - b'A' + 1) as usize]
} else {
ALL_KEYWORDS.len()
};

let keyword = ALL_KEYWORDS[start..end].binary_search(&word.as_str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could use something like

Suggested change
let keyword = ALL_KEYWORDS[start..end].binary_search(&word.as_str());
let keyword = ALL_KEYWORDS[start..end].binary_search_by(|s| s.eq_ignore_ascii_case(&word))

https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search_by
https://doc.rust-lang.org/std/primitive.str.html#method.eq_ignore_ascii_case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually tried to do this approach, and I found the to_uppercase call is being done as part of the token creation. So to save the allocation we need to to avoid

https://github.com/alamb/sqlparser-rs/blob/4ab3ab91473d152c652e6582b63abb13535703f9/src/tokenizer.rs#L347-L346

I think we can change it to make_ascii_uppercase perhaps

keyword.map_or(Keyword::NoKeyword, |x| ALL_KEYWORDS_INDEX[x + start])
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn check_keyword_index_roots() {
let mut root_index = Vec::with_capacity(26);
root_index.push(0);
for idx in 1..ALL_KEYWORDS.len() {
assert!(ALL_KEYWORDS[idx - 1] < ALL_KEYWORDS[idx]);
let prev = ALL_KEYWORDS[idx - 1].as_bytes()[0];
let curr = ALL_KEYWORDS[idx].as_bytes()[0];
if curr != prev {
root_index.push(idx);
}
}
assert_eq!(&root_index, KEYWORD_LOOKUP_INDEX_ROOT);
}

#[test]
fn check_keyword_lookup() {
for idx in 0..ALL_KEYWORDS.len() {
assert_eq!(lookup(ALL_KEYWORDS[idx]), ALL_KEYWORDS_INDEX[idx]);
}
}
}
6 changes: 2 additions & 4 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use crate::dialect::{
BigQueryDialect, DuckDbDialect, GenericDialect, MySqlDialect, PostgreSqlDialect,
SnowflakeDialect,
};
use crate::keywords::{Keyword, ALL_KEYWORDS, ALL_KEYWORDS_INDEX};
use crate::keywords::{self, Keyword};

/// SQL Token enumeration
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down Expand Up @@ -344,13 +344,11 @@ impl Token {
}

pub fn make_word(word: &str, quote_style: Option<char>) -> Self {
let word_uppercase = word.to_uppercase();
Token::Word(Word {
value: word.to_string(),
quote_style,
keyword: if quote_style.is_none() {
let keyword = ALL_KEYWORDS.binary_search(&word_uppercase.as_str());
keyword.map_or(Keyword::NoKeyword, |x| ALL_KEYWORDS_INDEX[x])
keywords::lookup(word)
} else {
Keyword::NoKeyword
},
Expand Down
Loading