(sry for the formatting, doesn’t really work on my mobile)
Inside some of the switch prongs, I used opcode to decide between instructions with short operands (1 byte) and long operands (3 bytes).
Changing this construct to a labelled switch, basically means to add continue :vmloop frame.readOpCode() at the end of each switch prong.
However, the opcode variable which is the original switch expression is still there, but no longer valid inside the prongs, it still contains the very first opcode.
Maybe there shouldn’t be an expression at all in a labelled switch?
That would make many idioms not work, e.g grep the Zig compiler code base for : switch (
Yeah I noticed similar improvements, until the opcode set grew (or the prongs did) and I had to revert back to a mix of loop and labelled switch. It’s hard to please the LLVM opt pipeline, so I gate vmloop changes with performance regression tests.
A quick, powerful, and basically language-agnostic[1] protip for this kind of refactor:
Rename the variable above the switch statement to e.g. opcode_ temporarily. But not with the help of an LSP, just manually at the declaration site. This turns all references to it into compiler errors, so you can dig through them one-by-one and fix them as needed, without worrying that you’ll miss an occurrence. Footgun eliminated by making the compiler help you.
Compiling with zig build --watch --prominent-compile-errors makes this sort of thing a breeze.
Except for languages that don’t require variable declarations before use, like Python or non-strict JavaScript. ↩︎