Skip to content

Commit ffbb798

Browse files
committed
feature #2821 [LiveComponent] Optimize LiveComponentStack::getCurrentLiveComponent() (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()`
2 parents f4395f3 + 5cc2e2f commit ffbb798

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/Util/LiveComponentStack.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
*/
2525
final class LiveComponentStack extends ComponentStack
2626
{
27+
/** @var array<class-string, bool> */
28+
private array $cacheLiveComponents = [];
29+
2730
public function __construct(private readonly ComponentStack $componentStack)
2831
{
2932
}
3033

3134
public function getCurrentLiveComponent(): ?MountedComponent
3235
{
3336
foreach ($this->componentStack as $mountedComponent) {
34-
if ($this->isLiveComponent($mountedComponent->getComponent()::class)) {
37+
$componentClass = $mountedComponent->getComponent()::class;
38+
if ($this->cacheLiveComponents[$componentClass] ??= $this->isLiveComponent($componentClass)) {
3539
return $mountedComponent;
3640
}
3741
}

0 commit comments

Comments
 (0)