Composer optimization level 1
Asked Answered
S

1

6

On Composer's autoload optimization page:

Note: You should not enable any of these optimizations in development as they all will cause various problems when adding/removing classes. The performance gains are not worth the trouble in a development setting.

I can definitely see problems for the level 2 optimizations (authoritative class map) on a development environment, but I can't determine what the problems are for level 1 optimizations (class map generation) if I follow the PSR-4 standard.

  • If I add a class that didn't get generated in the class map, it will fall back to PSR-4 rules to look for the class.
  • If I refactor (move) a class to a different namespace, it will also not find it in the class map and attempt to resolve it using PSR-4 rules.

What are the potential issues with the generated class map on dev environment with a project that complies with PSR-4?

Sacramentalist answered 26/4, 2018 at 5:17 Comment(0)
A
0

Level 1 optimizations may create problems if you will move class to different directory without changing namespace. Since there may be multiple ways to resolve single namespace, such changes will be properly handled by Composer, but may fail when you have a outdated classmap.

Example:

"autoload": {
    "psr-4": {
        "app\\": "src",
        "app\\db\\": "src/drafts/db"
    }
},

Class app\db\Entity may be placed in src/drafts/db/Entity.php or src/db/Entity.php and in this order Composer will search for class file. Normally if you move a file from src/drafts/db to src/db Composer will finally find this class. But if you have outdated classmap, Composer will blindly include non-existing file and you will get a fatal error.

In addition to this apcu-autoloader option will also cache misses. So if you request a non-existent app/db/NewEntity class, and then add this class, Composer will not detect this change since it has cached info that this class does not exist.

These are generally edge cases, usually you will never notice such nuances. But this is still possible and unnoticeable performance boost on development environment is not worth the risk of losing a few hours on debugging cache-related issues of Composer autoloader.

Apricot answered 26/4, 2018 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.