Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
isset()
is fast but doesn't take in account NULL values, we can use it ahead of an array_key_exists()
check to glean some of it's speed but PHP's short circuit ||
http://thinkofdev.com/php-fast-way-to-determine-a-key-elements-existance...
Proposed resolution
Before
if (array_key_exists($key, $array))
After
if (isset($array[$key]) || array_key_exists($key, $array))
Comments
Comment #2
joelpittetComment #3
joelpittetComment #4
ndobromirov CreditAttribution: ndobromirov at FFW commentedThis is trivial yet effective. Moving to RTBC.
Comment #5
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedLooks nice to me, but let's get it into Drupal 8 first.
Comment #6
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedI missed that there is already a parent issue for Drupal 8 (#2770065: array_key_exists() micro-optimization) - although it looks like maybe it only contains one of the fixes from this issue, but not both?
Postponing this on that issue for now.
Comment #7
joelpittet@David_Rothstein, that issue was testing a wider change in D8. This one is targeting something easier to test and I wanted a slightly faster D7 site;)
Fine with waiting for the D8 one to be committed first.
Comment #8
willzyx CreditAttribution: willzyx commentedProbably this is unnecessary since the previous statement is an
isset()
Otherwise looks good to me :)
Comment #9
joelpittetnice catch, good point! NW when the other gets in.
Comment #10
joseph.olstadjoelpittet , the D8 issue is stalled right now, it needs a reroll, they want it to be a smaller patch like this one, see the last couple comments. Also, what versions of php are you XHProf 'ing on?
wondering if PHP 7 also sees performance improvement with this patch, and if so, by how much.
Comment #11
joelpittetLikely 5.6. I was thinking of splitting it too. Easier to evaluate its merit as it's not a slam dunk as I originally thought
Comment #12
willzyx CreditAttribution: willzyx commentedWe can reopen this issue now that #2770065: array_key_exists() micro-optimization is in. Moving to NW for #8
Comment #13
Dinesh18 CreditAttribution: Dinesh18 as a volunteer commentedhere is an updated patch and interdiff which implements as per #8
Comment #14
willzyx CreditAttribution: willzyx commented@Dinesh18 thanks for the patch
array_key_exists
here is needed, this change should be reverted (in #8 I meant only theisset()
part :))Comment #15
Dinesh18 CreditAttribution: Dinesh18 as a volunteer commentedHere is an updated patch and interdiff as per comment #14
Comment #16
joseph.olstadRTBC #15
Comment #17
donquixote CreditAttribution: donquixote as a volunteer commentedI agree, #15 looks good.
(if it helps)
Comment #18
Fabianx CreditAttribution: Fabianx at Tag1 Consulting commentedLooks good to me. I am a fan of micro optimizations like this one :).
Pending Commit.
Comment #19
joseph.olstadEasy win here!
7.60?
Comment #20
joelpittetComment #21
joseph.olstadFeel the need for speed!
Comment #22
joseph.olstadComment #23
joseph.olstadRTBC maintain
I am a huge fan of optimisations like this going in, this improves speed for everyone!
FabianX already agreed to this change in #2863786-18: D7 ThemeRegistry array_key_exists() micro-optimization
So I think we have a clear consensus that this is ready.
Also, as mentioned in #12, it's already in D8 #2770065: array_key_exists() micro-optimization
We've put in similar optimisations to this already, and this is another good one. I would very much like to see this go into 7.68
Note: To increase our confidence in the test results for the above issue, these two should go in first so we can re-queue php 7.3 and php 5.3 tests.
#3047844: [Regression] Tests fail on PHP 5.3
#3025335: session_id() cannot be changed after session is started
Comment #24
joseph.olstadMaintain RTBC
1) Is already in D8 (similar fix)
2) we already put in performance optimisations similar to this.
3) passes tests
4) has been performance profiled to prove gains
Comment #25
MustangGB CreditAttribution: MustangGB commentedComment #26
MustangGB CreditAttribution: MustangGB commentedComment #27
volegerComment #28
joelpittetComment #29
mcdruidLGTM - we'll try to get this into the next release.
Comment #30
Fabianx CreditAttribution: Fabianx at Tag1 Consulting commentedRTBC + 1,
Just noting fun fact that for replacing NULL in arrays that only contain arrays an object of stdClass can be used as the NULL placeholder.
The reason is that `instanceof stdClass` is also not needing a function call, so can be cheaply checked.
So then you can get away with only isset() and instanceof operators and no function calls.
Comment #32
mcdruidThanks everyone!