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

2

In the composer it is possible to auto-generate the autoload with the command composer dump-autoload .

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

Would you like to know the difference between them?

What is this optimization of Composer?

    
asked by anonymous 13.04.2016 / 14:22

1 answer

4

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

Source: link

When we do not use -o it keeps classes divided into files:

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

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

After using -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]

In this way, if you have many classes to load into a request this can help save certain executions that the SPL would do to find the file corresponding to the class, but it is worth noting that in some cases this may have an adverse side effect. expected.

    
13.04.2016 / 16:10