Opened 5 months ago
Closed 4 months ago
#61624 closed enhancement (worksforme)
Cache translations
Reported by: | leonardola | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 6.6 |
Component: | I18N | Keywords: | reporter-feedback needs-patch close |
Focuses: | performance | Cc: |
Description
While working on a big codebase I found out that translations were taking up to 40% of request times.
When I dug deeper I noticed that some plugins were translating the same strings over and over and that WordPress does not cache the translations and ends up calling all of the gettext filters again each time.
It would be good to either cache the translations in WPCore or to at least add a hook that would allow plugins to cache translations and preemptive return a cached translation when they see fit instead of running all of the gettext filters again.
Change History (6)
This ticket was mentioned in PR #7010 on WordPress/wordpress-develop by leonardola.
5 months ago
#1
- Keywords has-patch added
#2
@
5 months ago
- Keywords reporter-feedback needs-patch added; has-patch removed
Hi there and welcome to WordPress Trac!
Did you do any concrete profiling for this? I would love to see your data.
The new translation system in 6.5+ is actually very lean and translating the same string multiple times doesn't cause any additional overhead (O(1) to access it from an array).
What definitely can add overhead is applying all these filters every time. However, plugins rely on these, and suddenly not running the filters could be a backward compatibility break.
Also, as you will have seen on GitHub, your PR breaks a lot of existing tests, so it's definitely not as straightforward as adding a simple caching variable. Not to mention that calling determine_locale()
so often in a hot code path actually makes things slower.
Trac ticket: https://core.trac.wordpress.org/ticket/61624
This PR adds a cache to the
translate
function to preventgettext
filters from running when the same string is being translated over and over by a plugin.