All well and good provided your opcodes are sequential/dense
The same is true for a "switch" statement. If the "case" values used in a "switch" are sparse, the "switch" will not be compiled into an indexed jump instruction, but into a sequence of conditional jump instructions, which test all the possible cases. In this situation, the alternative to "switch" for implementing dispatching is no longer a computed "goto", but a multiple "if"/"else" sequence. A smarter compiler coul…
> A smarter compiler could detect when a "switch" forms the body of a loop and it would replicate the indexed jump instruction at the end of each case
Some Forths do this to save 1 jump per executed opcode (dispatch -> opcodeA -> opcodeB -> opcodeC etc). At the cost of having X copies of the dispatch code in memory (and perhaps in I-cache too).