Speeding PHP Startup: Using Composer’s --optimize‑autoloader to Pre‑Build a Classmap
When PHP apps start up, Composer’s default autoloader can add latency. The <code>--optimize-autoloader</code> flag pre‑builds a classmap, cutting file lookups and speeding the first request. Learn how it works, when to use it, and how to verify the gains.
17 Apr 2026, 21:52 UTC

Why Your PHP App Is Slowing Down on First Request
When a PHP application boots, it must locate the file that contains the class you’re trying to instantiate. Composer’s default autoloader walks the namespace hierarchy, looking for a matching file on disk. For projects with dozens of packages and deep namespace trees, that walk can add noticeable latency to the first request.
The Composer Solution: --optimize-autoloader
Composer can replace the runtime namespace resolution with a static classmap. Running composer dump-autoload --optimize rewrites the autoloader to use a pre‑built map that points each fully‑qualified class name directly to its file path. The result is fewer file system lookups and a faster bootstrap.
How the Classmap Works
- Composer scans all
autoloadentries incomposer.json(PSR‑4, class‑map, files). - It builds an associative array:
\Namespace\ClassName => /path/to/file.php. - The generated
vendor/composer/autoload_classmap.phpis required once during bootstrap. - Subsequent
new \Namespace\ClassName()calls hit the map and include the file immediately.
When to Use It
- Production deployments where the codebase is static.
- Applications with many third‑party packages or deep namespace hierarchies.
- Environments where the first request latency is critical (API gateways, microservices).
Enabling the Feature
Open a terminal at your project root. You need
composerinstalled globally and filesystem write permissions forvendor/.Run:
composer dump-autoload --optimizeThis command rewrites
vendor/composer/autoload.phpto use the classmap and createsautoload_classmap.php.Alternatively, add to
composer.json:{ "config": { "optimize-autoloader": true } }Then run
composer installorcomposer updateand the classmap will be generated automatically.
Verifying the Impact
1. Check the file size increase:
ls -lh vendor/composer/autoload_classmap.php
A larger file indicates a populated classmap. For a medium‑sized project, you might see 1–5 MB.
2. Benchmark a simple script:
// bench.php
Run twice: once after composer dump-autoload without --optimize, then after with it. The optimized run should be noticeably faster (often 30‑50 % on large projects).
3. Confirm classmap validity after changes:
// Add a new class in src/Feature/Bar.php
Trade‑Offs and Limitations
- Memory & file size: A full classmap can be several megabytes. On low‑memory hosts, this may increase peak memory usage during bootstrap.
- Hot code changes: In a development environment where you add/remove classes frequently, you must run
composer dump-autoload --optimizeafter each change to keep the map accurate. Forgetting to do so will cause fatal class‑not‑found errors. - PHP 8.1+ improvements: Modern PHP engines have better autoload caching. For tiny projects, the performance gain may be negligible.
- File system impact: The classmap is static; if the underlying file system changes (e.g., moving files), the map becomes stale until regenerated.
Practical Checklist
- Run
composer dump-autoload --optimizebefore deploying to production. - Include the classmap file in your deployment artifacts.
- After any code or dependency change, regenerate the autoloader.
- Monitor startup times with a simple benchmark script to confirm the benefit.
- If memory usage becomes a concern, consider disabling optimization or using
--no-devto reduce the map size.
Conclusion
Composer’s --optimize-autoloader flag is a low‑cost, high‑impact technique for speeding up PHP application startup. By converting runtime namespace resolution into a static classmap, you reduce file system lookups and accelerate the first request. Just remember to regenerate the map whenever your code changes and weigh the memory cost against the performance gain. With a quick benchmark and a few deployment steps, you can bring measurable latency reductions to your PHP stack.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.