[php-src] Issue #9881: `array_merge` (and related same variable never failing internal operations) should be optimized more
| From: | mvorisek | Date: | Wed, 02 Nov 2022 20:37:39 +0000 |
| Subject: | [php-src] Issue #9881: `array_merge` (and related same variable never failing internal operations) should be optimized more | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-242738@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/9881
Author: mvorisek
### Description
https://3v4l.org/q7YIc
code patterns like:
```
$res = [];
foreach ($items as $item) {
$res = array_merge($res, $this->getData($item));
}
```
are very common and as shown in the 3v4l fiddle the code can run much faster if php will optimize
array_merge function using this algorithm:
- if array_merge is internal (not replaced/custom) function
- choose the argument with the most elements/count
- use such argument by reference and merge the other arguments into it
this optimization can reduce the complexity from O(n) to O(1) in extreme case (1 argument with many
elements and 1 argument with 1 element)
same optimization should be done for spread operator as well
related https://github.com/php/php-src/issues/9794 and
https://bugs.php.net/bug.php?id=72492