From 6ece41e16ff54c6ae4953e6369d35bf9187fc6dd Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Tue, 24 Nov 2026 22:31:59 -0800 Subject: [PATCH] perf(mobile): launch with many environments does less work A launch formatted over a hundred plural messages, and each one built a new Intl.PluralRules from the polyfill, about 10 ms each. Equal arguments now share one instance. Each connecting environment also read the device id from the keychain, one read after the other. The id is now read once. Co-Authored-By: Claude Fable 5.2 --- apps/mobile/src/i18n/intl-polyfills.test.ts | 20 +++++++++++++++++ apps/mobile/src/i18n/intl-polyfills.ts | 22 +++++++++++++++++++ .../src/persistence/mobile-storage.test.ts | 14 ++++++++++++ apps/mobile/src/persistence/mobile-storage.ts | 18 +++++++++++++-- 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 apps/mobile/src/i18n/intl-polyfills.test.ts diff --git a/apps/mobile/src/i18n/intl-polyfills.test.ts b/apps/mobile/src/i18n/intl-polyfills.test.ts new file mode 100644 index 000000000000..c39591fbde3c --- /dev/null +++ b/apps/mobile/src/i18n/intl-polyfills.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from "vite-plus/test"; + +import "./intl-polyfills"; + +describe("shared plural rules", () => { + it("builds one instance for equal arguments", () => { + const cardinal = new Intl.PluralRules("en"); + + expect(new Intl.PluralRules("en")).toBe(cardinal); + expect(new Intl.PluralRules("en", { type: "ordinal" })).not.toBe(cardinal); + expect(new Intl.PluralRules("de")).not.toBe(cardinal); + }); + + it("selects the same categories as an unshared instance", () => { + expect(new Intl.PluralRules("en").select(1)).toBe("one"); + expect(new Intl.PluralRules("en").select(2)).toBe("other"); + expect(new Intl.PluralRules("en", { type: "ordinal" }).select(2)).toBe("two"); + expect(new Intl.PluralRules("en")).toBeInstanceOf(Intl.PluralRules); + }); +}); diff --git a/apps/mobile/src/i18n/intl-polyfills.ts b/apps/mobile/src/i18n/intl-polyfills.ts index 68ab81c65f99..3f9908eef762 100644 --- a/apps/mobile/src/i18n/intl-polyfills.ts +++ b/apps/mobile/src/i18n/intl-polyfills.ts @@ -16,3 +16,25 @@ import "@formatjs/intl-pluralrules/locale-data/pt"; import "@formatjs/intl-pluralrules/locale-data/ru"; import "@formatjs/intl-pluralrules/locale-data/sv"; import "@formatjs/intl-pluralrules/locale-data/zh"; + +// GT builds a new Intl.PluralRules for every plural message it formats, and the polyfill's +// constructor compares the locale with every locale it knows. One launch formats over a hundred +// plural messages, so equal arguments share one instance. An instance holds no state that changes. +const PolyfilledPluralRules = Intl.PluralRules; +const sharedPluralRules = new Map(); +// The property is read-only in the lib types; the polyfill defines it the same way. +Object.defineProperty(Intl, "PluralRules", { + configurable: true, + writable: true, + value: new Proxy(PolyfilledPluralRules, { + construct(target, args: ConstructorParameters) { + const key = JSON.stringify(args); + let rules = sharedPluralRules.get(key); + if (rules === undefined) { + rules = new target(...args); + sharedPluralRules.set(key, rules); + } + return rules; + }, + }), +}); diff --git a/apps/mobile/src/persistence/mobile-storage.test.ts b/apps/mobile/src/persistence/mobile-storage.test.ts index 5568c777f2ae..8ff6d791f047 100644 --- a/apps/mobile/src/persistence/mobile-storage.test.ts +++ b/apps/mobile/src/persistence/mobile-storage.test.ts @@ -78,6 +78,20 @@ describe("mobile device identity", () => { }), ); + it.effect("reads the keychain once, however many environments ask for the identity", () => + Effect.gen(function* () { + values.set(deviceIdKey, "existing-device"); + const storage = yield* makeStorage; + + yield* Effect.all( + Array.from({ length: 12 }, () => storage.loadOrCreateAgentAwarenessDeviceId), + { concurrency: "unbounded" }, + ); + expect(yield* storage.loadOrCreateAgentAwarenessDeviceId).toBe("existing-device"); + expect(getItemAsync).toHaveBeenCalledTimes(1); + }), + ); + it.effect("retries a failed first write without retaining an unpersisted identity", () => Effect.gen(function* () { setItemAsync.mockRejectedValueOnce(new Error("Secure storage unavailable")); diff --git a/apps/mobile/src/persistence/mobile-storage.ts b/apps/mobile/src/persistence/mobile-storage.ts index 2abbcd4d1008..18dc291549f0 100644 --- a/apps/mobile/src/persistence/mobile-storage.ts +++ b/apps/mobile/src/persistence/mobile-storage.ts @@ -131,18 +131,32 @@ export const make = Effect.fn("MobileStorage.make")(function* () { yield* secureStorage.setItem(key, encoded); }); - const loadOrCreateRandomId = Effect.fn("MobileStorage.loadOrCreateRandomId")(function* ( + // An id never changes once stored. Every environment asks for it when it connects, + // and each keychain read waits for the one before it. + const knownIds = new Map(); + const readOrCreateRandomId = Effect.fn("MobileStorage.loadOrCreateRandomId")(function* ( key: string, ) { + const known = knownIds.get(key); + if (known !== undefined) return known; const existing = yield* secureStorage.getItem(key); - if (existing?.trim()) return existing; + if (existing?.trim()) { + knownIds.set(key, existing); + return existing; + } const created = yield* Effect.tryPromise({ try: () => import("../lib/uuid").then(({ uuidv4 }) => uuidv4()), catch: (cause) => new MobileDeviceIdGenerationError({ cause }), }); yield* secureStorage.setItem(key, created); + knownIds.set(key, created); return created; }, deviceIdLock.withPermits(1)); + const loadOrCreateRandomId = (key: string) => + Effect.suspend(() => { + const known = knownIds.get(key); + return known === undefined ? readOrCreateRandomId(key) : Effect.succeed(known); + }); const loadOrCreateAgentAwarenessDeviceId = loadOrCreateRandomId(AGENT_AWARENESS_DEVICE_ID_KEY); const loadAgentAwarenessDeviceId = secureStorage