-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[12.x] perf: Optimize BladeCompiler #55703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
is the bulk of the benefit from removing the I'm curious if you would have got the same improvements sticking with the chained Collection calls, but removing the |
You might could get some additional performance gains by using a variable instead of repeatedly accessing the array: foreach (token_get_all($contents) as $token) {
$token = $token[0];
if ($token === T_OPEN_TAG || $token === T_OPEN_TAG_WITH_ECHO || $token === T_CLOSE_TAG) {
$tokens[] = $token;
}
} Alternatively, you might could use foreach (array_column(token_get_all($contents), 0) as $token) {
if ($token === T_OPEN_TAG || $token === T_OPEN_TAG_WITH_ECHO || $token === T_CLOSE_TAG) {
$tokens[] = $token;
}
} |
been programming PHP for 20 years and TIL |
@browner12 the bulk of the imprvements come from 2 places:
|
I didn't benchmark anything, just thinking out loud---performance is one of those things that usually defies intuition |
You can even avoid using indices altogether by using array destructuring: foreach (token_get_all($contents) as $token) {
- $token = $token[0];
+ [$token] = $token;
if ($token === T_OPEN_TAG || $token === T_OPEN_TAG_WITH_ECHO || $token === T_CLOSE_TAG) {
$tokens[] = $token;
}
} |
You can also do foreach (token_get_all($contents) as [$token]) {
``` |
This PR reduces the execution time for
getOpenAndClosingPhpTokens()
by up to 5-8x and thecompile()
by up to 3x, based on my testing.As the improvements are for the BladeCompiler, these benefit the most situations when the Blade Components are not cached (local and misconfigured production environments).
I used the Livewire starter kit on PHP 8.3 and my mac with M3 Max to test my changes.
I went through a few iterations which I will add here:
v1: Original
v2: Most obvious optimization, doing the pluck after the filter
v3: Switching to
foreach()
, instead of using collectionv4: Final optimization: Switch from in_array, to multiple if conditions