Speed up leb128 encoding and decoding for unsigned values. by michaelwoerister · Pull Request #46919 · rust-lang/rust (original) (raw)
So, running some tests shows that the following implementation for u32 is 10% faster when encoding metadata (while showing no improvement for the query-cache and the dep-graph):
#[inline] pub fn write_leb128_u32(out: &mut Vec, start_position: usize, value: u32) -> usize {
if value <= (1 << 7) {
write_to_vec(out, start_position, value as u8);
1
} else if value <= (1 << 14) {
write_to_vec(out, start_position, (value as u8) | 0x80);
write_to_vec(out, start_position + 1, (value >> 7) as u8);
2
} else if value <= (1 << 21) {
write_to_vec(out, start_position, (value as u8) | 0x80);
write_to_vec(out, start_position + 1, ((value >> 7) as u8) | 0x80);
write_to_vec(out, start_position + 2, (value >> 14) as u8);
3
} else if value <= (1 << 28) {
write_to_vec(out, start_position, (value as u8) | 0x80);
write_to_vec(out, start_position + 1, ((value >> 7) as u8) | 0x80);
write_to_vec(out, start_position + 2, (value >> 14) as u8 | 0x80);
write_to_vec(out, start_position + 3, (value >> 21) as u8);
4
} else {
write_to_vec(out, start_position, (value as u8) | 0x80);
write_to_vec(out, start_position + 1, ((value >> 7) as u8) | 0x80);
write_to_vec(out, start_position + 2, (value >> 14) as u8 | 0x80);
write_to_vec(out, start_position + 3, (value >> 21) as u8 | 0x80);
write_to_vec(out, start_position + 4, (value >> 28) as u8);
5
}}
A similar implementation for usize does a lot worse than the one from the PR. Not sure if it's worth the trouble since my test data is only from one crate.