Just-in-time compilation
Adapted from Wikipedia · Discoverer experience
Just-in-time compilation, often called JIT, is a way computers turn code into instructions they can use while a program is running, instead of doing all the work before the program starts. This means the computer can look at the code as it runs and decide which parts should be turned into faster instructions to make the program work better.
JIT compilation mixes two older ways of getting programs to run: ahead-of-time compilation and interpretation. It tries to give the speed of compiled code with the flexibility of interpretation, but it also has some extra work that interpreters do, plus the work of compiling and linking the code.
This method is especially useful for dynamic programming languages because the computer can deal with changing data types and keep programs safe while they run. JIT compilation lets programs adapt and get even faster based on how the computer is set up.
History
The first known JIT compiler was created by John McCarthy in 1960 for the LISP programming language. Instead of saving the compiled code to punch cards, the code was translated during the program’s run time.
Later, Ken Thompson used JIT techniques in 1968 for pattern matching in the text editor QED. James G. Mitchell also explored JIT methods in 1970 with the experimental language LC².
Around 1980, Smalltalk introduced new JIT ideas, such as caching compiled code for later use and deleting it when memory was low. The Self language, developed by Sun, improved these methods and became very fast.
The term “Just-in-time compilation” became popular with Java starting 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, which 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, learn how the program is used, and make smart changes to the code while it runs. 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 may take a little longer to start because the computer needs time to prepare the code. The more work the JIT compiler does to make the code faster, the longer this starting time can be.
Some computers, like the HotSpot Java virtual machine, start by running the code slowly and then watch which parts are used often. These parts are then prepared to run much faster. This helps make the program quicker in the long run, even though it starts a bit slower. Different computers may choose to prepare code in different ways depending on what works best for the type of program being run.
Security
JIT compilation can create security challenges because it uses memory to run code directly. Normally, memory that can be changed should not run code, to keep systems safe. With JIT compilation, memory must be allowed to run code after it is set up, which needs careful handling to prevent problems.
Some types of attacks, like JIT spraying, try to use this feature to run unwanted code. This is why safety measures, such as those added in Firefox version 46 for JavaScript, are important to protect computers.
Uses
JIT compilation is used in some programs to make them run faster. For example, a text editor might compile a pattern at the same time it is used, so it can match faster. Many modern systems, like Java and .NET, use JIT compilation to run code quickly.
Often, programs are first turned into a special kind of code called bytecode. Then, JIT compilation changes this bytecode into machine code while the program runs. This makes the program faster than just interpreting the bytecode, but there can be a small delay while the code is being compiled. However, this delay is small because only parts of the program are compiled at a time, 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