What’s New in PHP 8.6: Performance, Operations, and Debugging Edition
Tech
This post gives you a different view on what is new in PHP 8.6, focusing only on performance, operations and debugging improvements.
Our colleague Tim worked on some of these improvements: 1. array_map to foreach in some circumstances 2. Faster argument passing to known constructors as well as provided valuable feedback, research and groundwork for other improvements.
Performance
We are looking at performance changes based on overall impact, individual impact greatly differs from application to application.
In our PHP 8.6 vs benchmarks, we could not see any measurable improvement of overall application performance with PHP 8.6 vs 8.5 and previous versions.
However, there are a large number of individual performance improvements in PHP 8.6 that can move the needle for you that we are going to look into this section.
New Polling API
By our estimate, the biggest performance impact from PHP 8.6 will be the new Polling API. It lifts a lot of the performance limitations of the stream_select() API that was required to use in PHP (outside of external extensions). You can now benefit from scalable epoll based I/O with little manual state management.
Major ZTS Improvements: Removed half the overhead compared to NTS
While PHP-FPM did not get faster, FrankenPHP will get faster overall. Thanks to the relentless optimization work of Marc Henderkes, the Zend-Thread-Safety (ZTS) mode of PHP got faster, which affects mostly FrankenPHP, Apache MPM event and Windows PHP builds as the major users of ZTS at the moment.
About half of the ZTS overhead compared to the “classic” NTS mode (PHP-FPM uses that) was removed. And Marc suggests that it’s possible to get this overhead down to nearly zero in the next version of PHP.
If that is actually the case, I would agree with him and argue that we should ditch NTS mode altogether, but that is for another blog post.
VM stack page cache on deep, fragmented call stacks
In specific circumstances, when a long running script was going deep into call stacks, the VM could run into an edge case where it allocates and deallocates a large number of stack pages, because it goes above and below thresholds all the time.
Ondřej Mirtes (PHPStan author) observed this with a specific repository, scanning with PHPStan.
The fix is keeping deallocated stack pages around in a free list and reusing them. For his specific use-case performance improved from 537 seconds to 51 seconds.
Stream TLS handshake reuse across requests
Similar to the cURL share improvements in PHP 8.5, you can now cache any OpenSSL TLS session for the use with streams using a stream context. As the RFC states this can often be 100ms.
<?php// Client: Resume previous session for faster reconnection$previousSession = $_SESSION['tls_session'] ?? null; $context = stream_context_create([ 'ssl' => [ 'peer_name' => 'api.example.com', 'session_data' => $previousSession ? OpenSSLSession::import($previousSession) : null, 'session_new_cb' => function($stream, OpenSSLSession $session) { $_SESSION['tls_session'] = $session->export(); } ]]); $fp = stream_socket_client('tls://api.example.com:443', context: $context);// Second connection will resume, saving ~100ms of handshake time
Individual PHP 8.6 function improvements with micro-benchmarks
A single release contains many small performance improvements that can have compounding effects on performance.
Measured application-wide they might not be noticeable, but if you use individual functions heavily in algorithms you will notice.
The following list is everything we found searching NEWS and UPGRADING documents as well as git history for performance improvements in PHP 8.6.
For each change we created our own micro-benchmark (source code on GitHub) using our hyperfine benchmarking approach described in a previous blog post. This way we can verify the results really are improvements on a neutral, dedicated machine and with optimal settings (Linux).
| Benchmark | PHP 8.5 | PHP 8.6 | % Change | Summary |
|---|---|---|---|---|
str_repeat_one | 1836.1 ms ± 9.2 | 36.7 ms ± 13.1 | −98.0% | str_repeat($s, 1) returns the input string without copying (#23681) |
str_pad | 3890.8 ms ± 49.1 | 98.3 ms ± 14.7 | −97.5% | Padding is filled with memset for one character and by doubling copies for longer pad strings (#19272, #23661) |
array_reduce | 736.2 ms ± 14.2 | 26.6 ms ± 8.7 | −96.4% | The array accumulator is no longer copied on each callback call, so the $carry[] = …; return $carry; pattern goes from O(n²) to O(n) (#21340) |
collator_sort_with_sort_keys | 2695.1 ms ± 13.2 | 139.1 ms ± 15.0 | −94.8% | Collator::sortWithSortKeys() sizes its buffers to the array instead of allocating 2 × 1 MiB per call (#23504) |
array_intersect | 1139.4 ms ± 15.5 | 61.7 ms ± 11.4 | −94.6% | Hash-based matching instead of sorting all arrays (#23019) |
zip_add_from_string | 266.4 ms ± 6.1 | 38.0 ms ± 1.1 | −85.7% | ZipArchive::addFromString() no longer copies the content (#20497) |
mb_str_pad | 634.3 ms ± 14.1 | 113.2 ms ± 7.4 | −82.2% | mb_str_pad() pads by doubling copies (#23667) |
addcslashes | 414.1 ms ± 13.1 | 85.5 ms ± 8.7 | −79.4% | Octal escapes are generated without a formatted print (#23680) |
str_split | 1491.0 ms ± 35.9 | 416.4 ms ± 4.5 | −72.1% | str_split() is optimized, especially for length 1 (#20419) |
printf | 1484.5 ms ± 31.9 | 540.3 ms ± 15.2 | −63.6% | printf() with only %s/%d is compiled into string interpolation (#19658); this only applies inside functions |
array_sum_product | 819.4 ms ± 50.8 | 356.7 ms ± 11.6 | −56.5% | Integer-only fast path for array_sum() / array_product() (#21787) |
first_class_callable | 1637.5 ms ± 41.9 | 833.2 ms ± 19.0 | −49.1% | First-class callables like foo(...) are cached instead of allocated each time (#19863) |
array_unshift | 776.6 ms ± 25.2 | 399.2 ms ± 14.0 | −48.6% | Fast path for packed (list) arrays (#20353) |
closure_static | 880.9 ms ± 15.4 | 487.2 ms ± 13.3 | −44.7% | Stateless closure cache for static fn closures without captures (RFC); this only applies inside functions |
pathinfo | 1028.3 ms ± 13.0 | 644.7 ms ± 16.2 | −37.3% | pathinfo() with a single PATHINFO_* flag skips the components that weren’t requested (#23726) |
sort_single_element | 982.1 ms ± 47.5 | 636.4 ms ± 7.6 | −35.2% | Sorting a single-element array skips unpacking and the comparator (#23550) |
asymmetric_visibility | 462.1 ms ± 19.3 | 310.1 ms ± 4.1 | −32.9% | The private(set) / protected(set) check is cached for writes (#22709) |
preg_replace_callback | 514.1 ms ± 13.8 | 416.2 ms ± 15.3 | −19.0% | Modifying $matches in the callback no longer copies the array (#21340) |
intval_binary | 857.3 ms ± 17.2 | 720.1 ms ± 18.1 | −16.0% | intval('0b…', 2) no longer copies the string to strip the prefix (0af87e970) |
dom_split_text | 321.1 ms ± 13.1 | 271.0 ms ± 13.3 | −15.6% | DOMText::splitText() is faster and uses less memory (#20371) |
array_fill_keys | 614.2 ms ± 53.2 | 537.3 ms ± 42.2 | −12.5% | array_fill_keys() optimized (#20347) |
new_known_constructor | 475.0 ms ± 14.0 | 432.9 ms ± 12.8 | −8.9% | Faster argument passing to constructors known at compile time, e.g. new self(...) (#20259) |
uri_rfc3986_getters | 435.2 ms ± 7.4 | 405.8 ms ± 10.4 | −6.8% | Getters on already-normalized Uri\Rfc3986\Uri objects no longer re-normalize (#21726) |
array_column | 446.4 ms ± 9.5 | 418.7 ms ± 9.3 | −6.2% | Key normalization moved out of the main loop (#23786) |
array_map_fcc | 422.6 ms ± 21.3 | 412.0 ms ± 17.0 | −2.5% | array_map(foo(...), $a) is compiled into a foreach loop (#20934) |
array_map_multiple | 589.7 ms ± 21.5 | 578.2 ms ± 2.8 | −1.9% | array_map() with several arrays avoids allocations on each iteration (79adb01bf) |
array_walk | 740.8 ms ± 9.8 | 729.8 ms ± 10.7 | −1.5% | Less callback overhead per element (#20322) |
stream_copy_to_socket | 847.9 ms ± 45.1 | 867.3 ms ± 48.8 | +2.3% | stream_copy_to_stream() from file to socket uses sendfile() (#20399); no difference in this run |
TAILCALL VM Improvements
The TAILCALL virtual machine (VM) is mostly used on macOS or with clang builds, where this improvement by Arnaud improved performance in Symfony by 1% and bench.php by 3.5%.
PHP runs in five different virtual machine kinds: CALL, GOTO, SWITCH, HYBRID and TAILCALL. These define how the engine jumps from one opcode handler to the next.
Operations
When operating PHP in production, there are a few things to know about that change in PHP 8.6.
- Stricter session configuration defaults:
session.use_strict_modenow defaults to 1,session.cookie_httponlyto 1 andsession.cookie_samesitetoLax. For applications using the ext/session core functionality, this hs the potential for breaking during an upgrade. - session file garbage collection now cleans nested subdirectories when
session.save_pathuses the dirdepth prefix (N;/path). Before, you needed your own cronjob for this. ini_get_all()reports built-in defaults. With$details = true, each directive now has abuiltin_default_valueelement. Comparing it with the current value shows configuration drift at runtime. This pairs well with 8.5’sphp --ini=diffcovered in my previous PHP 8.5 post.- OPcache file cache and system ID: Following up with PHP 8.5 changes we wrote about last year, a few fixes to the system ID computation were added in PHP 8.6.
Debugging
show error arguments that were passed with error_include_args
When the new error_include_args INI flag is enabled (which is off by default), then PHP will include function parameters in errors raised by functions.
This argument support respects existing backtrace argument behaviors, such as respecting zend.exception_string_param_max_len or the #[\SensitiveParameter] attribute.
This is a setting you could enable in development and should disable in production to avoid accidentally leaking sensitive information.
JSON errors include a location
json_last_error_msg() and JsonException now return messages like Syntax error near location 2:10 (line 2, column 10). This is a big help for debugging large payloads. As noted in the performance report, it originally slowed down the success path, and that cost was mostly removed again.
Structured errors in stream layer
You can pass a new error_mode option to a stream context now and modify the error handling of stream APIs to use exceptions instead of warnings.
This will greatly improve the code working with stream API, as you can now avoid having to use the silence operator @ which was still required to use in some cases.
$context = stream_context_create([ 'stream' => [ 'error_mode' => StreamErrorMode::Exception, ]]); try { $stream = fopen('/nonexistent/file.txt', 'r', false, $context);} catch (StreamException $e) { echo "Error: " . $e->getMessage() . "\n"; echo "Code: " . $e->getCode() . "\n";}
You should definitely read through the Stream Error Handling Improvements RFC from Jakub Zelenka in detail, as there are many interesting little changes that you could use in your streams code.
Additional Debugging Improvements
- New function
stream_socket_get_crypto_status()returnsSTREAM_CRYPTO_STATUS_WANT_READ/WANT_WRITEto help deciding on the next action during non-blocking TLS stream work. (#22193) - SOAP encoding errors improved and now name the affected type instead of “Violation of encoding rules”. (#22708)
- cURL option validation errors include the option name.
- Reflection and error-log output no longer truncate class and function names at NUL bytes, which matters for anonymous classes.
ReflectionProperty::isReadable()andisWritable()take visibility, hooks and readonly into account.ReflectionParameter::getDocComment()is new.- __debugInfo() magic method is now allowed on Enums
Disclaimer: This year we not only took our own meticulous notes following the PHP 8.6 commits and PRs, but also used AI to mine the NEWS, UPGRADING and git history for potential candidates and allowed us to reproduce benchmarks for all performance-optimizations. This gave us a much bigger set of topics to cover than the last two years. But the contents of the post itself were written by myself by reviewing and vetting the research results.
