-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
This bug report is not directly related to wp-cli core, but if affects all commands. That's why I decided on reporting it here, and if my suggestion is welcome, I can prepare pull requests for all the commands.
We have identified a performance issue in one of our projects related to the wp-cli commands and the way the composer autoloader handles them. Specifically, all wp-cli commands contain the following lines in the composer.json file:
"autoload": {
"psr-4": {
"": "src/"
}
},
The bug only affects performance if you install it into your project using Composer. When using the .phar version, this issue does not make any difference.
Defining an autoloader with an empty namespace prefix is detrimental to performance, as the autoloader will try to find classes within all the folders that were defined without a namespace.
This issue became apparent when activating the Yoast SEO plugin, as this has its own autoloader that gets registered after ours and it autoloads many files. As a result, our autoloader makes many roundtrips to the filesystem, causing a slowdown of 8 seconds per page load in our dev environment with Docker and mounted folders. In production (where everything is on the same filesystem) the slowdown is less noticeable, but still worth improving.
A short-term solution that should not cause any backwards compatibility issues is to replace the above fragment with the following:
"autoload": {
"classmap": [ "src/" ]
},
With the classmap key, a list of all found classes and corresponding files are added to the autoloader. Given that the commands each all have only one or two files, there should be no downside to this.
As I mentioned, I can prepare pull requests for all the commands if this change is accepted.
In the long-term, it would be a good idea to add namespaces to everything, but this is more likely to cause compatibility issues.