AK: Add fast paths for aligned bit writes in BitOutputStream

If the bit write is aligned (or has been aligned during the write) we can
write in multiples of 32/16/8 bits for increased performance.
This commit is contained in:
Idan Horowitz 2021-03-13 23:38:27 +02:00 committed by Andreas Kling
parent 3c7aa56ae8
commit 1b7b503bae

View file

@ -152,6 +152,12 @@ public:
void write_bits(u32 bits, size_t count)
{
VERIFY(count <= 32);
if (count == 32 && !m_next_byte.has_value()) { // fast path for aligned 32 bit writes
m_stream << bits;
return;
}
size_t n_written = 0;
while (n_written < count) {
if (m_stream.has_any_error()) {
@ -167,6 +173,12 @@ public:
m_stream << m_next_byte.value();
m_next_byte.clear();
}
} else if (count - n_written >= 16) { // fast path for aligned 16 bit writes
m_stream << (u16)((bits >> n_written) & 0xFFFF);
n_written += 16;
} else if (count - n_written >= 8) { // fast path for aligned 8 bit writes
m_stream << (u8)((bits >> n_written) & 0xFF);
n_written += 8;
} else {
m_bit_offset = 0;
m_next_byte = 0;