python-jvm-interpreter
python-jvm-interpreter copied to clipboard
Highjack OPCODES to run other common instructions
DO NOT MERGE
This is just for demonstration purposes. In a nutshell, I kinda-profiled the instructions being called by putting print(inst) before the first if inst == Inst.... line, then running python test.py | sort | uniq -c | sort -n to see which ones were called most. That happened to be Inst.INVOKESTATIC and Inst.IINC.
I pulled each of those out into methods on Machine (so that they still have access to self), then injected them into the OPCODES dict so that they act like the other opcode functions. The main difference is that OPCODES[inst](frame) is calling a method instead of a module-level function, but sshhhh, that's our little secret.
This gave about a 10% speedup, as Python doesn't have to work its way through a big if ... elif ... elif block.
Even if you ❤️ this approach, please do it in a prettier way than I did. I just wanted to make it work, period, as an experiment. 🙂
Oh, also, I apologize for the whitespace changes. I've configured my editor to use Black on all my Python, and I'd forgotten to disable that before saving. GitHub has an option to ignore whitespace in its diffs so that should hide the Black stuff so you can see the actual code changes.