Optimize and simplify BitReaderReversed
#81
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR refactors the
BitReaderReversed
object based on the structure from the Reading bits in far too many ways blog series.It retains the
get_bits
method to read bits from the stream, but also exposes lower-levelpeek_bits
,consume
, andrefill
methods. These new methods may unlock optimizations in other parts of the code by reducing the number of hard-to-predict branches on whether to refill the bit container. And a separatepeek_bits
method might be especially useful for Huffman decoding because it lets you do a table lookup with N bits and then decide to consume fewer than N of them, rather than the current strategy of intentionally reading past the end of the input.On my machine (a Ryzen 5600X), I see a ~5% end-to-end performance improvement in decode time for enwik9 from this change.