[virtmach] Your VM

Anton Ertl anton@mips.complang.tuwien.ac.at
Tue, 29 May 2001 12:09:56 +0200 (MET DST)

Thomas Fjellstrom wrote:
> I know you asked for every body elses ideas but the VM that
> I made seems slow to me when thinking of the average number
> of instructions per second that my VM does (600,000-800,000)

Given the kind of VM (2-address register machine) you have, this
performance would be decent on a 20 MIPS machine.

> Now I have a question: does any one know for sure
> if an array of function pointers is faster than
> a switch case? I know threaded code is faster but
> is an function pointer array? (will it help the
> cpu cache enough to make a difference?)

It's not clear if you want to use the function pointers in
threaded-code style (i.e., the actual VM code consists of function
pointers) or dispatch-table style (i.e., the VM code consists of
indices into the table, and the table contains function pointers).

Anyway, the main cost of using function pointers is that you have too
keep the implicit VM registers (instruction pointer, stack pointer
etc.) in global variables, i.e. in memory (unless you use gcc's
explicit register variables, but then, why not go for threaded code?).
You also have to do an additional return (quite cheap on CPUs with a
return stack, but expensive on some) for every VM instruction
executed, and possibly some function setup overhead.

In the switch case, the additional code is for the range check, and
(when compared to threaded-code style use of function pointers) a
table lookup.

My guess is that the switch approach will usually win.

- anton