3 minutes
Day 4: Autoloading Optimization in Composer
This article is part of my #100DaysOfCode and #100DaysOfBlogging challenge. R1D4
Today I’m studying the Autoloader Optimization article of the Composer documentation.
Purpose
The optimization is designed to reduce the performance impact of the autoloader. It is not intended for use in development as it will have undesired side-effects.
Optimization levels
There are two levels of optimization and the second level comes with two options. These two options cannot be combined.
- Optimization Level 1: Class map generation
- Optimization Level 2/A: Authoritative class maps
- Optimization Level 2/B: APCu cache
I will not get into detail, which level and options is doing what. The article is very brief and clear already.
Meat on the bone
Now, what does this actually look like in practice? To compare the differences, I will test it on an actual application. I will compare
- Level 0: No optimization -
composer install
, - Level 1: Class map generation -
composer dump-autoload -a
and - Level 2/A: Authoritative class maps -
composer dump-autoload -o
.
Create new project
To compare the different optimization levels any application utilizing the PSR-4 standard will do. I choose an empty Symfony application.
composer create-project symfony/website-skeleton my-project
Create autoloading with optimization levels 0, 1 and 2/A
The create-project
command will already execute an install
. The application has now no autoloading optimization and is ready for development. I copy the .\vendor\composer
folder to a different location. I then execute composer dump-autoload -o
and composer dump-autoload -a
and copy the .\vendor\composer
after each command execution.
What’s the difference?
I then compare the three copies:
Level 0
If no optimization is done, the autoloading is missing the class map which is generated for the other two options in autoload_classmap.php
and autoload_static.php
.
Level 1
Level 1 registers PSR-0 and PSR-4 directories to the Composer\Autoload\ClassLoader
. If a given class cannot be found, it falls back to look in the filesystem according to the registered rules.
.\vendor\composer\autoload_real.php
// [..]
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
// [..]
Level 2/A
Whereas Level 2/A does not register PSR-0 and PSR-4 directories and instead sets $classMapAuthoritative
in Composer\Autoload\ClassLoader
to true
.
Turns off searching the prefix and fallback directories for classes that have not been registered with the class map.
.\vendor\composer\autoload_real.php
// [..]
$loader->setClassMapAuthoritative(true);
// [..]
Conclusion
This study has helped me gain a better understanding of what is happening behind the scene in composer’s autoloading. Although everything is already very well documented in the article, I learn much better, if I take things apart.
So definitely in production.
composer install --no-ansi --no-dev --no-interaction --no-progress --no-scripts --classmap-authoritative