Skip to content

Commit

Permalink
Unpeel the first iteration of the loop in impl_read_unsigned_leb128.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Jan 7, 2022
1 parent 5f549d9 commit facba24
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions compiler/rustc_serialize/src/leb128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ macro_rules! impl_read_unsigned_leb128 {
($fn_name:ident, $int_ty:ty) => {
#[inline]
pub fn $fn_name(slice: &[u8], position: &mut usize) -> $int_ty {
let mut result = 0;
let mut shift = 0;
// The first iteration of this loop is unpeeled. This is a
// performance win because this code is hot and integer values less
// than 128 are very common, typically occurring 50-80% or more of
// the time, even for u64 and u128.
let byte = slice[*position];
*position += 1;
if (byte & 0x80) == 0 {
return byte as $int_ty;
}
let mut result = (byte & 0x7F) as $int_ty;
let mut shift = 7;
loop {
let byte = slice[*position];
*position += 1;
Expand Down

0 comments on commit facba24

Please sign in to comment.