From 71b0aaa461dec4cae5db8767bdf7f4b21fe03181 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Wed, 29 Apr 2026 09:51:30 +0200 Subject: [PATCH] AK: Simplify BigEndianInputBitStream::read_bits() No need to perform extra loop iterations every time we need to read a new byte. No functional changes. --- AK/BitStream.h | 54 +++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/AK/BitStream.h b/AK/BitStream.h index 7e55f7dd67..5f7eed91cf 100644 --- a/AK/BitStream.h +++ b/AK/BitStream.h @@ -60,39 +60,39 @@ public: size_t nread = 0; while (nread < count) { - if (m_current_byte.has_value()) { - if constexpr (!IsSame && !IsSame) { - // read as many bytes as possible directly - if (((count - nread) >= 8) && is_aligned_to_byte_boundary()) { - // shift existing data over - result <<= 8; - result |= m_current_byte.value(); - nread += 8; - m_current_byte.clear(); - } else { - auto const bit = (m_current_byte.value() >> (7 - m_bit_offset)) & 1; - result <<= 1; - result |= bit; - ++nread; - if (m_bit_offset++ == 7) - m_current_byte.clear(); - } + if (!m_current_byte.has_value()) { + m_current_byte = TRY(m_stream->read_value()); + m_bit_offset = 0; + } + + if constexpr (IsOneOf) { + // Always take this branch for booleans or u8: there's no purpose in reading more than a single bit. + auto const bit = (m_current_byte.value() >> (7 - m_bit_offset)) & 1; + if constexpr (IsSame) { + result = bit; + } else { + result <<= 1; + result |= bit; + } + ++nread; + if (m_bit_offset++ == 7) + m_current_byte.clear(); + } else { + // Read as many bits as possible directly. + if ((count - nread >= 8) && is_aligned_to_byte_boundary()) { + // shift existing data over + result <<= 8; + result |= m_current_byte.value(); + nread += 8; + m_current_byte.clear(); } else { - // Always take this branch for booleans or u8: there's no purpose in reading more than a single bit auto const bit = (m_current_byte.value() >> (7 - m_bit_offset)) & 1; - if constexpr (IsSame) - result = bit; - else { - result <<= 1; - result |= bit; - } + result <<= 1; + result |= bit; ++nread; if (m_bit_offset++ == 7) m_current_byte.clear(); } - } else { - m_current_byte = TRY(m_stream->read_value()); - m_bit_offset = 0; } }