mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
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:
parent
3c7aa56ae8
commit
1b7b503bae
Notes:
sideshowbarker
2024-07-18 21:23:27 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/1b7b503bae7 Pull-request: https://github.com/SerenityOS/serenity/pull/5767
1 changed files with 12 additions and 0 deletions
|
@ -152,6 +152,12 @@ public:
|
||||||
void write_bits(u32 bits, size_t count)
|
void write_bits(u32 bits, size_t count)
|
||||||
{
|
{
|
||||||
VERIFY(count <= 32);
|
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;
|
size_t n_written = 0;
|
||||||
while (n_written < count) {
|
while (n_written < count) {
|
||||||
if (m_stream.has_any_error()) {
|
if (m_stream.has_any_error()) {
|
||||||
|
@ -167,6 +173,12 @@ public:
|
||||||
m_stream << m_next_byte.value();
|
m_stream << m_next_byte.value();
|
||||||
m_next_byte.clear();
|
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 {
|
} else {
|
||||||
m_bit_offset = 0;
|
m_bit_offset = 0;
|
||||||
m_next_byte = 0;
|
m_next_byte = 0;
|
||||||
|
|
Loading…
Reference in a new issue