PHP 8 New Features

PHP

The official version of PHP 8 has been released for some time, and the corresponding frameworks and supporting components have begun to support it. I also played with it to briefly record the features of PHP 8.

First of all, PHP 8 is a milestone version, and the official website has a dedicated landing page to introduce it:

Portal: Official website PHP 8 New Features Introduction Page

Here is a brief summary of the key points mentioned on the page:

  1. Named parameters
  2. Annotation
  3. Constructor property improvement
  4. Union type
  5. match expression
  6. null safe operator
  7. String comparison is more logical
  8. JIT

Because the official website page is very clear, I recommend that you go directly to the official introduction page. Here is a very important feature that is not explained in detail on the official website: JIT

JIT Introduction

JIT (Just-In-Time) Just-in-time compilation is one of the important new features in PHP 8. It can compile all or part of the frequently called parts of PHP program execution into CPU-executable machine code and hand it over directly to the CPU for direct execution. In this way, Zend VM and its processes are bypassed, thereby improving performance.

It sounds a lot like Opcache. Yes, if this thing is turned on, it must have Opcache support. If you understand the principle of PHP code execution, you still know the difference.

Let’s look at the flow chart below:

┌────────────────┐
│ php code │
└───────┬────────┘
┌──────────────────▼────────────────────┐
│ │
│ Opcache │
│ ┌────────────────┐ │
│ │ │ │
│ │ JIT Buffer │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ Machine Code │ │
│ └─────▲───────┬──┘ │
│ Op Codes │ │ │
│ │ │ │
└────┬───────────┬────────┼───────┼─────┘
│ │ │ │
│ │ │ │
│ ┌──▼────────┴────┐ │
│ │ JIT Compiler │ │
│ └────▲───────────┘ │
│ │ │
│ │ │
┌────▼─────────────┴─────┐ │
│ Zend Virtual Machine │ │
└───────────┬────────────┘ │
│ │
│ │
│ │
┌──────────▼─────────────────────▼──────┐
│ │
│ CPU │
│ │
└───────────────────────────────────────┘

PHP JIT is implemented as part of Opcache.

This is also stated in the RFC on the official website: https://wiki.php.net/rfc/jit

For more detailed JIT principles, you can also see the above RFC. I won’t go into details here. Let’s take a look at how to configure and use it.

Since it is part of Opcache, to enable JIT you must enable Opcache.

Configure JIT

  1. Enable Opcache
  2. Add JIT configuration items (as of the current date, the default configuration file is not specified in the configuration file)

The key configuration is as follows:

[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1
; Determines if Zend OPCache is enabled for the CLI version of PHP
opcache.enable_cli=1
;开启**JIT**的debug
; opcache.jit_debug=1
;默认是tracing,也可以function,官方推荐值是1255,含义可以参照上述 RFC 文档,里面有说明
opcache.jit=1255
opcache.jit_buffer_size=100M

Performance testing

The performance test file from the official website is used here: https://raw.githubusercontent.com/php/php-src/master/Zend/bench.php

Terminal window
php bench.php

Take a look at the performance results with JIT enabled and without JIT enabled:

Terminal window
# 开启 **JIT**
simple 0.001
simplecall 0.001
simpleucall 0.001
simpleudcall 0.001
mandel 0.007
mandel2 0.008
ackermann(7) 0.011
ary(50000) 0.006
ary2(50000) 0.010
ary3(2000) 0.011
fibo(30) 0.026
hash1(50000) 0.010
hash2(500) 0.006
heapsort(20000) 0.011
matrix(20) 0.008
nestedloop(12) 0.005
sieve(30) 0.005
strcat(200000) 0.004
------------------------
Total 0.131
# 未开启 **JIT**
simple 0.015
simplecall 0.008
simpleucall 0.008
simpleudcall 0.008
mandel 0.061
mandel2 0.085
ackermann(7) 0.028
ary(50000) 0.008
ary2(50000) 0.009
ary3(2000) 0.053
fibo(30) 0.094
hash1(50000) 0.011
hash2(500) 0.011
heapsort(20000) 0.031
matrix(20) 0.031
nestedloop(12) 0.040
sieve(30) 0.016
strcat(200000) 0.010
------------------------
Total 0.527

The overall performance difference is roughly about 4 times, and the performance improvement is quite considerable.

Summary

PHP 8 As a milestone version, it has brought improvements in performance, syntax usage and strong type usage. To quote a sentence from the official website: better performance, better syntax, and better type safety.