mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 12:17:35 +00:00
AK: Optimize BitStream refilling a bit further
This tries to optimize the refill code by making it easier to digest for the branch predictor. This includes not looping as much across function calls and marking our EOF case to be unlikely. Co-Authored-By: Lucas Chollet <lucas.chollet@free.fr>
This commit is contained in:
parent
197331c922
commit
e9e89d7e4e
1 changed files with 20 additions and 19 deletions
|
@ -215,15 +215,8 @@ public:
|
|||
template<Unsigned T = u64>
|
||||
ErrorOr<T> peek_bits(size_t count)
|
||||
{
|
||||
while (count > m_bit_count) {
|
||||
if (TRY(refill_buffer_from_stream()))
|
||||
continue;
|
||||
|
||||
if (m_unsatisfiable_read_behavior == UnsatisfiableReadBehavior::Reject)
|
||||
return Error::from_string_literal("Reached end-of-stream without collecting the required number of bits");
|
||||
|
||||
break;
|
||||
}
|
||||
if (count > m_bit_count)
|
||||
TRY(refill_buffer_from_stream(count));
|
||||
|
||||
return m_bit_buffer & lsb_mask<T>(min(count, m_bit_count));
|
||||
}
|
||||
|
@ -254,10 +247,17 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
ErrorOr<bool> refill_buffer_from_stream()
|
||||
ErrorOr<void> refill_buffer_from_stream(size_t requested_bit_count)
|
||||
{
|
||||
if (m_stream->is_eof())
|
||||
return false;
|
||||
while (requested_bit_count > m_bit_count) [[likely]] {
|
||||
if (m_stream->is_eof()) [[unlikely]] {
|
||||
if (m_unsatisfiable_read_behavior == UnsatisfiableReadBehavior::FillWithZero) {
|
||||
m_bit_count = requested_bit_count;
|
||||
return {};
|
||||
}
|
||||
|
||||
return Error::from_string_literal("Reached end-of-stream without collecting the required number of bits");
|
||||
}
|
||||
|
||||
size_t bits_to_read = bit_buffer_size - m_bit_count;
|
||||
size_t bytes_to_read = bits_to_read / bits_per_byte;
|
||||
|
@ -267,8 +267,9 @@ private:
|
|||
|
||||
m_bit_buffer |= (buffer << m_bit_count);
|
||||
m_bit_count += bytes.size() * bits_per_byte;
|
||||
}
|
||||
|
||||
return true;
|
||||
return {};
|
||||
}
|
||||
|
||||
UnsatisfiableReadBehavior m_unsatisfiable_read_behavior;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue