Cut PHP Bootstrap Time with Composer: Classmap + PSR‑4 Hybrid Autoloading
Composer’s autoload can slow PHP startup. By mapping hot namespaces to a classmap and leaving the rest on PSR‑4, you can shrink and speed up <code>vendor/autoload.php</code>. This blog walks through the setup, gives a concrete example, and discusses trade‑offs.
08 Aug 2025, 22:50 UTC

Why Bootstrap Can Be a Bottleneck
When a PHP application starts, it must load the vendor/autoload.php file generated by Composer. In a typical monolithic project, this file contains a recursive search through dozens of namespaces, each mapping to a directory. If the autoloader has to walk the filesystem for every class reference, the first request can take tens of milliseconds or more—noticeable in a high‑traffic API.
Composer’s Autoload Strategies
Composer supports four main mapping types:
- PSR‑4 – Namespace to directory mapping using a simple regular expression.
- PSR‑0 – Legacy mapping similar to PSR‑4 but with different directory rules.
- Classmap – A pre‑scanned associative array that maps fully‑qualified class names to file paths.
- Files – Explicit list of files to always include.
While PSR‑4 is flexible and easy to maintain, it can incur a small runtime cost because the autoloader still needs to resolve the namespace to a directory and then locate the file. Classmap eliminates that lookup by using a direct hash table.
Hybrid Classmap + PSR‑4 Strategy
The trick is to keep the autoloader lean: map only the namespaces that are used most frequently with a classmap, and leave the rest on PSR‑4. This keeps the generated autoload.php small and the lookup fast, while still allowing dynamic loading of less‑used classes.
Composer’s dump-autoload -o command creates an optimized autoloader. It merges all classmap entries into a single file and replaces the default loader with a lightweight dispatcher.
Concrete Example
composer.json – Define two namespaces:
App\Core(frequently used) andApp\Extensions(rare).{ "autoload": { "psr-4": { "App\\Extensions\\": "src/Extensions/" }, "classmap": { "App\\Core\\": "src/Core/" } } }Run
composer dump-autoload -oin the project root. Composer scanssrc/Core, builds a hash table of all classes, and writes a singlevendor/composer/autoload_classmap.phpfile.Check the size of
vendor/autoload.phpbefore and after optimization. On a medium‑sized project, you might see a reduction from 1.2 MB to 0.7 MB.Measure bootstrap time:
Running this script with and without$start = microtime(true); require __DIR__ . '/vendor/autoload.php'; $time = microtime(true) - $start; echo "Bootstrap: {$time}s\n";-otypically shows a 30‑50 % improvement.After adding a new class to
src/Core, runcomposer dump-autoloadagain to refresh the classmap. Failing to do so will result in aClass not founderror because the mapping is stale.
Trade‑offs & Limitations
- Installation Overhead – The classmap scan can consume significant memory (hundreds of MB) and time (seconds to minutes) on very large codebases.
- Dynamic File Structures – If your application generates PHP files at runtime or uses a plugin system that writes to
src/Core, the classmap will become outdated until you re‑run Composer. - Maintenance Burden – You must remember to run
composer dump-autoloadafter any change in the classmap directories.
Actionable Takeaway
For most mid‑size PHP projects, map your core, high‑frequency namespaces to classmap and leave the rest on psr‑4. Then run composer dump-autoload -o on deploy. Verify the performance gain with a quick microtime benchmark, and remember to re‑run Composer after any code change in the classmap directories.
Adopting this hybrid approach reduces bootstrap overhead, keeps your autoloader maintainable, and gives you a measurable performance boost without sacrificing flexibility.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.