perf(mobile): memoize Intl polyfills used during thread opens #40602
Openabotmaker(GitHub profile, opens in a new tab)wants to merge 2 commits into
main fromrefs/pull/40602/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -0,0 +1,21 @@1+// Memoizes immutable Intl polyfill lookups (@formatjs + Hermes) that GT invokes per formatted message.2+3+const INSTALLED_FLAG = Symbol.for("t3code.mobile.intlPolyfillCacheInstalled");4+const MAX_CONSTRUCTOR_CACHE = 128;5+const MAX_LOCALE_CACHE = 256;6+const MAX_LOOKUP_CACHE = 512;7+8+type GenericConstructor = new (...args: unknown[]) => object;9+10+function serializeLocalesArg(11+ locales: Intl.LocalesArgument,12+ OrigLocale: typeof Intl.Locale | undefined,13+): string | null {14+ if (locales === undefined) return "__def__";15+ if (typeof locales === "string") return `s:${locales}`;16+ if (OrigLocale && locales instanceof OrigLocale) return `l:${locales.toString()}`;17+ if (Array.isArray(locales)) {18+ if (locales.length === 0) return "a:";19+ const parts: string[] = [];20+ for (let i = 0; i < locales.length; i++) {21+ const item = locales[i];@@ -0,0 +10,21 @@10+function serializeLocalesArg(11+ locales: Intl.LocalesArgument,12+ OrigLocale: typeof Intl.Locale | undefined,13+): string | null {14+ if (locales === undefined) return "__def__";15+ if (typeof locales === "string") return `s:${locales}`;16+ if (OrigLocale && locales instanceof OrigLocale) return `l:${locales.toString()}`;17+ if (Array.isArray(locales)) {18+ if (locales.length === 0) return "a:";19+ const parts: string[] = [];20+ for (let i = 0; i < locales.length; i++) {21+ const item = locales[i];22+ if (typeof item === "string") {23+ parts.push(item);24+ } else if (OrigLocale && item instanceof OrigLocale) {25+ parts.push(item.toString());26+ } else {27+ return null;28+ }29+ }30+ return `a:${parts.join("\u0000")}`;@@ -0,0 +20,21 @@20+ for (let i = 0; i < locales.length; i++) {21+ const item = locales[i];22+ if (typeof item === "string") {23+ parts.push(item);24+ } else if (OrigLocale && item instanceof OrigLocale) {25+ parts.push(item.toString());26+ } else {27+ return null;28+ }29+ }30+ return `a:${parts.join("\u0000")}`;31+ }32+ return null;33+}34+35+function serializePlainOptions(options: unknown): string | null {36+ if (options === undefined) return "";37+ if (typeof options !== "object" || options === null) return null;38+ const proto = Object.getPrototypeOf(options);39+ if (proto !== Object.prototype && proto !== null) return null;40+ const keys = Object.keys(options as Record<string, unknown>);@@ -0,0 +27,21 @@27+ return null;28+ }29+ }30+ return `a:${parts.join("\u0000")}`;31+ }32+ return null;33+}34+35+function serializePlainOptions(options: unknown): string | null {36+ if (options === undefined) return "";37+ if (typeof options !== "object" || options === null) return null;38+ const proto = Object.getPrototypeOf(options);39+ if (proto !== Object.prototype && proto !== null) return null;40+ const keys = Object.keys(options as Record<string, unknown>);41+ if (keys.length === 0) return "";42+ if (keys.length > 16) return null;43+ if (keys.length > 1) keys.sort();44+ const parts: string[] = [];45+ for (let i = 0; i < keys.length; i++) {46+ const key = keys[i]!;47+ const value = (options as Record<string, unknown>)[key];@@ -0,0 +35,21 @@35+function serializePlainOptions(options: unknown): string | null {36+ if (options === undefined) return "";37+ if (typeof options !== "object" || options === null) return null;38+ const proto = Object.getPrototypeOf(options);39+ if (proto !== Object.prototype && proto !== null) return null;40+ const keys = Object.keys(options as Record<string, unknown>);41+ if (keys.length === 0) return "";42+ if (keys.length > 16) return null;43+ if (keys.length > 1) keys.sort();44+ const parts: string[] = [];45+ for (let i = 0; i < keys.length; i++) {46+ const key = keys[i]!;47+ const value = (options as Record<string, unknown>)[key];48+ const type = typeof value;49+ if (value === undefined) continue;50+ if (type !== "string" && type !== "number" && type !== "boolean") {51+ return null;52+ }53+ parts.push(`${key}=${type[0]}:${String(value)}`);54+ }55+ return parts.join("|");@@ -0,0 +177,21 @@177+ }178+179+ const localeCache = new Map<string, Intl.Locale>();180+ function CachedLocale(181+ this: unknown,182+ tag: Intl.UnicodeBCP47LocaleIdentifier | Intl.Locale,183+ options?: Intl.LocaleOptions,184+ ) {185+ if (!new.target) {186+ return (TargetLocale as unknown as (...args: unknown[]) => unknown)(tag, options);187+ }188+ if (new.target !== CachedLocale) {189+ return Reflect.construct(190+ TargetLocale as unknown as GenericConstructor,191+ [tag, options],192+ new.target as unknown as GenericConstructor,193+ );194+ }195+ if (options === undefined) {196+ const key =197+ typeof tag === "string" ? tag : tag instanceof TargetLocale ? tag.toString() : null;@@ -0,0 +302,21 @@302+303+ const displayNamesCache = new Map<string, Intl.DisplayNames>();304+ function CachedDisplayNames(305+ this: unknown,306+ locales: Intl.LocalesArgument,307+ options: Intl.DisplayNamesOptions,308+ ) {309+ if (!new.target) {310+ return (TargetDisplayNames as unknown as (...args: unknown[]) => unknown)(locales, options);311+ }312+ if (new.target !== CachedDisplayNames) {313+ return Reflect.construct(314+ TargetDisplayNames as unknown as GenericConstructor,315+ [locales, options],316+ new.target as unknown as GenericConstructor,317+ );318+ }319+ const locKey = serializeLocalesArg(locales, OrigLocale);320+ const optKey = serializePlainOptions(options);321+ if (locKey !== null && optKey !== null) {322+ const key = `${locKey}\u0001${optKey}`;@@ -0,0 +394,21 @@394+ ) => string[],395+ OrigLocale,396+ );397+ function CachedDateTimeFormat(398+ this: unknown,399+ locales?: Intl.LocalesArgument,400+ options?: Intl.DateTimeFormatOptions,401+ ) {402+ if (new.target && new.target !== CachedDateTimeFormat) {403+ return Reflect.construct(404+ TargetDateTimeFormat as unknown as GenericConstructor,405+ [locales, options],406+ new.target as unknown as GenericConstructor,407+ );408+ }409+ const locKey = serializeLocalesArg(locales, OrigLocale);410+ const optKey = serializePlainOptions(options);411+ if (locKey !== null && optKey !== null) {412+ const key = `${locKey}\u0001${optKey}`;413+ const cached = dateTimeFormatCache.get(key);414+ if (cached !== undefined) return cached;
Pushed13ac036
github-actions[bot](GitHub profile, opens in a new tab) removed labelsneeds-rebase
Sign in to join this conversation.
No actionable comments were generated in the recent review.
Walkthrough
The mobile i18n code adds a bounded caching installer for available Intl APIs. The polyfill module invokes the installer during evaluation. Tests cover cached results, locale handling, constructor behavior, and selected Intl methods.
Changes: Intl Polyfill Caching
apps/mobile/src/i18n/intl-polyfill-cache.ts,apps/mobile/src/i18n/intl-polyfills.tsapps/mobile/src/i18n/intl-polyfill-cache.tsLocale,PluralRules,DisplayNames,NumberFormat, andDateTimeFormatinstances or selected method results.DateTimeFormatinstance caching requires an explicit stringtimeZone.apps/mobile/src/i18n/intl-polyfill-cache.test.tsEstimated code review effort: 3 (Moderate) | ~25 minutes
Change: Refactor
Sequence Diagram(s)#
Possibly related issues
Suggested reviewers:
t3dotggMerge Risk:
Low · up to
13ac0This change memoizes Intl polyfills to speed up opening large mobile threads. The one remaining concern is an unlikely edge case where an unusual locale-array input could be cached under the wrong key. It is low risk and mergeable, though the owner should be aware of it.
Pre-merge checks:
4
Full details: Title check
Explanation
The title "perf(mobile): memoize Intl polyfills used during thread opens" names the main change: bounded memoization of Intl polyfill operations (Locale, PluralRules, DisplayNames, NumberFormat, DateTimeFormat, getCanonicalLocales) in the mobile app, aimed at reducing JS-thread stalls when opening threads. It follows the conventional commit format and is specific enough for a teammate scanning history.
Full details: Description check
Explanation
The description covers the required sections of the template. Problem explains the stall on medium-to-large mobile threads with profiling data. Change lists the new cache module, its installation point, and the memoized Intl operations. Scope states this is a bug fix, limited to apps/mobile/src/i18n/, and cites CONTRIBUTING.md. Verification reports before/after device measurements, the checks that passed, and an iOS build that was not run. The title is not in the template's conventional commit form in the provided text, but the template only asks for it in an HTML comment. The description states that it is a performance bug fix and gives device measurements. It also names the agent and model, which the template requests. Overall, the description is mostly complete and specific.
Finishing Touches
Generate unit tests (beta)
ae4fda6a-eea4-4f84-9954-fef222bbb8f0Reviewing files that changed from the base of the PR and between 832bf405c36cb68b372dc594a22b5b19f3a2e367 and 13ac036bf0e6216af05a507bfa28d82d0335ba69.
apps/mobile/src/i18n/intl-polyfills.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.