Repository: SerenityOS/serenity
File: Userland/Libraries/LibGfx/ImageFormats/BilevelImage.cpp (C++)

Code before:
    VERIFY(y < m_height);
    size_t byte_offset = x / 8;
    size_t bit_offset = x % 8;
    u8 byte = m_bits[y * m_pitch + byte_offset];
    u8 mask = 1u << (8 - 1 - bit_offset);
    if (b)
        byte |= mask;
    else
        byte &= ~mask;
    m_bits[y * m_pitch + byte_offset] = byte;
}

void BilevelImage::fill(bool b)
{
    u8 fill_byte = b ? 0xff : 0;
    for (auto& byte : m_bits.bytes())
        byte = fill_byte;
}

template<BilevelImage::BlendType operator_>
void BilevelImage::blend_into(BilevelImage& out, IntPoint position) const
{
    constexpr auto combine = [](bool dst, bool src) -> bool {
        if constexpr (operator_ == BlendType::Or)
            return dst || src;
        if constexpr (operator_ == BlendType::And)
            return dst && src;
        if constexpr (operator_ == BlendType::Xor)
            return dst ^ src;
        if constexpr (operator_ == BlendType::XNor)
            return !(dst ^ src);
        if constexpr (operator_ == BlendType::Replace)
            return src;
        VERIFY_NOT_REACHED();
    };

    IntRect bitmap_rect { position, { m_width, m_height } };
    IntRect out_rect { { 0, 0 }, { out.width(), out.height() } };
    IntRect clip_rect = bitmap_rect.intersected(out_rect);

    for (int y = clip_rect.top(); y < clip_rect.bottom(); ++y) {
        for (int x = clip_rect.left(); x < clip_rect.right(); ++x) {
            bool src_bit = get_bit(x - position.x(), y - position.y());
            bool dst_bit = out.get_bit(x, y);
            out.set_bit(x, y, combine(dst_bit, src_bit));
        }
    }
}

void BilevelImage::blend_into(BilevelImage& out, IntPoint position, BlendType operator_) const
{

Review comment (suggestion):
is moving away from `switch` necessary, or do you still get the same speedup by keeping switch and moving to passing in the operator as template param? I would expect the latter, and that way you'll keep getting compiler diags when the number of enum entries change

Code after:
    VERIFY(y < m_height);
    size_t byte_offset = x / 8;
    size_t bit_offset = x % 8;
    u8 byte = m_bits[y * m_pitch + byte_offset];
    u8 mask = 1u << (8 - 1 - bit_offset);
    if (b)
        byte |= mask;
    else
        byte &= ~mask;
    m_bits[y * m_pitch + byte_offset] = byte;
}

void BilevelImage::fill(bool b)
{
    u8 fill_byte = b ? 0xff : 0;
    for (auto& byte : m_bits.bytes())
        byte = fill_byte;
}

template<BilevelImage::CompositionType operator_>
void BilevelImage::composite_onto(BilevelImage& out, IntPoint position) const
{
    static constexpr auto combine = [](auto dst, auto src) -> decltype(dst) {
        switch (operator_) {
        case CompositionType::Or:
            return dst | src;
        case CompositionType::And:
            return dst & src;
        case CompositionType::Xor:
            return dst ^ src;
        case CompositionType::XNor:
            // Clang is not happy with using ~ on a bool, even if it's fine with our use case.
            if constexpr (SameAs<decltype(dst), bool>)
                return !(dst ^ src);
            else
                return ~(dst ^ src);
        case CompositionType::Replace:
            return src;
        }
        VERIFY_NOT_REACHED();
    };

    IntRect bitmap_rect { position, { m_width, m_height } };
    IntRect out_rect { { 0, 0 }, { out.width(), out.height() } };
    IntRect clip_rect = bitmap_rect.intersected(out_rect);

    for (int y = clip_rect.top(); y < clip_rect.bottom(); ++y) {
        for (int x = clip_rect.left(); x < clip_rect.right(); ++x) {
            bool src_bit = get_bit(x - position.x(), y - position.y());
            bool dst_bit = out.get_bit(x, y);
            out.set_bit(x, y, combine(dst_bit, src_bit));