What’s new in PHP 8.4 in terms of performance, debugging and operations

The close of 2024 is near, and that also means a new version of PHP is about to be released: 8.4!

There has already been some discussion regarding the latest features and modifications affecting developers, for example on either stitcher.io or php.watch

We wrote this post with a totally different angle, highlighting the performance, debugging, and operations-related changes in PHP 8.4 that are usually less publicized.

Several of these changes were even contributed by Tideways.

SHA-NI for SHA-256

Cryptographic algorithms can be much faster when they are implemented directly on the CPU as instructions. This is what the SHA Extensions (SHA-NI) are for SHA-256.  

With this PR, Tim added SHA-NI support for the SHA-256 hash algorithm to PHP core, basing it on the excellent work in the tarsnap/libcperciva library.

This improves performance by a factor of 2x to 5x, which is a fantastic gain, making SHA-256 even faster than some of the less secure alternatives, allowing to improve performance and security at the same time.

#[Deprecated] Attribute

As of PHP 8.4, you can add #[\Deprecated] to any function, method or class constant and when they are used a deprecation message is triggered through the standard PHP error mechanism. 

Use of this attribute looks like this:

It nicely integrates into the Tideways deprecation tracking:

Screenshot PHP 8.4 performance

sprintf()-Compile Optimization

If you use just the %s and/or %d symbols in a sprintf format string, then starting with PHP 8.4 this is compiled down into the equivalent string interpolation.

We wrote a blog post on this optimization.

Property Hooks: Alternative to getter/setter methods?

The headline feature of PHP 8.4 is certainly property hooks and this zend.com blog post includes all the details.

From a performance POV, we asked ourselves, is using property hooks that much faster than getter/setter methods that we can get rid of them

Lazy Objects

Sometimes an object represents a network resource and its creation already takes a significant amount of time, even when it’s not used in the end. This is often the case with record or entity objects representing database rows. 

PHP 8.4 ships a new feature “lazy objects” that allows “shallow” creating an instance of an object that is only initiated when it’s actually accessing and using any properties.

A database library such as Doctrine ORM will greatly benefit from this as this can be implemented in PHP code, but it’s quite slow and complicated. Now, this kind of code is as simple as:

This is a pretty advanced feature and will probably be used mostly by libraries, but it can really provide some pretty powerful optimizations.

get_browser() Improvements

Niels Dossche provided a patch that improves the performance of get_browser() significantly. Depending on the use-case it will be 50%-250% faster than in PHP 8.3.

We wrote about how slow get_browser() is in this recent blog post comparing it to two userland libraries.

Frameless Internal Functions

Ilija Tovilo implemented a performance improvement that will benefit all PHP applications. With frameless internal functions, a few commonly used functions do not need a frame in the stack, which are expensive to set up.

Closure Naming in Stacktraces

With PHP 8.4, ambiguous naming of closures in stack traces has been improved. This is helpful for both debugging, but debuggers and profilers such as Xdebug and Tideways benefit as well by being able to re-use these names, reducing overhead slightly.

See our blog post about the closure naming change for details.

An AI would never be able to dive this deep into technical topics surrounding PHP performance! Follow us on LinkedIn or X and subscribe to our newsletter to get the latest posts.

Let’s dive in and find out what is causing performance bottlenecks! Without Tideways, you’re likely to fish in murky waters attempting to figure it out. Try our free trial today and be enlightened.

Related Posts

Benjamin Benjamin 19.11.2024