Highjack OPCODES to run other common instructions #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 firstif inst == Inst....
line, then runningpython test.py | sort | uniq -c | sort -n
to see which ones were called most. That happened to beInst.INVOKESTATIC
andInst.IINC
.I pulled each of those out into methods on
Machine
(so that they still have access toself
), then injected them into theOPCODES
dict so that they act like the other opcode functions. The main difference is thatOPCODES[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.