Lookup table to Function Pointer Array C++ performance

1.2k Views Asked by At

I have a following code to emulate basic system on my pc (x86):

typedef void (*op_fn) ();

void add()
{
   //add Opcode
   //fetch next opcode
   opcodes[opcode]();
}

void nop()
{
   //NOP opcode
   //fetch next opcode
   opcodes[opcode]();
}

const op_fn opcodes[256] =
{
    add,
    nop,
    etc...
};

and i call this "table" via opcodes[opcode]() I am trying to improve performance of my interpreter. What about inlining every function, like

inline void add()
inline void nop()

Is there any benefits of doing it?

Is there anyway to make it go faster? Thanks

3

There are 3 best solutions below

0
On BEST ANSWER

Just because you flag a method as inline it doesn't require the compiler to do so - it's more of a hint than an order.

Given that you are storing the opcode handlers in an array the compiler will need to place the address of the function into the array, therefore it can't inline it.

There's actually nothing wrong with your approach. If you really think you've got performance issues then get some metrics, otherwise don't worry (at this point!). The concept of a table of pointers to functions is nothing new - it's actually how C++ implement virtual functions (ie the vtable).

0
On

"Inline" means "don't emit a function call; instead, substitute the function body at compile time."

Calling through a function pointer means "do a function call, the details of which won't be known until runtime."

The two features are fundamentally opposed. (The best you could hope for is that a sufficiently advanced compiler could statically determine which function is being called through a function pointer in very limited circumstances and inline those.)

switch blocks are typically implemented as jump tables, which could have less overhead than function calls, so replacing your function pointer array with a switch block and using inline might make a difference.

0
On

inline is just a hint to your compiler, it does not guarantee any inlining being done. You should read up on inlining (maybe at the ISO C++ FAQ), as too much inlining can actually make your code slower (through code bloat and associated virtual memory trashing ).