From 0083c42304f8b523cbeb65387d0a7b01aa1aa3cd Mon Sep 17 00:00:00 2001 From: Levi Morrison Date: Sun, 25 Feb 2024 20:59:12 +0000 Subject: [PATCH] Faster encoded_varint64_len --- 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 eb9fc2d03..4d3e4ef67 100644 --- a/protobuf/src/varint/encode.rs +++ b/protobuf/src/varint/encode.rs @@ -92,12 +92,10 @@ pub(crate) fn encode_varint32(mut value: u32, buf: &mut [MaybeUninit]) -> 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)]