Recently I have stumbled upon a case where my Drupal installation exceeded 128 MB of PHP memory, because I have used array_merge() in a loop. PHPStorms EA extended plugin reports that array_merge() is a resources greedy construction, when used in a loop. Therefor it's good to replace such usages.

CommentFileSizeAuthor
#2 3044373-array_merge_loop.patch639 bytesjepster_

Comments

Peter Majmesku created an issue. See original summary.

jepster_’s picture

Status: Active » Needs review
StatusFileSize
new639 bytes

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

steven.wichers’s picture

Status: Needs review » Needs work

The attached patch changes the functionality. array_merge and += are functionally different. Here's a simple example to demonstrate:


$exceptions = [
    'foo' => 'bar',
    'fizz' => 'buzz',
    'pizza' => 'rolls',
];

$secondary = [
    'foo' => 'apples',
    'fizz' => 'banana',
    'socks' => 'shoes',
];

print_r(array_merge($exceptions, $secondary));
print_r($exceptions += $secondary);

Result:

Array
(
    [foo] => apples
    [fizz] => banana
    [pizza] => rolls
    [socks] => shoes
)
Array
(
    [foo] => bar
    [fizz] => buzz
    [pizza] => rolls
    [socks] => shoes
)

Note the difference in the foo and fizz items.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mxr576’s picture

Status: Needs work » Closed (duplicate)
Related issues: +#3266568: Refactor array_merge() usage in loops as possible for performance

Closing this in favor of #3266568 that addresses this problem too.

mxr576’s picture