On 7/24/2012 12:50 PM, Dmitry Olshansky wrote:
are the same (same as in same number of indirections).

     switch (code[pc++])

and

     goto code[pc++]()

are the same, too.

It's not. Let's get to assembly then. It's an illustration as I'm no expert and
may have made some illegal shortcuts in this listing.

goto code[pc++]() is roughly:

mov ecx, [ebx]
inc ebx
jmp [ecx]

   jmp code[ecx]


switch(code[pc++]) is:

mov ecx, [ebx]
inc ebx
mov ecx, [edx+ecx] // assuming jump table is at edx
jump [ecx]

   jmp jumptable[ecx]


If you count only jumps, then yes, the same number of indirect jumps. BUT note
the use of extra register to point to the table & extra read of jump table
contents. (BTW I assumed jump table address is loaded in register, a luxurious
assumption esp. on 32bit).

You don't need an extra register for the jump table address - and if you did, you'd need it for both, as the table needs to be referenced somehow.

Addressing modes have long been "for free" in turns of runtime cost, so

   [ECX]

and

   offset[ECX]

are the same cost.


Again, the biggest practical limitation of switches (loosing some performance
hurts but not show stopper) is that last time I checked dmd doesn't try to merge
equivalent jump tables.

I can't think of an example of this.


Thus I can't put VM dispatch switch at the of each branch of main opcode switch
(see my earlier posts) to help branch predictor. It just spawns ton of new
tables, of course it has lower performance and wastes data cache.

Please post source code example so I understand what you mean.

Reply via email to