Conversation
newpavlov
commented
Jan 27, 2021
|
|
||
| let flag = src_rem.len() == 1; | ||
| let mask = (flag as u8).wrapping_sub(1); | ||
| dst_rem[2] = (dst_rem[2] & mask) | (PAD & !mask); |
Member
Author
There was a problem hiding this comment.
Also note that I have replaced the explicit branch with this bit-twiddling. I wonder if there is a better way to generate mask from bool.
newpavlov
commented
Jan 27, 2021
| #[inline(always)] | ||
| fn encode_string(input: &[u8], padded: bool, hi_bytes: (u8, u8)) -> String { | ||
| let elen = encoded_len(input, padded); | ||
| let elen = encoded_len_inner(input.len(), padded).expect("input is too big"); |
Member
Author
There was a problem hiding this comment.
Thanks to the forced inlining, the second expect used on encode result will be properly removed. This change allows this function to panic before allocation, which simplifies generated assembly a bit.
tarcieri
reviewed
Jan 27, 2021
| } else { | ||
| encode_3bytes_padded(s, d, hi_bytes); | ||
| } | ||
| if let Some(dst_rem) = dst_chunks.next() { |
Member
There was a problem hiding this comment.
Oh neat, I guess that was all I was missing.
tarcieri
approved these changes
Jan 27, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improves the
encodefunction by improving code reuse and removing an unreachable panic in the padded path. Also marks all inner functions with#[inline(always)]to remove call indirection. Most often crates will use only one module, so I think such inlining is warranted.