3 ms·
>but I tried something different in an attempt to avoid large switch statements. This must be some sort of generation gap. Because array of pointers for opcode
by katastic 9y ago
>but I tried something different in an attempt to avoid large switch statements.
This must be some sort of generation gap. Because array of pointers for opcodes is like... emulator 101. It's just a jump table.
Glad you're learning and trying stuff though!
- loeg 9y agoAnd compilers will often create jump tables from large switch statements anyway.
- mikebenfield 9y agoIndeed. At least in my experience, Rust (or LLVM, presumably) will make jump tables from all sorts of nonsense, doing something efficient even when there are gaps in matched values. Manually creating jump tables seems to be a waste of time. (Actually, it doesn't even need to be a switch statement... Just a succession of `if`s works fine.)
- FRex 9y agoTables made out of computed gotos (so not exactly function pointers) are supposedly speeding CPython up: https://eli.thegreenplace.net/2012/07/12/computed-goto-for-efficient-dispatch-tables https://eli.thegreenplace.net/2012/07/12/computed-goto-for-e... I've also found this comment in Python 3.6 source code in ceval.c which is where opcode dispatch is: https://pastebin.com/kuV7UfG0 https://pastebin.com/kuV7UfG0
- DSMan195276 9y ago> At least in my experience, Rust (or LLVM, presumably) will make jump tables from all sorts of nonsense, doing something efficient even when there are gaps in matched values. Manually creating jump tables seems to be a waste of time. I wouldn't say it is always a waste of time just because jump tables can be easier to read and manage when you have a decent number of opcodes. Being able to have all your opcodes in separate functions and then just have one single list that shows exactly what opcode maps to what instruction is really nice: https://github.com/mkilgore/yaji/blob/master/src/op.c#L336 https://github.com/mkilgore/yaji/blob/master/src/op.c#L336 To each his own though, you're right it likely doesn't matter that much performance wise.