[virtmach] Your VM

MickaŽl Pointier virtmach@iecc.com
Wed, 30 May 2001 14:12:50 +0200

> > 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?)
> apart from speed considerations, i recently came across a useful reason
> for implementing as an array of function pointers rather than a switch
> statement: the core execution loop is very small, so it's possible to
> have alternative execution loops for very little cost.
> the Inferno VM uses this to implement single-step/breakpoint debugging
> - a process being debugged uses an alternative core execution function;
> others use the usual one (which is only 22 lines of code).
> so not only is the code prettier, it's also substantially more
> versatile.

It's a good reason, but I have another one too (still in versatility

If you use function pointers table, it means that you can dynamically
add or remove functions from the table. This allows you to add new
functions that can be present in dynamically loaded code (like DLL's),
using the DLL entry point to add the functions in the table. This way,
you do not have to change the core engine at all.

You can even have a "load extension" instruction in your basic bytecode
opcode list, that will load the DLL, declare the new functions in the table,
and then have your bytecode use new opcodes just after. It opens a lot
of possibilities, like extending a game thru internet downloads, and so on.

The fact of using pointers allows you to easily profile the time taken by
a specific instruction => Just have to replace the pointer by a call to
a function that increment a counter before calling the requested

Lot's of possibilities, no ?