Time Spent Autoloading PHP Classes

Autoloading classes takes a measurable amount of time in almost every PHP application. If autoloading takes too much time on average then there is performance optimiziation potential that can be tapped into:

  • Optimizing Composer Autoloading

  • Optimizing autoloading code

  • generated classmaps

  • configuration and filesystem optimizations

  • or preloading

are some options that can reduce the autoloading time.

Optimizing Composer Autoloading

Chances are you are using Composer and their autoloading. There are a few simple optimizations you can do when you use Composer.

The first optimization level is to generate a classmap. The classmap caches the filename each class is declared in, which will greatly reduce the overhead of autoloading. This is something everyone can do easily.

There are you two things you can during your deploy process to optimize the autoloader.

  • Call install or update commands with -o / --optimize-autoloader

  • Call dump-autoload command with -o / --optimize


composer install --optimize-autoloader

If you want or need to optimize autoloading performance with Composer even more then have a look into their two additional strategies:

Optimizing autoloading code

If you are using your own autoloader or an autoloader by a third party, then chances are that you can optimize it significantly by simplifying the code.

Complex logic in autoloaders is a primary source for significant performance degradation.

Use the Tideways Callgrah Profiler to get a picture of all function calls executed in a script and search for the node called [Autoloading]. All children of it are code that is executed during autoloading.

Generating Classmaps

If you must use your own autoloader consider generating a classmap to perform this task. See for example the phpab project for an implementation.

Configuration and Filesystem Optimizations

Using open_basedir or a slow filesystem such as NFS will make autoloading, loading and compiling code much slower.


PHP follows an execution model called "shared nothing" meaning on every request all scripts uses have to be loaded into memory from scratch. With preloading this process changes and you can load scripts and the classes and functions they contain into memory once and they are then shared across all requests.

As a downside the webserver startup gets slower and you have to restart the server whenever you change one of the preloaded scripts.

The exact benefit preloading has depends on your application but the maximum improvement you could theoretically achieve by preloading is the time spent in autoloading and compiling hooks of PHP.

PHP Manual: Opcache Preloading

Still need help? Email [email protected]