From 1b7b503bae7b0eda8260c41ca0cf628f0371735c Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sat, 13 Mar 2021 23:38:27 +0200 Subject: 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. --- AK/BitStream.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'AK') diff --git a/AK/BitStream.h b/AK/BitStream.h index 8e509dd5fe..ec6563f338 100644 --- a/AK/BitStream.h +++ b/AK/BitStream.h @@ -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; -- cgit v1.2.3