Skip to content

Commit

Permalink
Improve BoolReader::read_bool performance (#19)
Browse files Browse the repository at this point in the history
Compute the required left shift using `leading_zeros` instead of
shifting left one bit at a time per loop iteration.
  • Loading branch information
okaneco authored Dec 17, 2023
1 parent 123e967 commit f98b744
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/vp8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,18 +715,24 @@ impl BoolReader {
false
};

while self.range < 128 {
self.value <<= 1;
self.range <<= 1;
self.bit_count += 1;

if self.bit_count == 8 {
self.bit_count = 0;
if self.range < 128 {
// Compute shift required to satisfy `self.range >= 128`.
// Apply that shift to `self.range`, `self.value`, and `self.bitcount`.
//
// Subtract 24 because we only care about leading zeros in the
// lowest byte of `self.range` which is a `u32`.
let shift = self.range.leading_zeros() - 24;
self.value <<= shift;
self.range <<= shift;
self.bit_count += shift as u8;

if self.bit_count >= 8 {
self.bit_count %= 8;

// If no more bits are available, just don't do anything.
// This strategy is suggested in the reference implementation of RFC6386 (p.135)
if self.index < self.buf.len() {
self.value |= u32::from(self.buf[self.index]);
self.value |= u32::from(self.buf[self.index]) << self.bit_count;
self.index += 1;
}
}
Expand Down

0 comments on commit f98b744

Please sign in to comment.