Skip to content

Composer optimizations for production: autoload, classmap, and cache

How to squeeze maximum performance from Composer in production using classmap generation, APCu cache, and deterministic install flags.

Every PHP request that touches an autoloaded class pays a filesystem lookup cost unless you tell Composer to pre-build a classmap. The difference between a naive PSR-4 autoload and an optimized classmap can shave 2–8 ms off cold requests — measurable under high concurrency even with OPcache warm. This article covers the autoload optimization flags, APCu-backed classmap caching, and the deployment practices that keep your vendor directory deterministic and your bootstrap fast.

How to verify

Check the current autoload mode and whether APCu is available:

# See the autoload mode currently compiled into vendor/
grep -A5 '"autoload"' vendor/composer/installed.json | head -10
# Check if the classmap is populated
wc -l vendor/composer/autoload_classmap.php

# APCu availability
php -m | grep apcu
php -r 'var_dump(apcu_enabled());'

# Time a representative cold autoload
php -r '$t=microtime(true); require "vendor/autoload.php"; printf("%.2fms\n",(microtime(true)-$t)*1000);'

What’s happening

Composer supports three autoload modes. PSR-4 (the default) resolves a class name to a filesystem path by mapping namespace prefixes to directories, then requires the file if it exists. This involves at least one stat() call per class per request when OPcache does not have the file in its realpath cache. Classmap precomputes a flat PHP array of ClassName => /path/to/file.php at deploy time — zero filesystem lookups, just an array key hit. APCu-backed autoload takes the classmap one step further: it stores the resolved paths in shared memory, so not even the array lookup happens after the first request.

The --optimize-autoloader flag (-o) generates the classmap from your PSR-4 and PSR-0 definitions. The --apcu-autoloader flag generates a classmap and wraps it in APCu caching with a configurable prefix. Both are additive to your existing autoload declarations — you do not change composer.json.

One critical detail: composer install --no-dev removes development packages from the vendor directory. If you run composer dump-autoload -o before --no-dev removes packages, the classmap will reference files that no longer exist. Always run dump-autoload after the install, or use the combined flags.

The procedure

  1. Install with the production flags in one command.

    composer install \
      --no-dev \
      --no-interaction \
      --prefer-dist \
      --optimize-autoloader \
      --no-progress \
      --no-scripts

    --prefer-dist pulls zip archives rather than cloning git repos — faster in CI. --no-scripts skips post-install hooks; run them explicitly afterward if required. --optimize-autoloader generates the classmap inline.

  2. If you need APCu-backed autoload, add the flag and a prefix.

    composer install \
      --no-dev \
      --no-interaction \
      --prefer-dist \
      --optimize-autoloader \
      --apcu-autoloader \
      --apcu-autoloader-prefix "myapp_v$(git rev-parse --short HEAD)"

    The prefix ensures that after a deploy the new classmap does not serve stale entries from the previous deploy’s APCu cache. Tie it to the Git SHA or a build number.

  3. Regenerate the classmap after a hotfix without a full install.

    When you patch a single file and do not want to re-run the full install cycle:

    composer dump-autoload --optimize --no-dev
    # Or with APCu:
    composer dump-autoload --optimize --apcu --apcu-prefix "myapp_v$(git rev-parse --short HEAD)"
  4. Configure OPcache to cache the generated autoload files.

    The autoload bootstrap (vendor/autoload.php) and vendor/composer/autoload_classmap.php must be OPcache-warm on the first request. Ensure they are included in your preload script if you use one:

    ; /etc/php/8.3/fpm/conf.d/10-opcache.ini
    opcache.preload = /var/www/app/config/preload.php
    opcache.preload_user = www-data
    <?php
    // config/preload.php
    require '/var/www/app/vendor/autoload.php';
    // Preload hot files here
    opcache_compile_file('/var/www/app/vendor/autoload.php');
    opcache_compile_file('/var/www/app/vendor/composer/autoload_classmap.php');
  5. Lock down the Composer cache directory in CI/CD.

    Composer maintains a download cache at ~/.composer/cache (or $COMPOSER_CACHE_DIR). Mounting this as a persistent volume in CI avoids re-downloading packages on every build:

    # GitLab CI example
    variables:
      COMPOSER_CACHE_DIR: "$CI_PROJECT_DIR/.composer-cache"
    cache:
      key: "composer-$CI_COMMIT_REF_SLUG"
      paths:
        - .composer-cache/
  6. Verify the classmap coverage after deploy.

    # Count classes in the classmap — should be non-trivial for any real app
    php -r '
    $map = require "vendor/composer/autoload_classmap.php";
    echo count($map) . " classes in classmap\n";
    '
    # Check that no class misses (PSR-4 fallback) are in the map
    grep -c "psr4" vendor/composer/autoload_real.php

    If autoload_real.php still references PSR-4 loaders, it means some packages do not declare classmap-compatible autoload entries. This is expected for some edge cases — Composer falls back gracefully.

Common pitfalls

  • Running dump-autoload before install --no-dev corrupts the classmap. The classmap is built from the files present on disk at the time of the dump. If dev packages are still installed, their classes appear in the map and then fail to load in production when the files are absent.
  • APCu prefix collisions between deploy versions. If two deploys share the same APCu prefix, the old classmap entries survive until APCu’s TTL expires or the PHP-FPM pool is restarted. Always embed a version token in the prefix.
  • --no-scripts skips required post-install steps. Some packages (Laravel, Symfony) rely on post-install scripts to copy config files or compile assets. Audit composer.json’s scripts.post-install-cmd and run the necessary steps manually in your deploy pipeline.
  • Classmap does not cover dynamically-generated class names. Code that uses class_alias(), eval-based class definitions, or string concatenation to build class names will not be captured in the classmap and will still trigger PSR-4 filesystem lookups.
  • Composer’s platform requirements check can block deploys. If your deploy server has PHP 8.2 but composer.lock was generated on PHP 8.3, composer install may fail with a platform mismatch. Pin config.platform.php in composer.json to the target server version.

A reproducible Composer deploy — locked, no-dev, classmap-optimized, APCu-backed — is one of the lowest-effort performance wins available on PHP applications. Stack Harbor’s managed operations service includes deployment pipeline reviews that cover Composer, OPcache, and PHP-FPM configuration as a unit.