PHP Composer Autoloading takes forever
Asked Answered
V

1

12

Whenever I run composer update the 'Generating autoload files' part always takes forever (sometimes up to 2 full minutes). I've ran it with the --profile flag to pinpoint this issue.

I've read multiple articles on it, but I still can't understand why it takes this long on my projects, and if it's even useful for my projects? I'm running into this whenever I'm deploying changes live, and I want that deployment to be as fast as possible. I've tried to run it with --no-dev flag, but that doesn't change anything.

So my question is, why would autoloading take so long (and how could I improve its speed)?

Vanpelt answered 31/10, 2018 at 16:25 Comment(5)
How many packages are you using?Leak
Four in total, one that changes regularly, two that never change, and one that changes a couple times per year.Vanpelt
How many files/directories contains your project? It does not matter if it is PHP file or image.Medius
Uhm, very very much, in the thousands?Vanpelt
Composer scans all files in your project, it could take some time if the number of files is large.Medius
P
9

Depending on how your autoloader is configured in your composer.json file, composer can scan every single file in your project looking for classes. For big projects, this can take quite some time. Especially when using the classmap setting in composer. To speed up the autoload generation, you would want to use something like the PSR-4 autoloader, specifying a namespace prefix for your classes, but this might require some changes in your project depending on how your project and classes are structured.

Psalms answered 1/11, 2018 at 8:4 Comment(2)
This is useful information! I have a requirement in my composer file that checks if the CMS has updates. I think that's where the biggest loading time comes from then. Is there a way to exclude this from the autoloader? The CMS updates but a few times per yearVanpelt
Ok I found [this] (getcomposer.org/doc/04-schema.md#exclude-files-from-classmaps). if I add the /cms/ folder from /vendor/ in there, would that work? I tried it and it still takes a long time. Is there maybe a way to "profile" what the autoloader does? Or better still, do I really need the autoloader?Vanpelt

© 2022 - 2024 — McMap. All rights reserved.