Skip to content

array_merge should be optimized more #9881

Closed as not planned
Closed as not planned
@mvorisek

Description

@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 #9794 and https://bugs.php.net/bug.php?id=72492

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions