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))

CommentFileSizeAuthor
#2 3006123-2.patch592 bytesjoelpittet

Comments

joelpittet created an issue. See original summary.

joelpittet’s picture

Status: Active » Needs review
StatusFileSize
new592 bytes
joelpittet’s picture

Here are the XHProf results on a a really slow and large permissions page /admin/people/permissions

drupal_array_get_nested_value Run #5bc01efd78f85 Run #5bc01dd98cbba Diff Diff%
Number of Function Calls 30,869 30,869 0 0.0%
Incl. Wall Time (microsec) 342,967 211,098 -131,869 -38.4%
Incl. Wall Time (microsec) per call 11 7 -4 -38.4%
Excl. Wall Time (microsec) 342,967 211,098 -131,869 -38.4%
Incl. CPU (microsecs) 345,238 213,398 -131,840 -38.2%
Incl. CPU (microsecs) per call 11 7 -4 -38.2%
Excl. CPU (microsec) 345,238 213,398 -131,840 -38.2%
Incl. MemUse (bytes) 81,063,520 81,063,520 0 0.0%
Incl. MemUse (bytes) per call 2,626 2,626 0 0.0%
Excl. MemUse (bytes) 81,063,520 81,063,520 0 0.0%
Incl. PeakMemUse (bytes) 22,539,136 22,539,136 0 0.0%
Incl. PeakMemUse (bytes) per call 730 730 0 0.0%
Excl. PeakMemUse (bytes) 22,539,136 22,539,136 0 0.0%
joelpittet’s picture

Issue summary: View changes

Swapped out the referenced link as it's broken.

joseph.olstad’s picture

Status: Needs review » Reviewed & tested by the community

Nice work Joel.

joseph.olstad’s picture

Issue tags: +Drupal 7.68 target
joseph.olstad’s picture

joseph.olstad’s picture

Maintain the RTBC:

1) Has sufficient test coverage
2) 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

joseph.olstad’s picture

Issue tags: +Pending Drupal 7 commit

Pending Drupal 7 commit - based on FabianX's comment here on a related micro-optimization:
#2863786-18: D7 ThemeRegistry array_key_exists() micro-optimization

joseph.olstad’s picture

Maintain 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

Significant gains in performance here!

mustanggb’s picture

Issue tags: -Drupal 7.68 target +Drupal 7.69 target
mustanggb’s picture

Issue tags: -Drupal 7.69 target +Drupal 7.70 target
joseph.olstad’s picture

joseph.olstad’s picture

marcelovani’s picture

This seem to be a very low risk fix, I wonder why it takes 2 years to commit...

joelpittet’s picture

Issue tags: -Drupal 7.76 target +Drupal 7.78 target
ressa’s picture

This issue is included in #3179845: [meta] Priorities for 2020-12-02 bugfix release of Drupal 7.76 / 7.77, so tagging with "Drupal 7.xx target" probably isn't necessary. A new meta issue for a planned D7 release around February 2021 should be created soon, transferring the remaining issues there.

mcdruid’s picture

Issue tags: -Drupal 7.78 target

LGTM - we'll try to get this into the next release.

fabianx’s picture

RTBC + 1

  • mcdruid committed 867374f on 7.x
    Issue #3006123 by joelpittet: D7 drupal_array_get_nested_value()...
mcdruid’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Pending Drupal 7 commit

Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

jweowu’s picture

Quoting #10:

> Is already in D8 (similar fix)

Was this really done in D8?

It was obviously a performance bug in PHP, and it's been fixed. I'm using PHP 7.4.20 and this awkward combo of isset and array_key_exists is slower than array_key_exists on its own (according to the benchmark used in http://thinkofdev.com/php-fast-way-to-determine-a-key-elements-existance... which seems to be where this hack originated), so any such code should be reverted to the simpler and now-faster method.

jweowu’s picture

Also, hacks like this should always include comments to explain them, because the otherwise the code looks ludicrous, and is liable to be reverted in future by someone noticing the redundancy.

joseph.olstad’s picture

Yes we got these optimisations into D8 first

Not a hack. Well known performance of isset vs other functions.