PHP 5.5: Out-of-the-Box Bytecode Cache

Sebastian Bergmann |

PHP is an interpreted language. The default PHP runtime, the one that can be obtained from, compiles PHP sourcecode to an intermediate representation called PHP bytecode which is then executed. A bytecode cache stores this compiled representation of PHP sourcecode in shared memory. This eliminates the need to load and compile sourcecode on each request which leads to a significant increase in performance (up to 70% more requests per second).

The Past: PHP 4 to PHP 5.4

Since PHP 4 there have been third-party solutions for bytecode caching, both commercial (closed source) and open source ones. Zend Cache, one of the initial offerings of Zend Technologies Inc., was the first bytecode cache for PHP and originally released around 2000/2001. APC, the Alternative PHP Cache was one of the first open source solutions for bytecode caching. And although many PHP core developers contributed to APC over the years it still remained a third-party project.

A bytecode cache is integrated with the PHP runtime at a very low level. Thus, if the development of the bytecode cache is decoupled from the development of the PHP runtime itself there is a risk of "running out of sync". More often than not it took both the vendors of commercial products as well as the developers of open source solutions months to adapt their bytecode cache after a major new version of PHP had been released. In the past, this gap was a contributing factor to the slow adoption of new PHP versions.

The Present: PHP 5.5

At the beginning of this year, Zend Technologies Inc. open-sourced their bytecode cache product, then called ZendOptimizer+, and proposed to continue its development as part of the development of the runtime itself. Last week, the first stable version of PHP 5.5 was released. This is the first version of PHP to ship with an out-of-the-box bytecode cache. It is based on ZendOptimizer+, called OpCache, and shows significant performance improvements over APC (between 5% and 20% more requests per seconds).

When you download PHP 5.5 from and build the PHP interpreter yourself, OpCache is built by default but disabled by default. If you are still using PHP 5.2, PHP 5.3, or PHP 5.4, you can download and install OpCache from PECL. Once you have the OpCache extension installed, all you need to do in order to enable it is to add the following line to your PHP configuration file (php.ini):

The OpCache documentation suggests the following configuration settings:


In case your application (or the framework it is built on) only uses annotations at development time, you can achieve even better performance by setting opcache.save_comments=0 in your PHP configuration file.

Honoring the Separation of Concerns design principle, OpCache only implements bytecode caching whereas APC, for instance, provides both bytecode and data caching. If you want to migrate from APC to OpCache (which you should) but are using APC's data caching API in your code you need to refactor your code accordingly and use a different storage for your cache.

APCu is a fork of APC from which bytecode caching has been removed. It can be used as a drop-in replacement for APC's data caching API and requires no changes to your code. At the time of writing, however, it is only available in beta. Other alternatives to using APC for data caching are key-value stores such as Memcached.

The Future: PHP 5.6 and beyond

Starting with PHP 5.5, the development of the PHP compiler and its default bytecode cache is synchronized. New versions of PHP will always have robust bytecode caching available immediately after release. This eliminates a significant factor for the slow adoption of new PHP versions.

A deeper integration of OpCache and the compiler is planned for PHP 5.6 and beyond. This will, for instance, allow for more expensive optimizations of the bytecode in the compiler and improve the performance even further. It may well be that the bytecode cache will eventually not be an extension to the PHP runtime anymore but an integral part of it.

About the author

Sebastian Bergmann

Sebastian Bergmann is the author of PHPUnit and sets the industry standard of quality assurance.