Navigation Menu

Skip to content
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

[6.x] Speed up runningInConsole method #30922

Merged
merged 3 commits into from Dec 25, 2019
Merged

[6.x] Speed up runningInConsole method #30922

merged 3 commits into from Dec 25, 2019

Conversation

GrahamCampbell
Copy link
Member

@GrahamCampbell GrahamCampbell commented Dec 24, 2019

This code is actually quite hot, since it's called by most (package) service providers.

The new code is faster because:

  1. Env::get isn't all that cheap, in the case where the env variable was found in the last place we look, and so we should only call it once, and assign the result.
  2. The php optimizer can now ahead of time replace return php_sapi_name() === 'cli' || php_sapi_name() === 'phpdbg'; with either return true; or return false;, since it is able to replace the constant (that it knows is global due to the slash) with the string value, and then it can evaluate the equality and or, all ahead of time.

I detected this optimization when profiling a Laravel app with xdebug tracing btw. ;)

@GrahamCampbell GrahamCampbell changed the title [7.x] Speed up runningInConsole method [6.x] Speed up runningInConsole method Dec 24, 2019
@GrahamCampbell GrahamCampbell changed the base branch from master to 6.x December 24, 2019 18:01
@GrahamCampbell
Copy link
Member Author

We can do one better, actually, by caching the computation result, so we don't have to check the environment variable each time the method is called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants