You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
minor #2206 [Translator] Many performance improvements on the cache warming (Kocal)
This PR was squashed before being merged into the 2.x branch.
Discussion
----------
[Translator] Many performance improvements on the cache warming
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | no <!-- please update src/**/CHANGELOG.md files -->
| Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License | MIT
The Symfony UX Translator cache warmer can be an long task, especially if your app has a lot of translations.
I was able to identify some bottlenecks and fix them, with a dedicated commit for each of them.
My test application have ~25k translation keys (3.119 keys * 8 locales). Of course that's a lot, but I believe some people needs to dump all their translations (e.g. if they build an SPA on Symfony). For "more classic" applications, it is recommended to [use dedicated domains for translations JS-side](https://symfony.com/bundles/ux-translator/current/index.html#configuring-the-dumped-translations).
Anyway, in my application, I was able to reduce the Translator cache warming **from 2m11s** (higher than the reality, because of Blackfire profiling) **to 41s** (higher than the reality, because of Blackfire profiling):
<img width="1339" alt="image" src="https://github.com/user-attachments/assets/98b5546b-0ba9-457d-b007-e8afdee7e5ac">
The vast majority of improvements come from better use of [the Symfony String component](https://symfony.com/doc/current/string.html).
Blackfire profiles:
1. [Initial](https://app.blackfire.io/profiles/2e264f01-c3a6-46de-92fa-0856b2078010/graph)
2. [Re-using `s()` instances](https://app.blackfire.io/profiles/b4c5dea7-4309-4cb8-b99f-584ec3a4ddc9/graph)
3. [Removing useless `s()->slice()`](https://app.blackfire.io/profiles/f66b6bdf-39da-4d98-a966-781418b3a6f5/graph)
4. [Caching `$this->message->length()`](https://app.blackfire.io/profiles/1334e3e1-4405-44d1-a327-e446dad7d1d8/graph) as it will always return the same length
5. [Before/After](https://blackfire.io/profiles/compare/8a0df8d0-5d01-47bf-a064-bfed6783c5a3/graph): ~63% 🚀
Also, since the Translator component is still experimental, I've marked some classes as final and internal, as they should only be used internally. :)
Commits
-------
02e1eb3 [Translator] Mark internal classes as internal
f5e84c5 [Translator] Mark $message and $messageLength as readonly
2f13d73 [Translator] Improve performance, cache $this->message->length() to prevent unnecessary computations
b582090 [Translator] Improve performance, replace array_reduce/array_keys by foreach
0bf9041 [Translator] Improve performance, remove (internally) duplicated call to `->slice($offset, 1)`
18eac63 [Translator] Improve performance, extract some logic from a loop from `IntlMessageParser#generateConstantName()`
e053dd0 [Translator] Improve performance, reduce `s()` calls and inline `IntlMessageParser#offset()`
0 commit comments