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

Remove a lot of bounds checks in BinDecoder by tracking position with a second slice #1399

Merged
merged 2 commits into from Mar 11, 2021

Conversation

saethlin
Copy link
Contributor

@saethlin saethlin commented Mar 5, 2021

This branch is now awkwardly-named. It used to be a lot of changes, but I'm trickling them in one PR at a time. I don't mind the name, but I'm happy to replace the PR if y'all care.

The representation of BinDecoder incurred an extra bounds check on every operation, which was guaranteed to succeed. Using two slices (one is the original so that it can backtrack) to represent the state removes one bounds check on every access, and this also adds an assert to read_u32 and read_i32 to reduce the number of bounds checks in those.

This change to the representation of BinDecoder produces a ~19% improvement in the message-parsing code. This is not so much because the bounds checks were actually that much overhead; removing the checks shrinks the code size of many BinDecoder methods enough that LLVM decides to inline them where it didn't before.

@codecov
Copy link

codecov bot commented Mar 5, 2021

Codecov Report

Merging #1399 (fdf2420) into main (eac59e7) will increase coverage by 0.00%.
The diff coverage is 95.24%.

@@           Coverage Diff           @@
##             main    #1399   +/-   ##
=======================================
  Coverage   85.16%   85.16%           
=======================================
  Files         153      153           
  Lines       15038    15035    -3     
=======================================
- Hits        12806    12804    -2     
+ Misses       2232     2231    -1     

@saethlin saethlin force-pushed the shrink-errors branch 3 times, most recently from a1f94c2 to 1ebccb9 Compare March 10, 2021 04:27
Copy link
Collaborator

@djc djc left a comment

Choose a reason for hiding this comment

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

Okay, here's some preliminary questions/remarks, but I'd like to see this split into multiple commits (or PRs) that each make one logical change (separate Error type, change BinDecoder representation). Let's defer the addition of inline/cold attributes to the end and focus more on the functional changes first?

One other question I have is how much the quality of the error messages regresses once we no longer have the actual data in decode errors.

crates/proto/src/serialize/binary/decoder.rs Outdated Show resolved Hide resolved
crates/proto/src/serialize/binary/decoder.rs Outdated Show resolved Hide resolved
crates/proto/src/serialize/binary/decoder.rs Show resolved Hide resolved
crates/proto/src/error.rs Outdated Show resolved Hide resolved
@saethlin
Copy link
Contributor Author

@djc Really appreciate the feedback. I'm going to reduce this to just the first commit and send the other changes in at least 2 other PRs.

@saethlin saethlin marked this pull request as ready for review March 11, 2021 02:04
@saethlin saethlin changed the title Reduce the size of errors and other micro-optimizations in BinDecoder Remove a lot of bounds checks in BinDecoder by tracking position with a second slice Mar 11, 2021
Copy link
Collaborator

@djc djc left a comment

Choose a reason for hiding this comment

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

Mostly looks good!

crates/proto/src/serialize/binary/decoder.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@djc djc left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

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.

None yet

2 participants