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

Conversation

davisp
Copy link
Member

@davisp davisp commented Dec 11, 2024

Its a micro optimization but seemed to give a bit of a boost to only search words starting with the correct letter.

An alternative to #1590 that doesn't involve adding a new dependency. This just relies on having a pre-calculated table of word ranges to binary search.

Relatively speaking, this approach gets about slightly less than half the speed up of using phf.

Its a micro optimization but seemed to give a bit of a boost to only
search words starting with the correct letter.
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank you (again) @davisp -- I think this is a promising approach.

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 🤔

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

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.

@alamb alamb marked this pull request as draft December 24, 2024 15:00
@alamb
Copy link
Contributor

alamb commented Dec 24, 2024

Marking as draft as I think this PR is no longer waiting on feedback. Please mark it as ready for review when it is ready for another look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants