Skip to content

perf(i18n): build a locale's translator on its first use - #13788

Closed
yuxi-liu-wired wants to merge 1 commit into
logseq:masterfrom
yuxi-liu-wired:perf/i18n-lazy-dicts
Closed

yuxi-liu-wired wants to merge 1 commit into
logseq:masterfrom
yuxi-liu-wired:perf/i18n-lazy-dicts

Conversation

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor

Opening the desktop app spends about 180 ms building translations it never uses.

frontend.context.i18n builds two tongue translators when the namespace loads, translate and translate-strict, each over the dictionaries of every locale (25 of them). tongue's build-translate flattens and resolves aliases in every dictionary it is given, so both translators process all 25 dictionaries, while the app shows one locale. In a renderer CPU profile of an app open (page reload to the home screen, 1010 ms in all), tongue.core/build-translate takes 182 ms, the largest single cost of the open.

The fix builds a translator per locale on its first use (memoized per locale, with or without the English fallback), from that locale's dictionary, its parent tags' (:zh-Hant falls back through :zh, as tongue resolves tags), and English for the fallback one. t, t-en, t-locale and tt translate exactly as before. The first translation in a locale builds one or two dictionaries instead of 25 twice.

Test: per-locale-translators-match-one-over-every-locale-test compares, for every key of every locale (and for a locale that has no dictionary), the result of the new translators with and without fallback against tongue translators built over every locale's dictionary, as before. It passes, and it fails when the English fallback is left out of the fallback translator.

@yuxi-liu-wired
yuxi-liu-wired force-pushed the perf/i18n-lazy-dicts branch 6 times, most recently from b7585d8 to 8b59d8a Compare October 25, 2026 11:01
The two tongue translators were built at namespace load over all 25
locale dictionaries (182 ms of an app open in a renderer profile), while
the app shows one locale. Each locale now gets its translator on first
use, from its own dictionary, its parent tags' and English for the
fallback. A test compares every key of every locale against translators
built over every dictionary.
@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Combined into #13872 with the other PRs of this theme, to keep the number of open PRs down; the commits are unchanged there.

@yuxi-liu-wired
yuxi-liu-wired deleted the perf/i18n-lazy-dicts branch November 17, 2026 02:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant