-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[9.x] Add support for lazy loading commands #34873
[9.x] Add support for lazy loading commands #34873
Conversation
@paras-malhotra it seems like we could make the |
@taylorotwell the |
Thanks. Does it make sense to add a defaultName to some of Laravel's own heavier commands? |
Yes @taylorotwell, I'll send across a follow-up PR to add default names to the commands shipped with Laravel. That should be a nice performance boost. |
@paras-malhotra does this actually work? It still has to resolve all of the console commands in order to get the name: $this->artisan = (new Artisan($this->app, $this->events, $this->app->version()))
->resolveCommands($this->commands)
->setContainerCommandLoader(); ... then when /**
* Add a command, resolving through the application.
*
* @param string $command
* @return \Symfony\Component\Console\Command\Command|null
*/
public function resolve($command)
{
if (class_exists($command) && ($commandName = $command::getDefaultName())) {
$this->commandMap[$commandName] = $command;
return null;
}
return $this->add($this->laravel->make($command));
} It calls |
I just tested with a large class based class file and when making static calls to a class it does indeed load it in memory as can be observed with It's likely still much lower than actually resolving the classes from the container, but still room for optimization so the classes aren't loaded from disk at all. Though I'm not sure how this plays with OpCache as it may have those classes in memory already. |
Currently, all commands in Laravel have to be instantiated to be registered in the console application. That could be a problem for commands with big dependency graphs as all the dependencies are loaded in memory. See laravel/ideas#1399.
Symfony has had lazy loading support for commands since version 3.4. This PR extends Symfony's lazy loading to Laravel. To allow for lazy loading, Laravel package console commands and/or app console commands just need to set the static
$defaultName
property like so:Since the PR changes the return type of
Application::resolve
(adds a null return type), I'm sending this to master instead of 8x. If this PR is accepted, I can send across another PR to lazy load the commands shipped with Laravel.