If you haven't upgraded to PHP 8.0 or later yet, it's time to do it. PHP 8.0 was released on November 26, 2020 and brought the JIT (Just In Time) compiler, which is what we are going to talk about today.
Opcode
When you run a PHP script, PHP compiles it into an intermediate code called opcode (PHP Opcode). The opcode is then executed by the Zend virtual machine.
Here is an example of PHP code and its opcode:
PHP:
$a = M_PI;
$b = 10 + sin($a);
echo "Result: ", $b;Opcode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename: /in/ceTlX
function name: (null)
number of ops: 9
compiled vars: !0 = $a, !1 = $b
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
2 0 E > ASSIGN !0, 3.14159
3 1 INIT_FCALL 'sin'
2 SEND_VAR !0
3 DO_ICALL $3
4 ADD ~4 10, $3
5 ASSIGN !1, ~4
4 6 ECHO 'Result%3A+'
7 ECHO !1
5 8 > RETURN 1Basically, opcode is a sequence of instructions that the Zend virtual machine executes.
JIT
So where does the JIT come in? The JIT is a compiler that compiles opcode into native machine code. In other words, it turns opcode into machine code that the CPU can execute directly.
This way, we no longer use the Zend virtual machine to execute the opcode, but the native machine code generated by the JIT.
Enabling the JIT
The JIT is not enabled by default. To enable it, you need a few extra steps.
First, you need to install the opcache extension:
sudo apt-get install php8.0-opcacheThen, you need to enable the JIT in the PHP configuration file (php.ini):
[opcache]
opcache.enable=1
opcache.jit_buffer_size=100M
opcache.jit=tracing
opcache.jit=1205
opcache.enable_cli=1Here we are enabling the JIT and setting the JIT buffer size to 100 MB. The JIT can run in two modes: tracing and function. The tracing mode is the default and is faster, while the function mode is more precise.
To better understand the difference between the tracing and function modes, I recommend reading the official documentation.
Finally, we enable the JIT for the command line (opcache.enable_cli=1).
Benchmark
If you run PHP on the web, the JIT may not make much difference. But if you run PHP scripts on the command line, the JIT can bring a big performance gain.
So let's run a benchmark to compare PHP 8.0 performance with and without the JIT.
In the php-jit-doc repository you will find a PHP script that calculates a lot of things to keep the processor busy.
We won't go into the details of how this script is written or its patterns, since the focus is the JIT.
Let's run the script without the JIT:
./start.shThe result was:
Execution time: 6.2343468666077 seconds
Encrypted: 46d37684d6032ef0167c00d29f3358d5a180438d3e8604e4c11728699d78fdb0a32284bfa8234ed59f69e7febd0d23bebab983211565582a98da8cb5c8800e42Now let's enable the JIT and run the script again:
./start-jit.shThe result was:
Execution time: 3.292811870575 seconds
Encrypted: 46d37684d6032ef0167c00d29f3358d5a180438d3e8604e4c11728699d78fdb0a32284bfa8234ed59f69e7febd0d23bebab983211565582a98da8cb5c8800e42With the JIT enabled, the PHP script ran 3 seconds faster. That is a very significant performance gain.
But remember, the JIT is not a silver bullet. It may not make much difference for PHP scripts that run on the web. For PHP scripts that run on the command line, however, the JIT can bring a big performance gain.
Conclusion
This was a brief overview of the JIT in PHP 8.0. If you want to learn more about it, I recommend reading the official documentation.
I only brought basic, teaching examples so you can understand what the JIT is and how to enable it. I think it was important to cover this first so we can talk about more complex and advanced topics.
If you liked this content, leave a comment and share it with your friends. See you next time!
Comments