Skip to content

Commit 921540b

Browse files
committed
Auto merge of #69157 - nnethercote:tweak-LEB128-reading, r=<try>
Tweak LEB128 reading some more. PR #69050 changed LEB128 reading and writing. After it landed I did some double-checking and found that the writing changes were universally a speed-up, but the reading changes were not. I'm not exactly sure why, perhaps there was a quirk of inlining in the particular revision I was originally working from. This commit reverts some of the reading changes, while still avoiding `unsafe` code. I have checked it on multiple revisions and the speed-ups seem to be robust. r? @michaelwoerister
2 parents 5e7af46 + e25bd1f commit 921540b

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/libserialize/leb128.rs

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1+
#[cfg(target_pointer_width = "32")]
2+
const USIZE_LEB128_SIZE: usize = 5;
3+
#[cfg(target_pointer_width = "64")]
4+
const USIZE_LEB128_SIZE: usize = 10;
5+
6+
macro_rules! leb128_size {
7+
(u16) => {
8+
3
9+
};
10+
(u32) => {
11+
5
12+
};
13+
(u64) => {
14+
10
15+
};
16+
(u128) => {
17+
19
18+
};
19+
(usize) => {
20+
USIZE_LEB128_SIZE
21+
};
22+
}
23+
124
macro_rules! impl_write_unsigned_leb128 {
225
($fn_name:ident, $int_ty:ident) => {
326
#[inline]
427
pub fn $fn_name(out: &mut Vec<u8>, mut value: $int_ty) {
28+
// A `loop` is faster than a `for` loop for writing.
529
loop {
630
if value < 0x80 {
731
out.push(value as u8);
@@ -28,17 +52,17 @@ macro_rules! impl_read_unsigned_leb128 {
2852
let mut result = 0;
2953
let mut shift = 0;
3054
let mut position = 0;
31-
loop {
55+
// A `for` loop is faster than a `loop` for reading.
56+
for _ in 0..leb128_size!($int_ty) {
3257
let byte = slice[position];
3358
position += 1;
59+
result |= ((byte & 0x7F) as $int_ty) << shift;
3460
if (byte & 0x80) == 0 {
35-
result |= (byte as $int_ty) << shift;
36-
return (result, position);
37-
} else {
38-
result |= ((byte & 0x7F) as $int_ty) << shift;
61+
break;
3962
}
4063
shift += 7;
4164
}
65+
(result, position)
4266
}
4367
};
4468
}

0 commit comments

Comments
 (0)