Which would be a more efficient dispatch method for making my fetch-decode-execute times that little bit quicker?
For simplicity, I've kept this to a minimum, like operations operate on 1 byte operands and there are only two, for example.
The method I'm using at the moment (simplified) is:
typedef unsigned char byte;
vector<byte> _program = { INST::PUSH, 32, INST::POP};
enum INST {
PUSH =0, /*index=0*/
POP =1, /*index=1*/
}
//DISPATCHING METHOD #1
switch (curr_instruction) {
case INST::PUSH: {
/*declared inline*/ _push_to_stack(_program[instr_ptr+1]);
}
case INST::POP: {
/*declared inline*/ _pop_stack();
}
}
OR using a function pointer table to execute each instruction in the 'program' (vector of bytes/ vector _program
), like so:
typedef void (*voidptr)();
void hndl_push(){
/*declared inline*/ _push_to_stack(_program[instr_ptr+1]);
}
void hndl_push(){
/*declared inline*/ _pop_stack();
}
funcptr handlers[2] = {&hndl_push /*index=0*/, & hdnl_pop /*index=1*/}'
vector<byte> _program = { INST::PUSH, 32, INST::POP};
size_t instr_ptr=0;
//DISPATCHING METHOD #2
while (instr_ptr != _program.size()){
instr_ptr++;
_handlers[instr_ptr]();
}
I am using the VC++ (Visual Studio) compiler, the 2015 version.
Which of these is converted into more efficient assembler with the least overhead, or are they the same?
Thank you in advance!