Performance

Symfony is fast, right out of the box. However, you can make it faster if youoptimize your servers and your applications as explained in the followingperformance checklists.

Symfony Application Checklist

Production Server Checklist

Install APCu Polyfill if your Server Uses APC

If your production server still uses the legacy APC PHP extension instead ofOPcache, install the APCu Polyfill component in your application to enablecompatibility with APCu PHP functions and unlock support for advanced Symfonyfeatures, such as the APCu Cache adapter.

Use the OPcache Byte Code Cache

OPcache stores the compiled PHP files to avoid having to recompile them forevery request. There are some byte code caches available, but as of PHP5.5, PHP comes with OPcache built-in. For older versions, the most widelyused byte code cache is APC.

Configure OPcache for Maximum Performance

The default OPcache configuration is not suited for Symfony applications, soit's recommended to change these settings as follows:

  1. ; php.ini
  2. ; maximum memory that OPcache can use to store compiled PHP files
  3. opcache.memory_consumption=256
  4.  
  5. ; maximum number of files that can be stored in the cache
  6. opcache.max_accelerated_files=20000

Don't Check PHP Files Timestamps

In production servers, PHP files should never change, unless a new applicationversion is deployed. However, by default OPcache checks if cached files havechanged their contents since they were cached. This check introduces someoverhead that can be avoided as follows:

  1. ; php.ini
  2. opcache.validate_timestamps=0

After each deploy, you must empty and regenerate the cache of OPcache. Otherwiseyou won't see the updates made in the application. Given that in PHP, the CLIand the web processes don't share the same OPcache, you cannot clear the webserver OPcache by executing some command in your terminal. These are some of thepossible solutions:

  • Restart the web server;
  • Call the apc_clear_cache() or opcache_reset() functions via theweb server (i.e. by having these in a script that you execute over the web);
  • Use the cachetool utility to control APC and OPcache from the CLI.

Configure the PHP realpath Cache

When a relative path is transformed into its real and absolute path, PHPcaches the result to improve performance. Applications that open many PHP files,such as Symfony projects, should use at least these values:

  1. ; php.ini
  2. ; maximum memory allocated to store the results
  3. realpath_cache_size=4096K
  4.  
  5. ; save the results for 10 minutes (600 seconds)
  6. realpath_cache_ttl=600

Note

PHP disables the realpath cache when the open_basedir config optionis enabled.

Optimize Composer Autoloader

The class loader used while developing the application is optimized to findnew and changed classes. In production servers, PHP files should never change,unless a new application version is deployed. That's why you can optimizeComposer's autoloader to scan the entire application once and build a "class map",which is a big array of the locations of all the classes and it's storedin vendor/composer/autoload_classmap.php.

Execute this command to generate the class map (and make it part of yourdeployment process too):

  1. $ composer dump-autoload --no-dev --classmap-authoritative

  • —no-dev excludes the classes that are only needed in the developmentenvironment (i.e. require-dev dependencies and autoload-dev rules);
  • —classmap-authoritative creates a class map for PSR-0 and PSR-4 compatible classesused in your application and prevents Composer from scanning the file system forclasses that are not found in the class map. (see: Composer's autoloader optimization).

Learn more