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 much faster than array_key_exists()
.
Proposed resolution
Use isset()
Use both with a shortcut ||
after isset()
Comment | File | Size | Author |
---|---|---|---|
#2 | get_option_micro-2760419-2.patch | 520 bytes | joelpittet |
| |||
get_option.png | 99.9 KB | joelpittet |
Comments
Comment #2
joelpittetComment #3
joelpittetComment #4
joelpittet100x faster or 3ms:P!
Comment #5
joseph.olstadBravo! Great work @JoelPittet.
Comment #6
joseph.olstadWe've been using this patch in production for a few weeks, and on our dev machines, it's excellent!
Thanks again.
Comment #7
joseph.olstadComment #8
joseph.olstadjoelpittet has done some great work here. Adding the 'minor version target' tag.
Please include this for 7.x-3.17
Comment #9
joseph.olstadThese other performance fixes all play nice together. I recommend all of them for 7.x-3.17 please and thanks.
Comment #10
dsutter CreditAttribution: dsutter as a volunteer commentedRTBC+ patch #2
Comment #11
DamienMcKennaWhat's the benefit of keeping the array_key_exists() logic? isset() on its own should suffice.
Comment #12
DamienMcKennaHave you looked at making this (and the other) performance improvement in D8?
Comment #13
joseph.olstadreroll to follow , dropping array_key_exists
Comment #14
joseph.olstadComment #15
joseph.olstadD8 patch as been rolled
#2903843-1: views get_option() micro optimization
Comment #16
joseph.olstadComment #17
joseph.olstadComment #18
DamienMcKennaComment #19
joseph.olstadHmm, DamienMckenna Joel_Pittets patch is the way to go.
we need to check that the array_key_exists because isset when the array element value is null would return false (meaning the test would fail) but we want to know either way.
So back to Joel_Pittets patch. Otherwise we risk breaking something.
Comment #20
joseph.olstadaccording to Joel_Pittet , this is a 100x improvement
in relative terms, 3ms
We've been using his patch verbatim for quite a while now, it's holding up great.
Comment #21
DamienMcKennaThat crafty PHP! Thanks for digging into it, joseph.olstad.
Comment #22
joseph.olstadThanks to DSutter for the php in-depth analysis, he's sitting beside me here. A php wizard of wizards.
Comment #23
joelpittetIsset doesn't keep null values but array_key_exists does, I have a number of other d8 issues that make this optimization, don't have the reference off hand(on my phone) but easily googleable.
I didn't want to change logic as to make this change equivalent to what was there.
Comment #24
joseph.olstadyes agreed #23, basically what we were trying to say in #19, but helps explaining it in that way as well. PHP trickiness
nice work JoelPittet
Comment #25
joelpittetSorry, I didn't read the back scroll, you guys figured out that stuff within a span of a few hours. Nice:)
Comment #26
willzyx CreditAttribution: willzyx commented@joelpittet great work! maybe unrelated by I'm thinking that the same approach (
isset() || array_key_exists()
) could be used even in d7 core in ThemeRegistry::offsetExists. Not tested but probably could prevent a lot of calls to array_key_exists functionComment #27
joelpittet@willzyx, that's the one I was bumbling about in #23 on my phone:) Here's the link #2863786: D7 ThemeRegistry array_key_exists() micro-optimization #2770065: array_key_exists() micro-optimization
Comment #28
joelpittetHere's a link to find others like this:)
https://www.drupal.org/project/issues/search?text=&projects=&assigned=&s...
Comment #29
willzyx CreditAttribution: willzyx commented@joelpittet good to know :)
Comment #30
joseph.olstadFYI, we're ready.
This was committed to D8 views (in core 8.4.x and 8.5.x branches)
Thanks to Joel_pittet for the original patch.
Comment #31
willzyx CreditAttribution: willzyx commentedProbably patch in
#14#2 is a good candidate for 7.x-3.19 releaseComment #32
joseph.olstad@willzyx, the original patch #2 is the way to go, patch #14 is NOT the way. #14 was hidden.
joel_pittet patch is what all the related patches are based on.
Comment #33
willzyx CreditAttribution: willzyx commented@joseph.olstad oops you are right, my mistake :). Patch #2 is the way to go
Comment #35
DamienMcKennaCommitted. Thanks!