JIT compilation
JIT-компиляция
With just-in-time (JIT) compilation, a runtime turns code into machine instructions while the program is running. This can make frequently executed code run faster.
Also known as: JIT
What JIT compilation means
A program doesn't need to arrive with all its CPU instructions already generated. A runtime can start executing it and compile parts of it as it goes. That's just-in-time compilation. For example, javac normally produces bytecode before a Java application starts. HotSpot, a JVM implementation, can interpret that bytecode and later turn selected methods into native instructions. Saying “Java was already compiled” doesn't rule out JIT: the two stages produce different kinds of code.
How a runtime decides what to compile
Compiling takes time, so many runtimes spend that time on hot code: methods called repeatedly or loops that run for a long time. A runtime may start with an interpreter or a quick compiler and collect a profile along the way. That profile can tell it which branches are common or which implementation usually handles a method call. A tiered runtime can then spend more time optimizing code that's likely to run enough to repay the cost. Once generated, the native code can be reused rather than compiled again on every call.
Example: optimizing a call site
interface PriceRule {
int apply(int price);
}
static int calculate(PriceRule rule, int price) {
return rule.apply(price);
}
The signature of calculate allows any PriceRule implementation. But suppose a running service has only passed in instances of one class so far. HotSpot may inline that class's apply method: it puts the method body directly into the compiled caller. That can remove call overhead and expose more code to the optimizer. A runtime guard can check that the object is still of the expected class. If an assumption no longer holds, the runtime may take a general path or discard the specialized version and resume in less optimized code, a process called deoptimization.
Where it matters
HotSpot, .NET, and JavaScript engines such as V8 use JIT compilation. It's a choice made by the runtime, not a consequence of static or dynamic typing. Ahead-of-time (AOT) compilation moves native code generation before execution, avoiding that compilation work during startup. Adaptive JIT can instead use observations from the current run, at the cost of CPU time for compilation and memory for the generated code. Neither strategy guarantees better performance across all workloads.
How it comes up in interviews
A common question is why a benchmark's first few iterations aren't representative of later ones. Explain that the runtime may still be collecting profiles and compiling hot paths, but don't attribute every startup cost to JIT: class loading, cache population, and other initialization can matter too. If the goal is to measure cold-start latency, keep those early measurements. If the goal is steady-state throughput, warm the program up first and use a workload that resembles production.