Description
I am aware that JIT was previously mentioned in #4085, but I'm willing to work on this, so please hear me out.
I'm in the process of building a game console that utilizes MicroPython. I started over a year ago and I'm getting close to done but I have a few issues. I've been puzzling for a while how to make MicroPython faster. I know I can write C modules for certain functionality and APIs, but game developers only have access to natmod, and the viper and native decorators. I could be wrong, but I noticed that the code compiled by the viper and native decorators sticks around long after its needed. Also, I can only fit small amounts of code into IRAM. So when using large amounts of code that needs to run natively and at the same time, you run out of RAM.
So, after a few weeks of thought (and a lot of time spent playing on emulated consoles), I finally got an idea: a JIT Compiler. It will definitely increase memory usage and code size but it would result in significant speed boosts in certain cases.
So, if I'm remembering correctly, a JIT consists of the following components:
- Compiler (Already mostly implemented for xtensawin thanks to the viper and native decorators)
- Code analyzer. I feel like this would take a lot of memory and CPU time, but there's probably a way to minimize this.
- Cache. This would take a lot of RAM so this may need to be limited in size/entries or just not exist.
- Linker. If "hot" code is compiled by the JIT then we need to execute the instructions and not the bytecode. Then we'll have to relink it once this code is evicted from cache.
- Code pools. This is where we store the actively executing code.
This looks like a pretty complex system (and I'm sure the current compiler takes an input file to compile and not just a function but I could be wrong) and it would take a lot of time and effort to make a decent JIT.
I'm willing to do most if not all of the work on this. If @dpgeorge you have any suggestions please let me know. And if this JIT turns out to work pretty well, then I'll submit a PR to merge this into MicroPython as an optional component because I'm sure most people don't need a JIT and it will take a lot of memory (especially IRAM).