How to use the JIT compiler
The effect of the JIT compiler is most notable on mobile devices where we have observed around a 4x performance increase on our games.
Please note that the Moonshine JIT compiler is not in any way related to the LuaJIT project and performs a very different process.
How to enable the JIT
Simply download the latest version of Moonshine and add the following line before loading any code.
shine.jit.enabled = true;
Aside from the known limitations, using the JIT should be seamless.
There are some configurable pseudo-constants that dictate when and how the JIT compiler works:
This is the number of times a function is allowed to run in interpreted mode before it is compiled (on the next call). As there is an overhead to the compile phase, functions are not compiled until they are identified as having been executed enough times to make it worth the overhead. Increase this value to compile less functions and concentrate on only the functions that are used most often.
When the compilation of a function is deferred, a compile queue is created and an interval starts ticking. On each tick, the FPS since last tick is calculated and compared to shine.jit.MIN_FPS_TO_COMPILE. The shine.jit.COMPILE_INTERVAL constant defines the number of milliseconds between each tick. You may wish to increase this number on processor-intensive scripts.
The FPS that is required before the compile queue is processed. Reduce this number to loosen the definition of idle.
If this value is set to zero, compilation is not deferred and instead occurs inline.
The JIT compiler should be configured before any VM has started, like in this example:
shine.jit.enabled = true; shine.jit.INVOCATION_TOLERANCE = 3; shine.jit.MIN_FPS_TO_COMPILE = 29; var vm = new shine.VM(); vm.load('my-script.lua.json');
vm.suspend() uses the same method of suspending Lua execution in the interpretor as coroutines. You will get an error if you try to suspend a VM that is running compiled functions.
Again, the debugger used the same method of suspending the interpretor to enable you to place breakpoints in your code or step through it. The debugger will not work on functions that have been compiled.