[LiveComponent] Stop materializing the whole call stack twice per component id - #3778
Open
Kocal wants to merge 1 commit into
Open
[LiveComponent] Stop materializing the whole call stack twice per component id#3778Kocal wants to merge 1 commit into
Kocal wants to merge 1 commit into
Conversation
Kocal
force-pushed
the
perf/live-component-deterministic-id
branch
2 times, most recently
from
August 15, 2026 22:14
b2196e7 to
27271c9
Compare
…ponent id
| Q | A
| -------------- | ---
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Documentation? | no
| Issues | -
| License | MIT
`calculateDeterministicId()` runs for every live component rendered
without an explicit id. To find the rendering template and its line,
`guessTemplateInfo()` walked the entire call stack with
`debug_backtrace()`, then built a `Twig\Error\Error` purely to capture
that same stack a second time.
Both frames of interest sit near the top of the stack, so read it once
through a growing window (16, then 64, then unbounded) and reuse it for
both lookups. Output is unchanged.
10k ids from a Twig stack nested 40 frames deep: ~88 ms -> ~27 ms.
Benchmarked from the repository root with `blackfire run symfony php bench.php`:
```php
<?php
require __DIR__.'/src/LiveComponent/vendor/autoload.php';
use Symfony\UX\LiveComponent\Twig\DeterministicTwigIdCalculator;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\TwigFunction;
$calculator = new DeterministicTwigIdCalculator();
// Nested includes, so the calculator is called from a realistic Twig call stack.
$loader = new ArrayLoader([
'level3.html.twig' => '{% for i in 1..20 %}{{ deterministic_id() }}{% endfor %}',
'level2.html.twig' => '{{ include("level3.html.twig") }}',
'level1.html.twig' => '{{ include("level2.html.twig") }}',
'page.html.twig' => '{{ include("level1.html.twig") }}',
]);
$twig = new Environment($loader);
$twig->addFunction(new TwigFunction('deterministic_id', static fn (): string => $calculator->calculateDeterministicId()));
// Emulate the depth of a real Symfony request (kernel, controller, listeners...).
$deep = static function (int $depth, callable $fn) use (&$deep) {
return $depth > 0 ? $deep($depth - 1, $fn) : $fn();
};
for ($i = 0; $i < 60; ++$i) {
$calculator->reset();
$deep(40, static fn () => $twig->render('page.html.twig'));
}
```
Blackfire:
- before — 151ms wall / 124ms CPU / 4.03MB: https://app.blackfire.io/envs/5f4f9a62-eaa0-45ee-b7b3-a1b879f550e9/profiles/4d22039d-d050-49d2-b425-5711efd903b5/graph
- after — 81ms wall / 81ms CPU / 2.21MB: https://app.blackfire.io/envs/5f4f9a62-eaa0-45ee-b7b3-a1b879f550e9/profiles/c8121275-ffaf-4667-a3ac-da8e4cc31cdb/graph
- diff: https://app.blackfire.io/envs/5f4f9a62-eaa0-45ee-b7b3-a1b879f550e9/profiles/compare/4d22039d-d050-49d2-b425-5711efd903b5...c8121275-ffaf-4667-a3ac-da8e4cc31cdb/graph
Analysis, implementation and benchmarks by Claude Opus 5.
Kocal
force-pushed
the
perf/live-component-deterministic-id
branch
from
August 15, 2026 22:21
27271c9 to
458bbf3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
calculateDeterministicId()runs for every live component renderedwithout an explicit id. To find the rendering template and its line,
guessTemplateInfo()walked the entire call stack withdebug_backtrace(), then built aTwig\Error\Errorpurely to capturethat same stack a second time.
Both frames of interest sit near the top of the stack, so read it once
through a growing window (16, then 64, then unbounded) and reuse it for
both lookups. Output is unchanged.
10k ids from a Twig stack nested 40 frames deep: ~88 ms -> ~27 ms.
Benchmarked from the repository root with
blackfire run symfony php bench.php:Blackfire:
Analysis, implementation and benchmarks by Claude Opus 5.