@@ -51,28 +51,35 @@ macro_rules! impl_read_unsigned_leb128 {
5151 ( $fn_name: ident, $int_ty: ty) => {
5252 #[ inline]
5353 pub fn $fn_name( slice: & [ u8 ] , position: & mut usize ) -> $int_ty {
54- // The first iteration of this loop is unpeeled. This is a
55- // performance win because this code is hot and integer values less
56- // than 128 are very common, typically occurring 50-80% or more of
57- // the time, even for u64 and u128.
58- let byte = slice[ * position] ;
59- * position += 1 ;
60- if ( byte & 0x80 ) == 0 {
61- return byte as $int_ty;
62- }
63- let mut result = ( byte & 0x7F ) as $int_ty;
64- let mut shift = 7 ;
65- loop {
66- let byte = slice[ * position] ;
67- * position += 1 ;
54+ #[ inline]
55+ fn inner( slice: & [ u8 ] , position: & mut usize ) -> Option <$int_ty> {
56+ let mut pos = * position;
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. get( pos) ?;
62+ pos += 1 ;
6863 if ( byte & 0x80 ) == 0 {
69- result |= ( byte as $int_ty) << shift;
70- return result;
71- } else {
72- result |= ( ( byte & 0x7F ) as $int_ty) << shift;
64+ * position = pos;
65+ return Some ( byte as $int_ty) ;
66+ }
67+ let mut result = ( byte & 0x7F ) as $int_ty;
68+ let mut shift = 7 ;
69+ loop {
70+ let byte = * slice. get( pos) ?;
71+ pos += 1 ;
72+ if ( byte & 0x80 ) == 0 {
73+ result |= ( byte as $int_ty) << shift;
74+ * position = pos;
75+ return Some ( result) ;
76+ } else {
77+ result |= ( ( byte & 0x7F ) as $int_ty) << shift;
78+ }
79+ shift += 7 ;
7380 }
74- shift += 7 ;
7581 }
82+ inner( slice, position) . unwrap( )
7683 }
7784 } ;
7885}
0 commit comments