Just-in-time compilation
Adapted from Wikipedia · Adventurer experience
Just-in-time compilation, often called JIT, is a way computers change code into instructions while a program is running. This happens instead of doing all the work before the program starts. This lets the computer see the code as it runs and pick the best ways to make the program faster.
JIT compilation mixes two older ways to run programs: ahead-of-time compilation and interpretation. It tries to give the speed of compiled code with the flexibility of interpretation. It also does extra work that interpreters do, plus the work of compiling and linking the code.
This method works well for dynamic programming languages. The computer can handle changing data types and keep programs safe while running. JIT compilation helps programs adapt and get faster based on the computer's setup.
History
The first known JIT compiler was made by John McCarthy in 1960 for the LISP programming language. The code was translated while the program was running, instead of saving it to punch cards.
Later, Ken Thompson used JIT techniques in 1968 for pattern matching in the text editor QED. James G. Mitchell also looked at JIT methods in 1970 with the experimental language LC².
Around 1980, Smalltalk brought new JIT ideas, like saving compiled code for later use and removing it when memory was low. The Self language, made by Sun, improved these methods and became very fast.
The term “Just-in-time compilation” became popular with Java in 1993. Today, most Java systems use JIT compilation. An experimental project called Dynamo by HP also used JIT to improve speed.
In November 2020, PHP 8.0 added a JIT compiler, and in October 2024, CPython introduced an experimental JIT compiler.
Design
In a system where code is first turned into bytecode, the bytecode is a special kind of code that can work on many different computers. This bytecode is usually run by a program called a virtual machine.
A Just-in-time (JIT) compiler takes this bytecode and changes it into the actual computer code right when the program is running. This makes the program go faster. This can happen for small pieces of code or bigger parts, and the compiled code can be saved for later use.
Unlike regular interpreters that just read the bytecode and run it, which is slower, JIT compilation makes the program run much faster. It can even sometimes be faster than code that was compiled before the program started. This is because JIT can make the code better for the specific computer it is running on. However, JIT needs the computer to allow running new code, which isn’t possible on all systems.
Performance
When a program uses just-in-time (JIT) compilation, it might start a bit slower because the computer needs time to get the code ready. The more the JIT compiler works to speed up the code, the longer the start-up can take.
Some computers, like the HotSpot Java virtual machine, start by running the code slowly and then notice which parts are used most often. These parts are then made to run much faster. This helps the program run quicker over time, even if it starts a little slower. Different computers might prepare code in different ways, depending on what works best for the program.
Security
JIT compilation can make computers less safe because it lets code run in memory that is normally not allowed to run code. This needs careful handling to avoid problems.
Some attacks, like JIT spraying, try to use this feature to run unwanted code. That is why safety measures, such as those added in Firefox version 46 for JavaScript, are important to keep computers protected.
Uses
JIT compilation helps programs run faster. For example, a text editor can compile a pattern when you use it, so it works quicker. Many modern systems, like Java and .NET, use JIT compilation to speed up code.
Often, programs are first changed into a special kind of code called bytecode. Then, JIT compilation turns this bytecode into machine code while the program is running. This makes the program faster than just interpreting the bytecode. There might be a small delay while the code is compiled, but it is usually very small because only parts of the program are compiled at once, not the whole thing.
Related articles
This article is a child-friendly adaptation of the Wikipedia article on Just-in-time compilation, available under CC BY-SA 4.0.
Safekipedia