Skip to content

Commit

Permalink
Faster encoded_varint64_len
Browse files Browse the repository at this point in the history
  • Loading branch information
morrisonlevi authored and stepancheg committed Feb 25, 2024
1 parent c1c937b commit 0083c42
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 @@ -92,12 +92,10 @@ pub(crate) fn encode_varint32(mut value: u32, buf: &mut [MaybeUninit<u8>]) -> us
/// 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 0083c42

Please sign in to comment.