From c6f18ef1080c1ab5cac1010f1b7d5737c48c75d3 Mon Sep 17 00:00:00 2001 From: Levi Morrison Date: Fri, 2 Feb 2024 14:08:47 -0700 Subject: [PATCH] refactor: simplify encoded_varint64_len This greatly cleans up the assembly as well, but this function is not as likely to matter from a perf perspective. --- protobuf/src/varint/encode.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/protobuf/src/varint/encode.rs b/protobuf/src/varint/encode.rs index 4f751f4f2..c8d984d2a 100644 --- a/protobuf/src/varint/encode.rs +++ b/protobuf/src/varint/encode.rs @@ -59,12 +59,10 @@ pub(crate) fn encode_varint32(value: u32, buf: &mut [MaybeUninit]) -> 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)]