[StimulusBundle] Speed up Stimulus name normalization - #3781
Merged
Conversation
smnandre
approved these changes
Aug 15, 2026
smnandre
left a comment
Member
There was a problem hiding this comment.
Should we define a minimum gap to accept improvements ?
There is a hidden cost in maintenance, history, etc.
Genuine question, I have no opinion here :)
Kocal
force-pushed
the
perf/stimulus-bundle-name-normalization
branch
2 times, most recently
from
August 15, 2026 22:14
7d573ee to
cf6ceac
Compare
| Q | A
| -------------- | ---
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Documentation? | no
| Issues | -
| License | MIT
`normalizeKeyName()` ran three `preg_*` calls for every value, class and
param name, and `normalizeControllerName()` a `preg_replace()` for every
controller name. Both run on every `stimulus_controller()` /
`stimulus_action()` call, on names that repeat across the whole page.
Memoize the key names (a fixed set coming from templates and controllers)
and drop the regex from the controller name, which only strips a leading
`@`.
Rendering 50k controllers with values, classes, an action and a target:
~414 ms -> ~356 ms.
Benchmarked from the repository root with `blackfire run symfony php bench.php`:
```php
<?php
require __DIR__.'/src/StimulusBundle/vendor/autoload.php';
use Symfony\UX\StimulusBundle\Dto\StimulusAttributes;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
$env = new Environment(new ArrayLoader([]));
for ($i = 0; $i < 5000; ++$i) {
$attributes = new StimulusAttributes($env);
$attributes->addController('my-controller', ['name' => 'ryan', 'isEnabled' => true, 'count' => 42], ['loading' => 'spinner']);
$attributes->addAction('my-controller', 'onClick', 'click');
$attributes->addTarget('my-controller', 'element');
(string) $attributes;
}
```
Blackfire:
- before — 833ms wall / 816ms CPU: https://app.blackfire.io/envs/5f4f9a62-eaa0-45ee-b7b3-a1b879f550e9/profiles/4d6d06bc-f658-4786-952a-302cc8c852ec/graph
- after — 632ms wall / 628ms CPU: https://app.blackfire.io/envs/5f4f9a62-eaa0-45ee-b7b3-a1b879f550e9/profiles/c56f1954-74f2-40ad-9514-eab58bcb8912/graph
- diff: https://app.blackfire.io/envs/5f4f9a62-eaa0-45ee-b7b3-a1b879f550e9/profiles/compare/4d6d06bc-f658-4786-952a-302cc8c852ec...c56f1954-74f2-40ad-9514-eab58bcb8912/graph
Analysis, implementation and benchmarks by Claude Opus 5.
Kocal
force-pushed
the
perf/stimulus-bundle-name-normalization
branch
from
August 15, 2026 22:21
cf6ceac to
267c77e
Compare
Member
Author
Before AI, I'd say we had to be careful with performance patches when it came to code readability, but now I think it's fine... The code is still easy to read IMHO |
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.
normalizeKeyName()ran threepreg_*calls for every value, class and param name, andnormalizeControllerName()ran apreg_replace()for every controller name. Both run on everystimulus_controller()/stimulus_action()call, on names that repeat across the whole page.Memoize the key names (a fixed set coming from templates and controllers) and drop the regex from the controller name, which only strips a leading
@.Rendering 50k controllers with values, classes, an action and a target goes from ~414 ms to ~356 ms. On a more realistic page though, an admin CRUD page with 100 Stimulus controllers, that's 0.45 ms -> 0.42 ms per page: a 7% cut on a budget that's already under a millisecond. smnandre asked in review whether the project should have a minimum gain worth accepting, given the maintenance and history cost of a change like this. That's a fair question: this is a small, low-risk win, not a big one, and reviewers should weigh it as such rather than as more than it is.
Benchmarked from the repository root with
blackfire run symfony php bench.php:Blackfire:
Analysis, implementation and benchmarks by Claude Opus 5.