Skip to content

base64ct: minor code improvements#234

Merged
newpavlov merged 2 commits intomasterfrom
base64_impr
Jan 27, 2021
Merged

base64ct: minor code improvements#234
newpavlov merged 2 commits intomasterfrom
base64_impr

Conversation

@newpavlov
Copy link
Member

@newpavlov newpavlov commented Jan 27, 2021

Improves the encode function 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.

@newpavlov newpavlov requested a review from tarcieri January 27, 2021 09:10

let flag = src_rem.len() == 1;
let mask = (flag as u8).wrapping_sub(1);
dst_rem[2] = (dst_rem[2] & mask) | (PAD & !mask);
Copy link
Member Author

Choose a reason for hiding this comment

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

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.

#[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");
Copy link
Member Author

@newpavlov newpavlov Jan 27, 2021

Choose a reason for hiding this comment

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

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.

} else {
encode_3bytes_padded(s, d, hi_bytes);
}
if let Some(dst_rem) = dst_chunks.next() {
Copy link
Member

Choose a reason for hiding this comment

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

Oh neat, I guess that was all I was missing.

@newpavlov newpavlov merged commit 5eb3e6b into master Jan 27, 2021
@newpavlov newpavlov deleted the base64_impr branch January 27, 2021 14:53
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