Skip to content

Use isset() instead of array_key_exists() in DIC #8907

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

Closed
wants to merge 5 commits into from

Conversation

catch56
Copy link

@catch56 catch56 commented Sep 1, 2013

It doesn't look like the array_key_exists() in Container::get() and Container::has() is necessary. Changing this to isset() removed over 1,000 calls on a default Drupal 8 install - attaching before/after xhprof screenshots with this change.

screen shot 2013-09-01 at 1 13 41 pm

screen shot 2013-09-01 at 1 13 49 pm

@stloyd
Copy link
Contributor

stloyd commented Sep 1, 2013

This is wrong as some services can be null (synthetic, i.e. request). Just check broken build at Travis-CI.

@catch56
Copy link
Author

catch56 commented Sep 1, 2013

Yes I'm sure about xdebug being disabled (although it looks like you deleted your comment?).

isset() || array_key_exists() handles the case of a null service, and still gives me significant reduction in calls.

screen shot 2013-09-01 at 2 03 37 pm

@lsmith77
Copy link
Contributor

lsmith77 commented Sep 1, 2013

didn't we go back and forth here before?

@catch56
Copy link
Author

catch56 commented Sep 1, 2013

Well remembered. I did a search and found it discussed here: #7007 (diff)

But isset() || array_key_exists() wasn't mentioned in that exchange.

@fabpot
Copy link
Member

fabpot commented Sep 1, 2013

@catch56 Does it bring a noticeable performance improvement on a full page?

@catch56
Copy link
Author

catch56 commented Sep 1, 2013

It's 0.8 of a millisecond on my local install and 1/45th of the total function calls on the page.

That's not huge but yes it's a measurable improvement (i.e. ten similar changes and they'd add up to 8ms).

@catch56
Copy link
Author

catch56 commented Sep 2, 2013

I think we can likely save some of the 1,200 or so strtolower() calls here as well - by checking for the string as passed in first in the common cases, then falling back to strtolower() later. That's roughly a millisecond on my machine as well - will put it in a new pull request but it'll need to depend on this one since it's the same lines of code.

@fabpot
Copy link
Member

fabpot commented Sep 2, 2013

@catch56 As this is also a performance optimization, feel free to do everything in this PR. That makes things easier for everyone. Thanks.

@catch56
Copy link
Author

catch56 commented Sep 2, 2013

OK sounds good, thanks!

@catch56
Copy link
Author

catch56 commented Sep 2, 2013

Pushed that commit. Here's before after xhprof UI screenshots showing the calls removed.

screen shot 2013-09-02 at 4 14 54 pm
screen shot 2013-09-02 at 4 13 35 pm

@catch56
Copy link
Author

catch56 commented Sep 3, 2013

Because that's Drupal coding standards which my brain defaults to. Updated.

fabpot added a commit that referenced this pull request Sep 3, 2013
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #8907).

Discussion
----------

Use isset() instead of array_key_exists() in DIC

It doesn't look like the array_key_exists() in Container::get() and Container::has() is necessary. Changing this to isset() removed over 1,000 calls on a default Drupal 8 install - attaching before/after xhprof screenshots with this change.

![screen shot 2013-09-01 at 1 13 41 pm](https://f.cloud.github.com/assets/116285/1063965/d826057c-1300-11e3-96c3-2c94b89fe83a.png)

![screen shot 2013-09-01 at 1 13 49 pm](https://f.cloud.github.com/assets/116285/1063961/99a0f7b2-1300-11e3-9bd2-b0bf22d4245e.png)

Commits
-------

3c01ae6 Use isset() instead of array_key_exists() in DIC
@fabpot
Copy link
Member

fabpot commented Sep 3, 2013

merged in 2.3, thanks.

@fabpot fabpot closed this Sep 3, 2013
// available services. Service IDs are case insensitive, however since
// this method can be called thousands of times during a request, avoid
// calling strotolower() unless necessary.
foreach (array(false, true) as $strtolower) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does flatten code would be more efficient than this loop ?

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.

6 participants