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

Compile DFAs as lookup tables instead of linear if-elses #6

Closed
osa1 opened this issue Jun 1, 2021 · 2 comments
Closed

Compile DFAs as lookup tables instead of linear if-elses #6

osa1 opened this issue Jun 1, 2021 · 2 comments
Labels
perf Related to runtime performance

Comments

@osa1
Copy link
Owner

osa1 commented Jun 1, 2021

It's not uncommon for a lexer state to be compiled to dozens of states. Currently we generate code like this:

match self.state {
    0 -> ...,
    1 -> ...,
    ...
    _ -> ...,
}

For N states this means N-1 comparisons in the worst case to the take last branch.

Instead we should compile each state to a function, then put them all in an array:

static STATES: [fn(...) -> ...; N] = [...];

Then just do STATES[self.state](...);.

@osa1 osa1 added the perf Related to runtime performance label Jun 1, 2021
@osa1
Copy link
Owner Author

osa1 commented Jun 4, 2021

I implemented this in state_functions but cargo bench reports 5% regression in the Lua benchmark, and even worse in a micro benchmark.

@osa1
Copy link
Owner Author

osa1 commented Jun 17, 2021

I implemented this in state_functions but cargo bench reports 5% regression in the Lua benchmark, and even worse in a micro benchmark.

This is because the current code is compiled to a jump table. Closing.

@osa1 osa1 closed this as completed Jun 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf Related to runtime performance
Projects
None yet
Development

No branches or pull requests

1 participant