Skip to content

Commit 2f1e7b8

Browse files
committed
minor symfony#2343 [TwigComponent] Improve BlockStack performances (smnandre)
This PR was merged into the 2.x branch. Discussion ---------- [TwigComponent] Improve BlockStack performances BlockStack Quick Optimization Minor change with major impact (test app with a component rendered 5000 times in the main part of the page) <img width="971" alt="Capture d’écran 2024-11-06 à 03 09 43" src="https://github.com/user-attachments/assets/367ef408-ade8-4b3b-abab-23960f7ca094"> Commits ------- 18bc31b [TwigComponent] Improve BlockStack performances
2 parents 78d243e + 18bc31b commit 2f1e7b8

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/TwigComponent/src/BlockStack.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ final class BlockStack
2828
*/
2929
private array $stack;
3030

31+
/**
32+
* @var array<class-string, int>
33+
*/
34+
private static array $templateIndexStack = [];
35+
3136
public function convert(array $blocks, int $targetEmbeddedTemplateIndex): array
3237
{
3338
$newBlocks = [];
39+
$hostEmbeddedTemplateIndex = null;
3440
foreach ($blocks as $blockName => $block) {
3541
// Keep already converted outer blocks untouched
3642
if (str_starts_with($blockName, self::OUTER_BLOCK_PREFIX)) {
@@ -41,7 +47,7 @@ public function convert(array $blocks, int $targetEmbeddedTemplateIndex): array
4147
// Determine the location of the block where it is defined in the host Template.
4248
// Each component has its own embedded template. That template's index uniquely
4349
// identifies the block definition.
44-
$hostEmbeddedTemplateIndex = $this->findHostEmbeddedTemplateIndex();
50+
$hostEmbeddedTemplateIndex ??= $this->findHostEmbeddedTemplateIndex();
4551

4652
// Change the name of outer blocks to something unique so blocks of nested components aren't overridden,
4753
// which otherwise might cause a recursion loop when nesting components.
@@ -69,12 +75,10 @@ private function findHostEmbeddedTemplateIndex(): int
6975
{
7076
$backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS | \DEBUG_BACKTRACE_PROVIDE_OBJECT);
7177

72-
$componentTemplateClassName = null;
73-
7478
foreach ($backtrace as $trace) {
7579
if (isset($trace['object']) && $trace['object'] instanceof Template) {
7680
$classname = $trace['object']::class;
77-
$templateIndex = $this->getTemplateIndexFromTemplateClassname($classname);
81+
$templateIndex = self::getTemplateIndexFromTemplateClassname($classname);
7882
if ($templateIndex) {
7983
// If there's no template index, then we're in a component template
8084
// and we need to go up until we find the embedded template
@@ -93,7 +97,7 @@ private function findCallingEmbeddedTemplateIndex(): int
9397

9498
foreach ($backtrace as $trace) {
9599
if (isset($trace['object']) && $trace['object'] instanceof Template) {
96-
return $this->getTemplateIndexFromTemplateClassname($trace['object']::class);
100+
return self::getTemplateIndexFromTemplateClassname($trace['object']::class);
97101
}
98102
}
99103
}
@@ -108,7 +112,7 @@ private function findHostEmbeddedTemplateIndexFromCaller(): int
108112
foreach ($backtrace as $trace) {
109113
if (isset($trace['object']) && $trace['object'] instanceof Template) {
110114
$classname = $trace['object']::class;
111-
$templateIndex = $this->getTemplateIndexFromTemplateClassname($classname);
115+
$templateIndex = self::getTemplateIndexFromTemplateClassname($classname);
112116
if (null === $renderer) {
113117
if ($templateIndex) {
114118
// This class is an embedded template.
@@ -139,8 +143,8 @@ private function findHostEmbeddedTemplateIndexFromCaller(): int
139143
return 0;
140144
}
141145

142-
private function getTemplateIndexFromTemplateClassname(string $classname): int
146+
private static function getTemplateIndexFromTemplateClassname(string $classname): int
143147
{
144-
return (int) substr($classname, strrpos($classname, '___') + 3);
148+
return self::$templateIndexStack[$classname] ??= (int) substr($classname, strrpos($classname, '___') + 3);
145149
}
146150
}

0 commit comments

Comments
 (0)