summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-03-13 23:38:27 +0200
committerAndreas Kling <kling@serenityos.org>2021-03-13 23:50:07 +0100
commit1b7b503bae7b0eda8260c41ca0cf628f0371735c (patch)
tree198d2f24be3dada555dcd19a29a4043fdddb8c6b /AK
parent3c7aa56ae85bd1dcbfbbc9e739f8bac53d7596d9 (diff)
downloadserenity-1b7b503bae7b0eda8260c41ca0cf628f0371735c.zip
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.
Diffstat (limited to 'AK')
-rw-r--r--AK/BitStream.h12
1 files changed, 12 insertions, 0 deletions
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;