TOP 10 … JUST JOKING PHP 8 AND JIT
Php 8 is coming soon. There are several changes from union types to named arguments but I’ll write about JIT in this article.
PHP, unlike some of the languages, is an interpreted language. When you run the script, it is compiled on run time. This means that server will work to compile the code every time script is run. With version 5.* OPCACHE came to our lives. In a nutshell, OPCACHE pre-compiles the code and fastens your php applications. In simple, what PHP does is;
- Tokenize
- Parse
- Compile
- Execute
- Send to CPU
If there is cached script with OPCACHE. It will be like this;
- Look at OPCACHE if a script is cached.
- Execute
- Send to CPU
But Opcode is not machine code either. OPCACHE still got work to do and in some occasions there are delays because of that.
And with PHP 8.0 JIT (Just In Time) is coming. JIT will prioritize OPCACHE code and will send pre-converted code straightly to CPU.
Below you can find a beautiful video from Zeev Zuaski about JIT’s performance. As I read from other articles, there are still questions if JIT will improve web applications’ performance with significant amount but on some level, I think, it will definitely help.
I will show you how to enable JIT but first let’s see the difference between ‘with JIT’ and ‘without JIT’. In my computer I ran a calculation based php script;
- Without JIT
- With JIT
COMPUTER SPEC
- Machine used : Intel Core i7–8700 PU 3.20 GHz
- Processors : 12
- Memory : 16 GB RAM
- OS : Ubuntu 18.04.5 LTS
- PHP Version: 8.0.
ab -n 200 -c 5 http://localhost:8000/
Below you can see REQUESTS PER SECOND. They all ran in my local computer so there are no live project examples in these scenarios.
I’ll leave this to here and won’t go deeper about other components or options. I installed PHP 8.0 and didn’t do anything to boost performance or anything else. I will attach some links below if you want to wander around and see other results and opinions as well.
SO ACTIVATING JIT
First of all, you must have php 8.0.
php -v
If you don’t have it, below you can find what to do. It is for Ubuntu by the way;
sudo apt install php8.0-common php8.0-cli -y
You can install some other extensions too but I won’t go into that. After this check the version again.
php -v
You must see that php is using version 8.* right now. Go into ‘/etc/php/8.0/cli/php.ini’ and change and add statements below.
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1
opcache.jit_buffer_size = 100M
opcache.jit=function
opcache.jit option can vary but I could not run it with other options like ‘tracing’ or numerical values. If you run it please write to the comments.
You can check if JIT is running;
var_dump(opcache_get_status()[‘jit’]);
You will see something like this;
array (size=7)
'enabled' => boolean true
'on' => boolean true
'kind' => int 0
'opt_level' => int 5
'opt_flags' => int 6
'buffer_size' => int 104857584
'buffer_free' => int 104850864
And that is it. You can definitely improve it, play with its options. If you find any useful things please write below.
As I said, I didn’t run it in a remote server or a live app. In my local environment there is almost %20 performance improvement with JIT. But this won’t be the case in every application and in every function. Some say it is not big of a deal, some say it will open new doors for PHP and I am in that second group. As we see from Zeev’s video, computational works will be more efficient and some never-thought application ideas will born.
Really good article:
Another one about the upgrade;