Skip to content

Commit facba24

Browse files
committed
Unpeel the first iteration of the loop in impl_read_unsigned_leb128.
1 parent 5f549d9 commit facba24

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

compiler/rustc_serialize/src/leb128.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,17 @@ macro_rules! impl_read_unsigned_leb128 {
5454
($fn_name:ident, $int_ty:ty) => {
5555
#[inline]
5656
pub fn $fn_name(slice: &[u8], position: &mut usize) -> $int_ty {
57-
let mut result = 0;
58-
let mut shift = 0;
57+
// The first iteration of this loop is unpeeled. This is a
58+
// performance win because this code is hot and integer values less
59+
// than 128 are very common, typically occurring 50-80% or more of
60+
// the time, even for u64 and u128.
61+
let byte = slice[*position];
62+
*position += 1;
63+
if (byte & 0x80) == 0 {
64+
return byte as $int_ty;
65+
}
66+
let mut result = (byte & 0x7F) as $int_ty;
67+
let mut shift = 7;
5968
loop {
6069
let byte = slice[*position];
6170
*position += 1;

0 commit comments

Comments
 (0)