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

parser: Use a lookup table for Delimiter::from_byte. #358

Merged
merged 2 commits into from
Sep 11, 2023

Conversation

emilio
Copy link
Member

@emilio emilio commented Sep 6, 2023

It's faster.

@emilio
Copy link
Member Author

emilio commented Sep 6, 2023

r? @tiaanl

@tiaanl
Copy link
Collaborator

tiaanl commented Sep 10, 2023

@emilio Did you measure the performance of this change somewhere?

@emilio
Copy link
Member Author

emilio commented Sep 10, 2023

@tiaanl Our similar match_byte optimization is a huge perf improvement in the tokenizer, see #128. So I didn't measure this function explicitly but I'd expect similar gains.

@emilio
Copy link
Member Author

emilio commented Sep 10, 2023

Built a benchmark for this because I had some time. Before:

test tests::delimiter_from_byte ... bench:     177,361 ns/iter (+/- 7,390)

After:

test tests::delimiter_from_byte ... bench:      46,917 ns/iter (+/- 906)

Copy link
Collaborator

@tiaanl tiaanl left a comment

Choose a reason for hiding this comment

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

I was a bit skeptical about this one. It was either a few conditional jump instructions or a few memory accesses (the buffer doesn't fit fully into a cache line) and I was betting the memory accesses would take longer.

Benchmark numbers look like a good improvement.

src/parser.rs Outdated
@@ -313,16 +313,22 @@ impl Delimiters {
}

#[inline]
fn from_byte(byte: Option<u8>) -> Delimiters {
pub(crate) fn from_byte(byte: Option<u8>) -> Delimiters {
const TABLE: [Delimiters; 255] = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You want [Delimiters; 256] here. Leaving off the last option will generate code that checks for bounds and panics.

Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops, thanks!

src/tests.rs Outdated
fn delimiter_from_byte(b: &mut Bencher) {
b.iter(|| {
for _ in 0..1000 {
for i in 0..255 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

To 256 here too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed.

@emilio
Copy link
Member Author

emilio commented Sep 11, 2023

Updated, thanks for the review @tiaanl!

@emilio emilio added this pull request to the merge queue Sep 11, 2023
Merged via the queue into master with commit 1ccc577 Sep 11, 2023
11 checks passed
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.

2 participants