# Sub-optimal asm code for string-scanning state machine

**URL:** <https://ziggit.dev/t/sub-optimal-asm-code-for-string-scanning-state-machine/4402>\
**Category:** Help\
**Tags:** compiler, cross-compile\
**Created:** [May 22, 2024, 2:01am UTC](https://ziggit.dev/t/sub-optimal-asm-code-for-string-scanning-state-machine/4402 "2024-05-22T02:01:26Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [May 22, 2024, 5:53pm UTC](https://ziggit.dev/t/sub-optimal-asm-code-for-string-scanning-state-machine/4402/13 "2024-05-22T17:53:05Z")

</div>

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:

```zig
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](https://github.com/ziglang/zig/issues/19616)

---

_[View the full topic](https://ziggit.dev/t/sub-optimal-asm-code-for-string-scanning-state-machine/4402)._
