c++ - Optimal virtual machine/byte-code interpreter loop -
my project has vm executes byte-code compiled domain-specific-language. i'm looking @ ways can improve execution time of byte-code. first step i'd see if there way improve byte-code interpreter before venture machine code compilation.
the main loop of interpreter looks this:
while(true) { uint8_t cmd = *code++; switch( cmd ) { case op_1: ...; break; ... } } question: there faster way implement loop without resorting assembler?
the 1 option see gcc specific use dynamic goto label addresses. rather break @ end of each case jump directly next instruction. had hoped optimizer me, looking @ disassembly apparently doesn't: there repeated constant jump @ end of op_codes.
if relevant vm simple register based machine floating point , integer registers (8 of each). there no stack, global heap (that language not complicated).
one easy optimisation instead of switch /case/case/case/case/case,
just define array function pointers (where each function process specified command, or couple of commands in case set several entries in array same function, , function check exact code), , instead of
switch(cmd) just a
array[cmd]() this given dont have many commands. also, checking if not define possible commands (maybe have 300 commands, have use 2 bytes representing them, instead of definining array 65536 items, check if command less 301 , if not, dont lookup)
if won't that, @ least sort commands used ones in beginning of switch statement.
otherwise hashtables, assume don't have many commands, , in case overhead of doing hash function cost more not having switch. (or have very simple hash function)
Comments
Post a Comment