diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-01-07 10:11:49 +1100 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-01-07 10:40:51 +1100 |
| commit | 5f549d9b49868648f0fb152aa12084dcb859ee45 (patch) | |
| tree | b20812720ddf48bcd2c7ed0fc3f37b76c9fbdb8a /compiler/rustc_serialize/src | |
| parent | ddabe0775c5f5b551d5eb54e3c4366fb8bec0c92 (diff) | |
| download | rust-5f549d9b49868648f0fb152aa12084dcb859ee45.tar.gz rust-5f549d9b49868648f0fb152aa12084dcb859ee45.zip | |
Modify the buffer position directly when reading leb128 values.
It's a small but clear performance win.
Diffstat (limited to 'compiler/rustc_serialize/src')
| -rw-r--r-- | compiler/rustc_serialize/src/leb128.rs | 18 | ||||
| -rw-r--r-- | compiler/rustc_serialize/src/opaque.rs | 6 |
2 files changed, 9 insertions, 15 deletions
diff --git a/compiler/rustc_serialize/src/leb128.rs b/compiler/rustc_serialize/src/leb128.rs index ea2df80e641..de333774763 100644 --- a/compiler/rustc_serialize/src/leb128.rs +++ b/compiler/rustc_serialize/src/leb128.rs @@ -53,16 +53,15 @@ impl_write_unsigned_leb128!(write_usize_leb128, usize); macro_rules! impl_read_unsigned_leb128 { ($fn_name:ident, $int_ty:ty) => { #[inline] - pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) { + pub fn $fn_name(slice: &[u8], position: &mut usize) -> $int_ty { let mut result = 0; let mut shift = 0; - let mut position = 0; loop { - let byte = slice[position]; - position += 1; + let byte = slice[*position]; + *position += 1; if (byte & 0x80) == 0 { result |= (byte as $int_ty) << shift; - return (result, position); + return result; } else { result |= ((byte & 0x7F) as $int_ty) << shift; } @@ -122,15 +121,14 @@ impl_write_signed_leb128!(write_isize_leb128, isize); macro_rules! impl_read_signed_leb128 { ($fn_name:ident, $int_ty:ty) => { #[inline] - pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) { + pub fn $fn_name(slice: &[u8], position: &mut usize) -> $int_ty { let mut result = 0; let mut shift = 0; - let mut position = 0; let mut byte; loop { - byte = slice[position]; - position += 1; + byte = slice[*position]; + *position += 1; result |= <$int_ty>::from(byte & 0x7F) << shift; shift += 7; @@ -144,7 +142,7 @@ macro_rules! impl_read_signed_leb128 { result |= (!0 << shift); } - (result, position) + result } }; } diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index cc1216418ae..3d28e3293e1 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -559,11 +559,7 @@ impl<'a> Decoder<'a> { } macro_rules! read_leb128 { - ($dec:expr, $fun:ident) => {{ - let (value, bytes_read) = leb128::$fun(&$dec.data[$dec.position..]); - $dec.position += bytes_read; - Ok(value) - }}; + ($dec:expr, $fun:ident) => {{ Ok(leb128::$fun($dec.data, &mut $dec.position)) }}; } impl<'a> serialize::Decoder for Decoder<'a> { |
