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.
This piece fo code in registry.inc is terribly inefficient calling array_keys for (the relatively large array) $files in every loop iteration.
foreach ($lookup_cache as $key => $file) {
// If the file for this cached resource is carried over unchanged from
// the last registry build, then we can safely re-cache it.
if ($file && in_array($file, array_keys($files)) && !in_array($file, $parsed_files)) {
$unchanged_resources[$key] = $file;
}
}
On my machine it takes abount 20 miliseconds to run this loop. Using array_key_exists instead cuts the execution time to 0.1 miliseconds, making it two orders of magnitude faster.
foreach ($lookup_cache as $key => $file) {
// If the file for this cached resource is carried over unchanged from
// the last registry build, then we can safely re-cache it.
if ($file && array_key_exists($file, $files) && !in_array($file, $parsed_files)) {
$unchanged_resources[$key] = $file;
}
}
I know this is nitpicking as this code is not run very often, but such it's such a small, 100% safe, change (with a marginally positive effect on performance) i still think it could be worth committing.
Comment | File | Size | Author |
---|---|---|---|
#5 | 2146597-5.patch | 626 bytes | joelpittet |
#1 | drupal-registry-update-optimization-2146597-1.patch | 631 bytes | gnucifer |
Comments
Comment #1
gnucifer CreditAttribution: gnucifer commentedComment #2
gnucifer CreditAttribution: gnucifer commentedComment #3
gnucifer CreditAttribution: gnucifer commentedComment #4
mgiffordDoes this apply to Drupal 8 as well? If so it needs to be fixed there first.
Comment #5
joelpittetisset()
is a bit more of a micro-optimization, how about this?@mgifford, the registry exists in D8 but this pattern could be fixed in a number of other places in D8 and D7. Anybody interested in opening up issues for these I'll gladly RTBC:
Regex search:
in_array\(\$[^,]+, array_key
Comment #6
sjerdopatch #5 LGTM +1
Comment #7
joseph.olstad+1 , feel that need for speed.
Comment #8
joelpittetThis was fixed in #1470656: Registry rebuild should not parse the same file twice in the same request
Comment #9
joseph.olstadYay, faster and faster! Keep up the great work Joelpittet