Time Spent Compiling PHP Scripts

Compiling PHP scripts takes a measurable amount of time in almost every PHP application. This overhead can be reduced to nearly zero when using the Opcache extension. There are cases when compiling can still have a sizable effect on PHP performance that should be investigated, for example when using generated PHP scripts as cache or Opcache is not configured correctly.

The following problems could cause higher than necessary compile overhead:

  • Opcache is not enabled

  • Opcache is misconfigured

  • a very large script not stored in Opcache

  • Use of File-based PHP cache

Opcache is not enabled

A PHP application should never be run in production without Opcache. It is a central piece of PHPs architecture and performance story. It goes so far that PHP 7 made the use-case of not using Opcache slower by introducing an Abstract Syntax Tree (AST).

To see if Opcache is enabled look into phpinfo() for the section Zend Opcache that should show that its both up and running and enabled.

Zend Opcache Extension in phpinfo()

Opcache is misconfigured

There are few php.ini settings for Opcache that are central to optimal performance:

  • opcache.memory_consumption defines how much memory in MB is allocated to store compiled PHP scripts. If this number is too low then not all scripts might be stored in Opcache, leading to them being compiled over and over in every request they get used in.

  • opcache.max_accelerated_files defines how many files are stored in Opcache. If your application uses more than this number of files, then the one in excess are not stored in Opcache and are compiled in every request they are used.

  • opcache.interned_strings_buffer defines how much memory in MB is used for interned strings, which represent literal strings in code, filenames, classnames and many other symbols. As interned strings they are shared across all requests and processes and reduce the memory used and increase performance.

a very large script not stored in Opcache

If opcache.max_file_size is configured then large scripts may exceed this file size and not be stored in Opcache. This is especially tricky when your application generates large files of code.

Use of File-bsaed PHP Cache

If you are using a cache that stores entries into PHP files to make use of Opcache, then the opcache.max_accelerated_files setting can quickly become a problem if there are too many cache entries.

You can skip files from being stored into Opcache by using opcache.blacklist_filename. There is more information in the documentation.

Still need help? Email [email protected]