What is the difference between normal dump-autoload and optimized dump-autoload in Composer?

Asked

Viewed 11,573 times

5

In the Poser it is possible to automatically generate autoload using the command composer dump-autoload.

But it is also possible to generate an "optimized autoload" through the command composer dump-autoload -o.

I wonder what the difference is between them?

What is this optimization of Composer?

1 answer

6


The -o or --optimize converts the PSR-0/4 to the classmap to be able to get the classes faster in the autoloader. This is highly recommended mainly in production servers, optimization may take some time to further.

Source: https://getcomposer.org/doc/03-cli.md#dump-autoload

When we don’t use the -o it keeps divided the classes in the archives:

  • composer/autoload_files.php
  • composer/autoload_namespaces.php
  • composer/autoload_psr4.php
  • composer/autoload_files.php

And most objects are detected according to namespace-based PATH at runtime, so for each namespace class you will have to do path and file checking, but when using -o it maps all namespaces and classes directly to an array within the autoload_classmap.php, in a project I have with Lumen has ~45kb, that’s because yours have Phpunit and some other dependencies to the development environment.

Already after using the -o it gets ~206kb and has all "possible classes" already mapped:

<?php

// autoload_classmap.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'App\\Console\\Kernel' => $baseDir . '/app/Console/Kernel.php',
    'App\\Events\\Event' => $baseDir . '/app/Events/Event.php',
    'App\\Events\\ExampleEvent' => $baseDir . '/app/Events/ExampleEvent.php',
    'App\\Exceptions\\Handler' => $baseDir . '/app/Exceptions/Handler.php',
    'App\\Http\\Controllers\\Controller' => $baseDir . '/app/Http/Controllers/Controller.php',
    'App\\Http\\Controllers\\ExampleController' => $baseDir . '/app/Http/Controllers/ExampleController.php',
    'App\\Http\\Middleware\\Authenticate' => $baseDir . '/app/Http/Middleware/Authenticate.php',
    'App\\Http\\Middleware\\ExampleMiddleware' => $baseDir . '/app/Http/Middleware/ExampleMiddleware.php',
    'App\\Jobs\\ExampleJob' => $baseDir . '/app/Jobs/ExampleJob.php',
    'App\\Jobs\\Job' => $baseDir . '/app/Jobs/Job.php',
    'App\\Listeners\\ExampleListener' => $baseDir . '/app/Listeners/ExampleListener.php',
    'App\\Providers\\AppServiceProvider' => $baseDir . '/app/Providers/AppServiceProvider.php',
    'App\\Providers\\AuthServiceProvider' => $baseDir . '/app/Providers/AuthServiceProvider.php',
    'App\\Providers\\EventServiceProvider' => $baseDir . '/app/Providers/EventServiceProvider.php',
    'App\\User' => $baseDir . '/app/User.php',
    'Carbon\\Carbon' => $vendorDir . '/nesbot/carbon/src/Carbon/Carbon.php',
    'Carbon\\CarbonInterval' => $vendorDir . '/nesbot/carbon/src/Carbon/CarbonInterval.php',
     ...

     [total de 1815 classes mapeadas]

This way if you have many class to load in a request this can help save certain executions that the SPL would do to find the file corresponding to class, but it is good to note that in some cases this can have a side effect contrary to the expected.

  • 1

    Well, that’s important information. Because optimize It became vague: "Optimize what?"... Now you can understand

  • 1

    @Wallacemaxters managed to formulate an example :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.