Skip to content

[5.8] Arr::collapse better performance#28662

Merged
taylorotwell merged 4 commits into
laravel:5.8from
awais-vteams:5.8
May 30, 2019
Merged

[5.8] Arr::collapse better performance#28662
taylorotwell merged 4 commits into
laravel:5.8from
awais-vteams:5.8

Conversation

@awais-vteams
Copy link
Copy Markdown
Contributor

@awais-vteams awais-vteams commented May 30, 2019

For large array to collapse() get better performance.

Ubuntu 14.04 Core™ i3-4130 CPU @ 3.40GHz × 4, 8GB

$arr = [];
for ($i = 0; $i < 20000; $i++) {
    $arr[] = [1, 2, 3];
}
Arr::collapse($arr);

Before: 6.88 s
After: 0.03 ms

@awais-vteams awais-vteams reopened this May 30, 2019
@taylorotwell
Copy link
Copy Markdown
Member

I don't get it... what is the purpose of merging two empty arrays at the end.

@awais-vteams
Copy link
Copy Markdown
Contributor Author

I don't get it... what is the purpose of merging two empty arrays at the end.

without empty array, PHP 7.3 gives notice to array_merge() expect one parameter to be an array

@taylorotwell
Copy link
Copy Markdown
Member

Is this actually the exact same behavior as the earlier array_merge logic?

@awais-vteams
Copy link
Copy Markdown
Contributor Author

I don't get it But I believe this really time breaking performance in Arr::collapse function You can test run it.

@awais-vteams
Copy link
Copy Markdown
Contributor Author

Actually, array_merge calling in loop previously having O(n) complexity but it now its O(1) 6x faster

@taylorotwell taylorotwell merged commit a5b8f74 into laravel:5.8 May 30, 2019
@staudenmeir
Copy link
Copy Markdown
Contributor

Do we need two empty arrays? array_merge([], ...$results) works for me.

@tantana5
Copy link
Copy Markdown

awesome, exactly i need

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants