This article is geared toward those familiar with PHP, or with scripting languages and how they interact with the hardware upon which they run.
Sometime in November 2015, the PHP Group will be releasing PHP 7. This release promises greater performance and some new tools to further improve responsiveness. This article takes a quick look at how PHP’s developers accomplished these improvements and what they will mean for the future of PHP.
PHP 7 Performance Tweaks
The PHP 7 development team focused on performance optimization using real world web frameworks and software as a guide. PHP inventor Rasmus Lerdorf recently discussed the development team’s efforts at frOSCon 2015. One of the team’s first tasks was to increase the speed of PHP by reducing both the amount of time and instructions needed for requests against WordPress–one of the most common and popular PHP applications online. Some of their accomplishments during this time include:
- Much more CPU cache friendly
- New memory allocator similar to jemalloc
- Faster hashtable iteration API
- Array duplication optimization
- Fast ZPP (ZendParseParameters) implementation
- Faster stack-allocated zvals (instead of heap)
- Optimized VM calling
- Global register variables with gcc 4.8+
- zval size reduced from 24 to 16 bytes
- Hashtable size reduced from 72 to 56 bytes
- Hashtable bucket size reduced from 72 to 32 bytes
- Immutable array optimization (from 43M of memory down to 6M of memory).
Much of this list taken from Lerdorf’s talk in Santa Clara on Speeding up the Web with PHP7.
Lerdorf also presented benchmarks showing significantly increased performance in Drupal 8, WordPress 4.1.1, phpBB 3.1.3, MediaWiki 1.24.1, OpenCart 22.214.171.124, Wardrobe CMS 1.2.0, Geeklog 2.1.0., and many others. Lerdorf reported performance improvements in some applications was double that of PHP 5.x in his benchmark tests–with the caveat that replicating these results depends on the system on which PHP 7 runs.
Introducing the Secondary File-Based Cache
PHP 7 introduces a major performance improvement by augmenting the OPCache extension (bundled into PHP >=5.5.0) with a secondary, persistent file-based cache. The OPCache extension works by storing the OPCodes generated by the compiling of a PHP script in shared memory. When the script executes a second time, it can load the OPcode file from shared memory into OPCache and then execute the file. This process is substantially faster than recompiling everything. In PHP 7, users will have the option to enable a secondary file-based cache in addition to the shared memory cache. If a user enables the secondary file-based cache, then the system running PHP will have a fallback alternative location on disk for already generated OPCodes; it will only consult this secondary cache if the OPCodes have been flushed from shared memory. If the OPCode file that matches the script is in this new file-based cache, it will load right back into OPCache for the script to finally execute.
How Does The New Secondary Cache Increase Performance?
This feature will be mostly beneficial to performance on high traffic websites. When these servers need to be restarted, their memory cache is cleared, so everything has to be recompiled. With the addition of the secondary OPCache on disk, PHP can grab the OPCode from the new secondary file-based cache instead of having to recompile everything. It’s important to remember that this isn’t as fast as getting the memory cache from the shared memory. To compare the speeds, consider the time it takes to read an uncompiled PHP index file as the base speed. It is 4 times as fast to run from the disk than from the index file, and 10 times faster to run from the shared memory OpCache than it is from the index file. So, while there is still a performance hit upon server restarts, the secondary cache still executes much faster than running from the index file.
Future Updates to PHP 7
Absent from PHP 7 is a JIT (Just in Time) compiler, which could greatly improve PHP 7 performance. The developers have stated that they are working on JIT to be included in a future update to PHP 7. As it stands right now, PHP 7 is written with the intent to eventually support a migration to a JIT compiler.
Another potential update we could see in PHP 7 is support for asynchronous programming. This ability will allow code to perform tasks such as accessing files, networks, timers, and databases through the use of a PHP event-loop.
Thank you for reading, and good luck as you migrate your code to PHP7. Check out our how-to’s on installing PHP7 and try it yourself (CentOS, Debian, Ubuntu). To learn more about some of the other new things to expect from PHP7, check out some of our other articles: