-
-
Notifications
You must be signed in to change notification settings - Fork 362
[LiveComponent] Optimize LiveComponentStack::getCurrentLiveComponent()
#2821
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
Should this use |
Oh, that's a good point, I didn't think about it. I think we are safe here, given this reproducer: <?php
$componentsCount = 100;
$cache = [];
echo sprintf('Memory usage before: %d MiB', memory_get_peak_usage(true) / 1024 / 1024 ) . PHP_EOL;
for ($i = 0; $i < $componentsCount; $i++) {
$cache['\App\Twig\Components\ComponentA' . $i] = $i % 2;
}
echo sprintf('Memory usage after: %d MiB', memory_get_peak_usage(true) / 1024 / 1024 ) . PHP_EOL;
The memory usage is not an issue. And |
LiveComponentStack::isLiveComponent
LiveComponentStack::isLiveComponent()
f6ea6cc
to
b866cdc
Compare
I went for another solution, where we can completely shortcut It gives better results, https://blackfire.io/profiles/compare/8bbc8e11-fbad-4853-bb08-9cbdcb857e50/graph: |
b866cdc
to
cc7f4fa
Compare
LiveComponentStack::isLiveComponent()
LiveComponentStack::getCurrentLiveComponent()
We've been using maps of component names -> stuff in various places for some time now, and they've helped winning major optimizations, while I have not received nor read any downsides. But more importantly ... The class is used here, not the name. That means all the anonymous components in a given project count as one. Finally, I've never heard of a project with a thousand TwigComponent classes for now... and i'm pretty sure there would be a lot of other problems before 😶 |
TL;DR; maybe let's see if there is some problem here and we will adapt accordingly :) (i'm talking about the kernel.reset here, not this PR that is a wonderful "little change-big effect" illustration of @Kocal's work ❤️ ) |
cc7f4fa
to
a7920d4
Compare
…tLiveComponent()` (Kocal) This PR was squashed before being merged into the 2.x branch. Discussion ---------- [LiveComponent] Optimize `LiveComponentStack::getCurrentLiveComponent()` | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Docs? | no <!-- required for new features --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT Using Reflection during runtime is costly. Here I suggest using static caching to prevent unnecessary usages of Reflection. I've re-used reproducer from #2812, but with 50 rows (otherwise I can't create a Blackfire Profile), and it reduced the rendering time by 200ms, https://blackfire.io/profiles/compare/7aa62248-9332-40b3-b6fa-58756cb17232/graph <img width="1341" alt="image" src="https://github.com/user-attachments/assets/d03148c5-521e-4501-937c-54d9e90ece73" /> **EDIT:** see symfony/ux#2821 (comment) Commits ------- a7920d429a9 [LiveComponent] Optimize `LiveComponentStack::getCurrentLiveComponent()`
Using Reflection during runtime is costly. Here I suggest using static caching to prevent unnecessary usages of Reflection.
I've re-used reproducer from #2812, but with 50 rows (otherwise I can't create a Blackfire Profile), and it reduced the rendering time by 200ms, https://blackfire.io/profiles/compare/7aa62248-9332-40b3-b6fa-58756cb17232/graph

EDIT: see #2821 (comment)