Skip to content

Commit

Permalink
refactor: simplify encoded_varint64_len
Browse files Browse the repository at this point in the history
This greatly cleans up the assembly as well, but this function is not
as likely to matter from a perf perspective.
  • Loading branch information
morrisonlevi committed Feb 2, 2024
1 parent ed42c9e commit c6f18ef
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions protobuf/src/varint/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,10 @@ pub(crate) fn encode_varint32(value: u32, buf: &mut [MaybeUninit<u8>]) -> usize
/// Encoded size of u64 value.
#[inline]
pub(crate) fn encoded_varint64_len(value: u64) -> usize {
if value == 0 {
1
} else {
let significant_bits = 64 - value.leading_zeros();
(significant_bits + 6) as usize / 7
}
// Bitwise-or'ing by 1 allows the `value = zero` case to work without
// affecting other cases.
let significant_bits = 64 - (value | 1).leading_zeros();
(significant_bits + 6) as usize / 7
}

#[cfg(test)]
Expand Down

0 comments on commit c6f18ef

Please sign in to comment.