On 28.10.2011 22:57, ponce wrote:
Provided some hairy conditions, the switch instruction will optimize to
a jump table in GCC and probably most C compilers.


They do, you have no guaranties. It will either fly or crawl, depending on sparseness of values. Even so it *usually* makes table, just check on it from time to time ;).

In ICC, some static analysis is even used to optimize out the test
before the switch.

But switch-jump alone is not enough to get efficient VM interpreter.
The thing is this tiny bit at the end of every instruction code:
....
 goto opcodes[pProgram[regs.PC++]];

This is instruction dispatch, the trick in the branch prediction that operates on per branch basis, thus single switch-jump based VM dispatch will mispredict jumps most of the time. I seen claims of up to 99% on average. If you place a whole switch at the end of every instruction code I'm not sure compiler will find it's way out of this mess, or even optimize it. I tried that with DMD, the problem is it have no idea how to use the *same* jump *table* for all of identical switches.

In D, final switch might enable such an optimization with statically
checking for out-of-enum values.


--
Dmitry Olshansky

Reply via email to