PHP 8 JIT [most expected feature]

Coding (PHP 8)


We have heard about JIT for a long time and it almost among us, learn all there is to know about it
 
/img/blog/php-8-jit-most-expected-feature.jpg

Introduction

 

One of the most anticipated features of the upcoming PHP 8 is without any doubt JIT.

 

JIT is acrimonious of Just In Time, it is an approach that compiles portion of code at runtime.

Consequently, that compiled portion will be used.

 

To put it in simpler words, It would be like copying some code in memory while running it.

 

The scope of this is to be able to create faster applications.

 

In this article, you will discover how and most important when to use JIT.

 

 

 

 

A look into PHP 8 the series

 

The goal of this series is to get you ready to work with the latest version of this beautiful backend language.

 

The episode of the series are (in chronological order):

  • September's updates the collection of all the most important features and changes a month before they arrive at our machine 
  • New Features the list with explanation of all the new toys we will have in our arsenal
  • Changes Discover all the new improvements, syntax fixes and new features of PHP 8
  • JIT We have heard about JIT for a long time and it almost among us, learn all there is to know about it

 

Table of Content

 

Timeline
New features
    Syntax
        str_contains

        str_starts_with

        str_ends_with

        fdiv

        get_debug_type

        get_resource_id

        Union types

        Named arguments

        nullsafe operator

        Attributes, commonly known as annotations

        Constructor property promotion

        New mixed type

        Trailing comma in parameter lists 

    Operations

        JIT

        ext-json always available 

        Match expression

    Behaviours

        Throw expression

        New static return type 

        Alternative to get_class()

        Inheritance with private methods

        Weak maps

        Non-capturing catches

        Create DateTime objects from interface

        New Stringable interface

        Abstract methods in traits improvements

        implementation of token_get_all
Changes

    Consistent type errors

    Reclassified engine warnings

    The @ operator no longer silences fatal errors

    Default error reporting level

    Default PDO error mode

    Concatenation precedence

    Stricter type checks for arithmetic and bitwise operators

    Namespaced names being a single token

    Saner numeric strings

    Safer string to number comparisons

    Reflection method signature changes

    Stable sorting

    Fatal error for incompatible method signatures

 

 

Become a Patron!

 

 

Before PHP 8

 

Let’s start from the very beginning,

 

There are 2 types of programming language: 

 

  • interpreted 
  • Compiled

 

Languages as C, Java or Rust are compiled, it means that after you write you code you need to execute it,

 

When you do so, a compiler will take care of your application and return an error if that occurs.

 

PHP, on the other side, is an interpreted language, this means it is translated to machine code at runtime.

 

When you execute some PHP file 4 main stages happens on the background:

 

They are:

 

  • Tokenizing: the PHP interpreter scans the code and assembles a series of tokens.
  • Parsing: once these tokens are set, the interpreter starts to inspect whether the application follows the correct syntax rules and use the tokens of the language properly while doing that it creates a rendering of the structure of the source code, called AST
  • Compilation: the next step is to convert the AST nodes into Zend opcodes (more of this below), 
  • Interpretation: these opcodes are interpreted and run on the Zend Virtual Machine.

 

So as you have seen above the code is compiled into a series of instruction.

 

These instructions are called opcodes and are comprehensible by the virtual machine,

 

A little note about the latter:

 

PHP comes with a virtual machine named the Zend VM. 

 

The opcode you were reading above is a low-level programming language, it is way closer to the machine language than PHP.

 

The format of these opcodes is numeric identifiers determining the type of instruction performed by the Zend VM.

 

This is translated in faster interpretation when compared to other languages.

 

These opcodes are then executed by the Zend VM in the runtime stage.

 

This part of the execution is called compile time.

 

What part plays JIT in all of this?

 

You need to look at JIT as an independent part of OPcache.

 

Almost like an extension to cache, the opcodes works so that compilation occurs only when it is required. 

 

JIT will consider the instructions generated for the Zend VM as the intermediate representation. 

 

PHP will generate architecture-dependent machine code, to make the host of the code the CPU rather than the Zend VM.

 

What is JIT?

 

Sorry about the long introduction but some explanation was due, 

 

I am sure that even among the more expert reader here, there was something that they did not know.

 

now, JIT!.

 

Let talk about the main reason you are reading this article,

 

What is JIT and why this is very important for the future of PHP?

 

The RFC to add JIT to PHP was opened by Dmitry Stogov,

 

The goal of the RFC is to have JIT working as a cached version of the interpreted code, always considering that it is generated at runtime.

 

JIT is implemented as an independent part of the OPcache.

 

Another relevant characteristic is that JIT could be enabled or disabled using different methods.

 

They would be a compile time-and at runtime.

 

Consider JIT to work as a guard,

 

This guard looks at the code while it runs, this JIT senses parts of the code that are re-executed.

 

It will set the parts as warm and hot depending on how frequent these snippets are executed.

 

These hot bits of code can be compiled as machine code and reused on the fly rather than use the real code.

 

This is the reason the JIT compiler is supposed to improve the performance of your software, by a lot.

 

When JIT is enabled the native code of the PHP files are going to be saved in a sector of the OPcache shared memory and the op_array will retains pointers to the entry point of the JIT code.

 

