-
Notifications
You must be signed in to change notification settings - Fork 129
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
Conversation
r? @tiaanl |
daaa60d
to
64a2d76
Compare
@emilio Did you measure the performance of this change somewhere? |
Built a benchmark for this because I had some time. Before:
After:
|
There was a problem hiding this 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] = { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To 256
here too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed.
2fc1fb4
to
adf08ea
Compare
Updated, thanks for the review @tiaanl! |
It's faster.