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:
- Named parameters
- Annotation
- Constructor property improvement
- Union type
- match expression
- null safe operator
- String comparison is more logical
- 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
- Enable Opcache
- 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 enabledopcache.enable=1
; Determines if Zend OPCache is enabled for the CLI version of PHPopcache.enable_cli=1
;开启**JIT**的debug; opcache.jit_debug=1
;默认是tracing,也可以function,官方推荐值是1255,含义可以参照上述 RFC 文档,里面有说明opcache.jit=1255opcache.jit_buffer_size=100MPerformance testing
The performance test file from the official website is used here: https://raw.githubusercontent.com/php/php-src/master/Zend/bench.php
php bench.phpTake a look at the performance results with JIT enabled and without JIT enabled:
# 开启 **JIT**simple 0.001simplecall 0.001simpleucall 0.001simpleudcall 0.001mandel 0.007mandel2 0.008ackermann(7) 0.011ary(50000) 0.006ary2(50000) 0.010ary3(2000) 0.011fibo(30) 0.026hash1(50000) 0.010hash2(500) 0.006heapsort(20000) 0.011matrix(20) 0.008nestedloop(12) 0.005sieve(30) 0.005strcat(200000) 0.004------------------------Total 0.131
# 未开启 **JIT**simple 0.015simplecall 0.008simpleucall 0.008simpleudcall 0.008mandel 0.061mandel2 0.085ackermann(7) 0.028ary(50000) 0.008ary2(50000) 0.009ary3(2000) 0.053fibo(30) 0.094hash1(50000) 0.011hash2(500) 0.011heapsort(20000) 0.031matrix(20) 0.031nestedloop(12) 0.040sieve(30) 0.016strcat(200000) 0.010------------------------Total 0.527The 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.