Talk to Luajit

JIT

What is JIT
JIT = Just In Time Just-in-time compilation is a form of dynamic compilation and a technology that optimizes the operation of virtual machines.
There are usually two ways to run a program, one is static compilation, the other is dynamic interpretation, and just-in-time compilation mixes the two. This technique is used in both Java and .Net/mono.
However, it is forbidden in IOS (not for JIT, but all dynamic compilation is not supported)!

Why use JIT

Explanation and execution:

Low efficiency.
Code exposure.

Static compilation:

Not flexible enough to update hot.
Poor platform compatibility.

JIT:

Efficiency: higher than interpretation and execution, lower than static compilation.

Security: Generally, it will be converted to bytecode first.

Hot update: Both source code and bytecode are essentially resource files.

Compatibility: The virtual machine handles platform differences and is transparent to users.

How JIT is implemented
What I’m talking about here is actually a variant of JIT: adaptive dynamic compilation. It is divided into two types: Method JIT and Trace JIT.
As shown in the picture, this is the process of jvmjit:

Share a picture

In simple terms:< /p>

Trace hot functions or traces, compile them into machine code for execution, and cache them for later use.
Explanation and execution of non-hotspot functions.
Why only compile hot functions?
For code that is executed only once, interpretation and execution are always faster than JIT compilation and execution. It can be said that it is not worth the gain to do JIT compilation and then execution of these codes. For code that is only executed a small number of times, the increase in execution speed brought by JIT compilation may not be able to offset the overhead caused by the initial compilation. Only for frequently executed code, JIT compilation can guarantee positive benefits.

LuaJIT

vs. Lua
Lua is mainly composed of the following three parts:

  1. Syntax implementation.
  2. Library functions.
  3. Bytecode.

LuaJIT is mainly composed of the following four parts:

  1. Syntax implementation.
  2. Trace JIT compiler.
  3. Library functions.
    1. Native library++ (enhanced native library)
    2. bit
    3. ffi
    4. jit

    < /li>

  4. Bytecode

Note: The latest luajit corresponds to lua5.1.5.

trace jit compiler

share picture

It is roughly the same as jvmjit.
The so-called trace is a linear bytecode sequence. Hot-spot traces are compiled into machine code, and non-hot-spot traces are interpreted and executed.
Note: Not all code can be JIT. (NYI)

bytecode

Bytecode can basically be regarded as the instruction code of the virtual machine (“basically” because luajit uses uleb128).
Pros:

  1. Reduce file size.
  2. Generate function prototypes faster.
  3. Increase the difficulty of being cracked.
  4. Slight optimization of the source code.
  5. Library functions and executable files

The compilation steps are divided into three steps:

Share pictures

minilua: actually a subset of lua native code for execution Lua scripts and generate platform-related instructions.
buildvm: used to generate the mapping from opcode/library functions to assembly/C language for jit compilation.
lib
exec: can execute Lua code or convert bytecode.

Encoding

Command line execution

luajit –b  . 

The virtual machine judges whether it is bytecode, so no additional operations are required.
In addition, it can be mixed, that is: part of the file is compiled into bytecode, and the other part is kept as the source code.

iSO64-bit error report Cannot load incompatible bytecode! This error is because gcr is used in luajit to compare object pointers. In a 64-bit environment, there are only 47 valid values ​​(default user memory will not exceed 128T). Among the remaining 17 bits, 4 bits store the object type, that is, two pieces of information are stored in a section of memory. So there are some places in the function stack operation that need a null value placeholder. Because the bytecode directly reflects the function stack operation, 64-bit and 32-bit bytecodes are different.

luajit –b  . 

Leave a Comment

Your email address will not be published.