The mind-blowing point of this is that this procedure does not require any engine adjustment.

 

In theory, this new feature is simply amazing,

 

And that is the reason the idea to implement JIT in PHP started with PHP 7.

 

In fact, Dmitry Stogov started this project back in 2011.

 

Since then he and other core developers have tried to implement JIT 3 times.

 

The problems that the implementation of JIT would cause can be resumed in 3 motives.

 

The first one is that, as you will see below, there is not really any considerable improvement in the performance on web apps (and PHP is mainly used as a language for developing in the web).

 

Next, we need to take into account that this feature is incredibly hard to maintain and the PHP core team need someone that can work with this low-level language constantly in-house.

 

JIT, in fact, generates machine code, I do not need to remind you how complicated this content is for a 'higher level programmer' to understand.

 

That is also because if you have machine code as output, 

 

It is difficult to debug errors thrown in the PHP's JIT compiler.

 

It is logical so to have someone in charger of the maintaining of this feature,

 

A good point is to train people about how the compiler works, on this, we go straight to the latest reason.

 

Especially in the last couple of years, we have seen that PHP is in continuous evolution.

 

Which means there could be different patches the team could have been followed to improve performance and maybe get a better return on the time invested than the one spend while working on JIT.

 

These above where all reason to discard the JIT project altogether,

 

In 2020 the situation is a bit different.

 

Currently, instead, PHP 7 has been updated consistently every year and the core team does not believe there is more room for improvement rather than the use of JIT.

 

 

Why is JIT useful?

 

Adding JIT among the tools a PHP developer has at his disposal supply the possibility to develop built-in function in PHP, rather than doing so using C.

 

This means that won’t be the performance disadvantage related to this problem.

 

This also means that it would be possible to create a more secure implementation and innovate the code faster.

 

The reason being these type of improvements are prone to rely on memory management, overflows and other C-based issues.

 

As you might have imagined already, implementing JIT causes a lot of calculation to take place over time.

 

The problem is, as I wrote above, that PHP is mostly used for coding application on the web.

 

These types of application are not famous for having a lot of hot code that deal with the requests.

 

Why does it matter?

 

Because since JIT works on a hot and warm bit of code the improvement is not going to be as significant.

 

However this argument does not mean that JIT is useless and we should care about it, 

 

Actually, quite the opposite.

 

Implementing it is going to create an opportunity for PHP outside of the web realm, (machine learning anyone?)

 

Also, you need to consider that this will be the first implementation of JIT, and other improvements will surely be on their way with PHP 8.1 onwards.

 

 

How to use JIT

 

You can enable JIT in your machine by specifying the size of the buffer of the Opcache option in your php.ini file.

 

Make sure the directive is included because the default value is 0 and JIT will not work.

 

Here is an example:

 

opcache.jit_buffer_size=100M

 

Another directive you should set is the mode of JIT, it determines how JIT will monitor to the hot parts of the code.

 

There are 2 option here, the first one is function the second one is tracing.

 

opcache.jit=function
opcache.jit=tracing

 

 

Once all of this is done, you can verify if you are running by using the PHP function opcache_get_status()

 

Just like the example below:

 

die(var_dump(opcache_get_status()['jit']));

array:7 [▼
  "enabled" => true
  "on" => true
  "kind" => 5
  "opt_level" => 4
  "opt_flags" => 6
  "buffer_size" => 104857584
  "buffer_free" => 104478688
]

 

If the enabled key is positive, you should see a performance improvement straight away.

 

 

What does the future hold for JIT?

 

As I repeated a few time over this article probably JIT will not be a feature as groundbreaking as we imagine but, it opens the door for many new implementations.

 

It will allow us to do the thing we could not even think were possible in PHP.

 

All the crazy calculation needed for programming machine learning, AI, IoT and other fancy stuff like that. 

 

The future is still opaque because we still do not know what we will be able to do with it.

 

But this is not the point,

 

That is ok if JIT do not provide any meaningful improvement in the short-term.

 

What is important for me, and it should be for you as well, is that PHP has now a lot of opportunities to become a growing programming language.

 

If I think a few years back many considered it as a dead language.

 

If you want to learn more about JIT here is a crash course created by the guy at Mozilla

 

 

 

 

That's it for now, 

 

The series regarding PHP 8 is over,

 

I have been trying to explain as much as possible in the easiest way.

 

Hope you enjoyed this bonus article all about JIT.

 

This year we are going to see a lot of improvement, amazing features added to the language and as always significant changes.

 

PHP is alive, now more than ever,

 

This update will demonstrate it.

 

If you had enough about all these updates and you just want to brush up some basics her is the article for you PHP BASICS

 

If you want to challenge yourself with something more advanced why don't you try with some design patterns

 
 
If you like this content and you are hungry for some more join the Facebook's community in which we share info and news just like this one!

Other posts that might interest you

Coding (PHP 8) Oct 1, 2020

PHP 8 Changes [what is next?]

See details
Coding (PHP 8) Nov 13, 2022

The New Match Expressions in PHP

See details
Coding (PHP 8) Nov 13, 2022

How to fix the PHP fatal error: allowed memory exhausted?

See details
Get my free books' review to improve your skill now!
I'll do myself