Sub-optimal asm code for string-scanning state machine

One difference that I can see is that the C++ version uses a u8 for the enum.

If I also use a u8 in the Zig version, like this:

const State = enum(u8) {
...
};

then the total number of instructions shrinks significantly.
Upon further inspection the actual size doesn’t really matter, enum(usize) seems to produce the same code.
I think by default the backing integer of the enum is an odd integer, like a u2 in this case. For these odd integers there is a performance issue, introducing unecessary code: LLVM: Non power of two integer arithmetic emits slow assembly · Issue #19616 · ziglang/zig · GitHub

7 Likes