Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[11.x] Blade @include performance #54633

Merged
merged 4 commits into from
Feb 17, 2025

Conversation

AlliBalliBaba
Copy link
Contributor

@AlliBalliBaba AlliBalliBaba commented Feb 16, 2025

After looking at some flamegraphs with xdebug, I noticed that for templates with many includes a lot of time was being spent on this line:

return "<?php echo \$__env->make('{$path}', {$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";

After looking at what Arr::except does, I noticed there's a built in function that does the same and can be used in this case instead: array_diff_key. array_diff_key exists since PHP 5.1 and excludes the keys of the second array from the first one.

In the below flamegraph you can see, that the built in function is around ~500% more efficient
(comparison done by excluding 2 keys from an array with 100 keys)
array-key-diff

Using the built in function improves performance for Views with lots of @includes and generally makes the <?php snippet more portable (it doesn't need to access a namespaced class)

@KennedyTedesco
Copy link
Contributor

Nice!

@Diddyy
Copy link

Diddyy commented Feb 16, 2025

I love attention to detail like this! Nice work :)

@taylorotwell taylorotwell merged commit eefa55a into laravel:11.x Feb 17, 2025
44 checks passed
@AndrewMast
Copy link

Maybe we should change Arr::except to be return array_diff_key($array, array_flip($keys));?

@AlliBalliBaba
Copy link
Contributor Author

Arr::except also supports ArrayAccess, not just arrays I think.

@KennedyTedesco
Copy link
Contributor

KennedyTedesco commented Feb 18, 2025

@AlliBalliBaba Would you mind sharing what you use to make these performance comparisons and the flamegraph? Do you use php-spx? Do you have any gist or something to help get started with this? Thanks!

@AlliBalliBaba
Copy link
Contributor Author

AlliBalliBaba commented Feb 18, 2025

You can create nice flamegraphs for pretty much any programming language using [brendangregg's flamegraph repo](https://github.com/brendangregg/FlameGraph]. xdebug supports creating cpu profiles since recently, so you can follow this guide, though it is a bit outdated.
I might write down a step by step guide for Laravel/Octane at some point.

Also I forgot to mention: in order to create more detailed flamegraphs, you might want to spam a specific endpoints with many requests. There are some command line tools that let you do this easily, like wrk or ab.

@KennedyTedesco
Copy link
Contributor

@AlliBalliBaba Nice! I'll play with this right away.

@AJenbo
Copy link
Contributor

AJenbo commented Feb 18, 2025

Hm even though this broke the Bladestan tests, this at least gives me some hope that there is a decent chance we could clean up some of the generated code to make it a bit easier to analyze :D

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants