We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5f549d9 commit facba24Copy full SHA for facba24
compiler/rustc_serialize/src/leb128.rs
@@ -54,8 +54,17 @@ macro_rules! impl_read_unsigned_leb128 {
54
($fn_name:ident, $int_ty:ty) => {
55
#[inline]
56
pub fn $fn_name(slice: &[u8], position: &mut usize) -> $int_ty {
57
- let mut result = 0;
58
- 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
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;
68
loop {
69
let byte = slice[*position];
70
*position += 1;
0 commit comments