Repository: microsoft/terminal
File: src/terminal/adapter/SixelParser.cpp (C++)

Code before:
    // may cover more than one device pixel, depending on the aspect ratio.
    const auto targetOffset = _imageCursor.y * _imageMaxWidth + _imageCursor.x;
    auto imageBufferPtr = std::next(_imageBuffer.data(), targetOffset);
    repeatCount = std::min(repeatCount, _imageMaxWidth - _imageCursor.x);
    for (auto i = 0; i < 6; i++)
    {
        if (sixelValue & 1)
        {
            auto repeatAspectRatio = _pixelAspectRatio;
            do
            {
                std::fill_n(imageBufferPtr, repeatCount, _foregroundPixel);
                std::advance(imageBufferPtr, _imageMaxWidth);
            } while (--repeatAspectRatio > 0);
        }
        else
        {
            std::advance(imageBufferPtr, _imageMaxWidth * _pixelAspectRatio);
        }
        sixelValue >>= 1;
    }
    _imageCursor.x += repeatCount;
}

void SixelParser::_eraseImageBufferRows(const int rowCount, const til::CoordType rowOffset) noexcept
{
    const auto pixelCount = rowCount * _cellSize.height;
    const auto bufferOffset = rowOffset * _cellSize.height * _imageMaxWidth;
    const auto bufferOffsetEnd = bufferOffset + pixelCount * _imageMaxWidth;
    if (static_cast<size_t>(bufferOffsetEnd) >= _imageBuffer.size()) [[unlikely]]
    {
        _decreaseFilledBackgroundHeight(_imageCursor.y);
        _imageBuffer.clear();
        _imageCursor.y = 0;
    }
    else
    {
        _decreaseFilledBackgroundHeight(pixelCount);
        _imageBuffer.erase(_imageBuffer.begin() + bufferOffset, _imageBuffer.begin() + bufferOffsetEnd);
        _imageCursor.y -= pixelCount;
    }
}

void SixelParser::_maybeFlushImageBuffer(const bool endOfSequence)
{
    // Regardless of whether we flush the image or not, we always calculate how
    // much we need to scroll in advance. This algorithm is a bit odd. If there
    // isn't enough space for the current segment, it'll scroll until it can fit
    // the segment with a pixel to spare. So in the case that it's an exact fit,
    // it's expected that we'd scroll an additional line. Although this is not
    // common, since it only occurs for pixel aspect ratios of 4:1 or more. Also

Review comment (suggestion):
Initially, I did this `int16_t` cast, because `std::fill` contains optimizations for primitive types (those that can be memset'd). To my dismay, I found out today that they added this check for "are all bits zero? if so, use memset": https://github.com/microsoft/STL/commit/a27d894d63d228217faac37d04e6d9e8acb58675

Code after:
    // is received. So if we haven't filled it yet, we need to do so now.
    _fillImageBackground();

    repeatCount = std::min(repeatCount, _imageMaxWidth - _imageCursor.x);
    if (repeatCount <= 0)
    {
        return;
    }

    if (sixelValue == 0)
    {
        _imageCursor.x += repeatCount;
        return;
    }

    // This allows us to unsafely cast _imageBuffer to uint16_t
    // and benefit from compiler/STL optimizations.
    static_assert(sizeof(IndexedPixel) == sizeof(int16_t));
    static_assert(alignof(IndexedPixel) == alignof(int16_t));

    // Then we need to render the 6 vertical pixels that are represented by the
    // bits in the sixel value. Although note that each of these sixel pixels
    // may cover more than one device pixel, depending on the aspect ratio.
    const auto targetOffset = _imageCursor.y * _imageMaxWidth + _imageCursor.x;
    const auto foreground = std::bit_cast<int16_t>(_foregroundPixel);
    auto imageBufferPtr = reinterpret_cast<int16_t*>(_imageBuffer.data() + targetOffset);

    // A aspect ratio of 1:1 is the most common and worth optimizing.
    if (_pixelAspectRatio == 1)
    {
        do
        {
            // This gets unrolled by MSVC. It's written this way to use CMOV instructions.
            // Modern CPUs have fat caches and deep pipelines. It's better to do pointless reads
            // from memory than causing branch misprediction, as sixelValue is highly random.
            for (int i = 0; i < 6; i++)
            {
                const auto test = sixelValue & (1 << i);
                // Possibly pointless read from memory, but...
                const auto before = imageBufferPtr[i * _imageMaxWidth];
                // ...it allows the compiler to turn this into a CMOV (= no branch misprediction).
                const auto after = test ? foreground : before;
                imageBufferPtr[i * _imageMaxWidth] = after;
            }

            _imageCursor.x += 1;
            imageBufferPtr += 1;
            repeatCount -= 1;
        } while (repeatCount > 0);
    }
    else