Repository: dotnet/runtime
File: src/coreclr/jit/emitwasm.cpp (C++)

Code before:
    assert(FitsIn<uint16_t>(opcode));
    if (FitsIn<uint8_t>(opcode))
    {
        emitOutputByte(dst, opcode);
        sz += 1;
    }
    else if (FitsIn<uint16_t>(opcode))
    {
        dst += emitOutputByte(dst, opcode & 0xFF);
        emitOutputByte(dst, opcode >> 8);
        sz += 2;
    }
    return sz;
}

size_t emitter::emitOutputPaddedReloc(uint8_t* destination)
{
    if (destination)
    {
        static_assert(PADDED_RELOC_SIZE > 1);
        // Write zeroes with the bit set that indicates another byte is coming
        for (unsigned i = 1; i < PADDED_RELOC_SIZE; i++)
            *(destination++ + writeableOffset) = 0x80;

        // Write a final zero
        *(destination + writeableOffset) = 0x0;
    }
    return PADDED_RELOC_SIZE;
}

size_t emitter::emitOutputConstant(uint8_t* destination, const instrDesc* id, bool isSigned, CorInfoReloc relocType)
{
    if (id->idIsCnsReloc())
    {
        emitRecordRelocation(destination, (void*)emitGetInsSC(id), relocType);
        return emitOutputPaddedReloc(destination);
    }
    else if (isSigned)
        return emitOutputSLEB128(destination, (int64_t)emitGetInsSC(id));
    else
        return emitOutputULEB128(destination, (uint64_t)emitGetInsSC(id));
}

size_t emitter::emitOutputInstr(insGroup* ig, instrDesc* id, BYTE** dp)
{
    const bool SIGNED = true;
    const bool UNSIGNED = false;

    BYTE*       dst    = *dp;
    size_t      sz     = emitSizeOfInsDsc(id);
    instruction ins    = id->idIns();

Review comment (suggestion):
```suggestion
        // Write zeroes with the bit set that indicates another byte is coming
        for (unsigned i = 0; i < PADDED_RELOC_SIZE - 1; i++)
            destination += emitOutputByte(0x80);

        // Write the final zero
        emitOutputByte(destination, 0x00);
```
The fewer `writeableOffset` references the better.

Code after:

    assert(FitsIn<uint16_t>(opcode));
    if (FitsIn<uint8_t>(opcode))
    {
        emitOutputByte(dst, opcode);
        sz += 1;
    }
    else if (FitsIn<uint16_t>(opcode))
    {
        dst += emitOutputByte(dst, opcode & 0xFF);
        emitOutputByte(dst, opcode >> 8);
        sz += 2;
    }
    return sz;
}

size_t emitter::emitOutputPaddedReloc(uint8_t* destination)
{
    static_assert(PADDED_RELOC_SIZE > 1);
    // Write zeroes with the bit set that indicates another byte is coming
    for (unsigned i = 0; i < PADDED_RELOC_SIZE - 1; i++)
    {
        destination += emitOutputByte(destination, 0x80);
    }

    emitOutputByte(destination, 0x0);
    return PADDED_RELOC_SIZE;
}

size_t emitter::emitOutputConstant(uint8_t* destination, const instrDesc* id, bool isSigned, CorInfoReloc relocType)
{
    if (id->idIsCnsReloc())
    {
        emitRecordRelocation(destination, (void*)emitGetInsSC(id), relocType);
        return emitOutputPaddedReloc(destination);
    }

    if (isSigned)
    {
        return emitOutputSLEB128(destination, (int64_t)emitGetInsSC(id));
    }
    else
    {
        return emitOutputULEB128(destination, (uint64_t)emitGetInsSC(id));
    }
}

size_t emitter::emitOutputInstr(insGroup* ig, instrDesc* id, BYTE** dp)
{
    const bool SIGNED   = true;
    const bool UNSIGNED = false;