Optimizing Performance

LuaJIT produces very efficient compiled code in most cases with no tuning required. That said, the numerical computing performance guide contains guidelines that it's best to keep in mind if the algorithm's performance is critical.

If performance turns out to be unsatisfactory the first step is to investigate the cause of the problem. The easiest way to do so is to run again the problematic code with the -jv flag which displays information about the progress of the JIT compiler:

lua -jv problematic_script.lua

The problematic spots will be marked by "fallback to interpreter" preceded by hints about the issue.

Additionally, LuaJIT 2.1 bundles a profiler which can be used to identify where most of the computational effort is spent and optimize the code accordingly. The profiler is activated by the -jp flag (multiple profiling options are available via -jp=...):

lua -jp problematic_script.lua

Finally, LuaJIT uses a probabilistic approach to make decisions regarding how to compile (or not) chunks of code. Occasionally it might be required to tune the optimization parameters affecting this probabilistic approach. If everything else fails, It is suggested to try moderately increasing the -Oloopunroll and -Ocallunroll limits, especially when hinted so by the output of lua -jv, and check whether this solves the issue:

lua -Oloopunroll=60 problematic_script.lua

All of LuaJIT optimization settings can be changed via the -O flag as explained in the LuaJIT manual.