Every program you run on a managed runtime — JavaScript in the browser, Python with PyPy, Java on the JVM — starts life as bytecode or source that is interpreted: the runtime reads each instruction and acts on it. Interpreters are flexible and portable, but they pay a cost per instruction that adds up fast in tight loops.
Just-in-time (JIT) compilation is the runtime's answer: instead of deciding everything at compile time, it watches the program while it runs, identifies the code that executes most (the "hot paths"), and compiles that code to native machine instructions on the fly. The result is that a JavaScript loop can eventually run at speeds approaching hand-written C — without you changing a line of source.
The trick comes with a catch. A JIT compiler must make assumptions about types and shapes to generate fast code. If those assumptions turn out to be wrong — say, a function that always saw integers suddenly receives a string — the runtime must deoptimize: throw away the compiled code and fall back to the interpreter until it has gathered enough new information to try again.
Understanding JIT means understanding this feedback loop: profile → compile → assume → deoptimize → repeat.
Comments
Loading comments...