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 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 Dec 24, 2019
@GrahamCampbell

This comment has been minimized.

Copy link
Member Author

GrahamCampbell commented Dec 24, 2019

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.

@taylorotwell taylorotwell merged commit 2bde5b7 into 6.x Dec 25, 2019
2 of 4 checks passed
2 of 4 checks passed
continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
continuous-integration/travis-ci/push The Travis CI build is in progress
Details
continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/styleci/push The analysis has passed
Details
@taylorotwell taylorotwell deleted the sapi branch Dec 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.