mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +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
Notes:
sideshowbarker
2024-07-17 09:49:48 +09:00
Author: https://github.com/timschumi Commit: https://github.com/SerenityOS/serenity/commit/e9e89d7e4e Pull-request: https://github.com/SerenityOS/serenity/pull/21890 Issue: https://github.com/SerenityOS/serenity/issues/21869 Reviewed-by: https://github.com/IdanHo Reviewed-by: https://github.com/LucasChollet Reviewed-by: https://github.com/trflynn89
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…
Reference in a new issue