Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

perf(mobile): 减少任务列表更新时的重算与整屏重绘

Dash66ef30062c1c
diff --git a/apps/mobile/src/i18n/presentationLocalizer.ts b/apps/mobile/src/i18n/presentationLocalizer.ts
index 069431ca77..ee3d078c73 100644
--- a/apps/mobile/src/i18n/presentationLocalizer.ts
+++ b/apps/mobile/src/i18n/presentationLocalizer.ts
@@ -1,12 +1,13 @@
import { i18n } from '@/i18n';
import { formatPresentationDate, formatPresentationTime } from './dateFormatters';
+import { memoizedTranslation } from './translationMemo';
import type {
PresentationLocalizer,
PresentationTranslator,
} from '@cindy/maker-shared/presentation-localization';
export const mobilePresentationTranslate: PresentationTranslator = (key, fallback, values) =>
- i18n.t(key, { defaultValue: fallback, ...values });
+ memoizedTranslation(key, { defaultValue: fallback, ...values });
export const mobilePresentationLocalizer: PresentationLocalizer = {
translate: mobilePresentationTranslate,
diff --git a/apps/mobile/src/i18n/translationMemo.ts b/apps/mobile/src/i18n/translationMemo.ts
new file mode 100644
index 0000000000..6ae348d4c5
--- /dev/null
+++ b/apps/mobile/src/i18n/translationMemo.ts
@@ -0,0 +1,28 @@
+import { i18n } from '@/i18n';
+
+// Plural and interpolated labels cost tens of microseconds each in i18next on Android,
+// and the task list rebuilds hundreds of them on every task update. A label depends only
+// on the language, key and values, so keep it until the language or resources change.
+const MAX_LABELS = 2000;
+const labels = new Map<string, string>();
+let watching = false;
+
+function clearLabels(): void {
+ labels.clear();
+}
+
+export function memoizedTranslation(key: string, options?: Readonly<Record<string, unknown>>): string {
+ if (!watching) {
+ watching = true;
+ i18n.on?.('languageChanged', clearLabels);
+ i18n.store?.on?.('added', clearLabels);
+ i18n.store?.on?.('removed', clearLabels);
+ }
+ const cacheKey = `${i18n.language}\u0000${key}\u0000${options ? JSON.stringify(options) : ''}`;
+ const previous = labels.get(cacheKey);
+ if (previous !== undefined) return previous;
+ const label = i18n.t(key, options as Record<string, unknown>);
+ if (labels.size >= MAX_LABELS) labels.clear();
+ labels.set(cacheKey, label);
+ return label;
+}
diff --git a/apps/mobile/src/session/HomeSurface.tsx b/apps/mobile/src/session/HomeSurface.tsx
index 6396e897de..8ab333c772 100644
--- a/apps/mobile/src/session/HomeSurface.tsx
+++ b/apps/mobile/src/session/HomeSurface.tsx
@@ -2372,9 +2372,14 @@ function HomeScreenContent({ active = true, onModeChange, width, newSessionInSys
guardedPush(href);
}, [guardedPush, rememberTask, swipeRegistry, t]);
+ // Every visible row receives this callback, and the list presentation is rebuilt on each
+ // task update. Read the latest target on tap so updates do not re-render every row.
+ const newSessionTargetRef = useRef({ primaryDevice: home.primaryDevice, newSessionDeviceOptions, selectedDeviceId });
+ newSessionTargetRef.current = { primaryDevice: home.primaryDevice, newSessionDeviceOptions, selectedDeviceId };
const openNewSession = useCallback((project?: MobileHomeProjectGroup, suggestion?: RemoteTaskSuggestionId, explicitDeviceId?: string, origin?: ComposerMorphOrigin) => {
- const deviceId = project?.deviceId ?? explicitDeviceId ?? home.primaryDevice?.deviceId;
- const deviceName = project?.deviceName ?? newSessionDeviceOptions.find((device) => device.deviceId === deviceId)?.name ?? home.primaryDevice?.label ?? deviceId ?? '';
+ const { primaryDevice, newSessionDeviceOptions, selectedDeviceId } = newSessionTargetRef.current;
+ const deviceId = project?.deviceId ?? explicitDeviceId ?? primaryDevice?.deviceId;
+ const deviceName = project?.deviceName ?? newSessionDeviceOptions.find((device) => device.deviceId === deviceId)?.name ?? primaryDevice?.label ?? deviceId ?? '';
if (!deviceId) {
setError(t('devices.list.error.noDevice'));
return;
@@ -2393,7 +2398,7 @@ function HomeScreenContent({ active = true, onModeChange, width, newSessionInSys
...(selectedDeviceId || explicitDeviceId ? { deviceExplicit: '1' } : {}),
},
});
- }, [guardedPush, home.primaryDevice, newSessionDeviceOptions, selectedDeviceId, t]);
+ }, [guardedPush, t]);
useEffect(() => {
if (!newSessionActionRef) return;
diff --git a/apps/mobile/src/session/sessionList.ts b/apps/mobile/src/session/sessionList.ts
index b3f6b4e7b3..9d94ad9d69 100644
--- a/apps/mobile/src/session/sessionList.ts
+++ b/apps/mobile/src/session/sessionList.ts
@@ -1,6 +1,7 @@
import { i18n } from '@/i18n';
import { formatPresentationDate } from '@/i18n/dateFormatters';
import { mobilePresentationLocalizer } from '@/i18n/presentationLocalizer';
+import { memoizedTranslation } from '@/i18n/translationMemo';
import {
buildRemoteSessionCardPreview as buildRemoteSessionCardPreviewShared,
buildRemoteSessionListContext as buildRemoteSessionListContextShared,
@@ -18,18 +19,9 @@ export * from '@cindy/maker-shared/session-list';
type ListTranslator = (key: string, values?: Record<string, string | number>) => string;
-/** Scoped to one synchronous list build: repeated status/count labels only run
- * through i18next once, with no stale strings after locale/resource changes. */
+/** Status, count and time labels repeat across rows and list rebuilds. */
export function createSessionListTranslator(): ListTranslator {
- const labels = new Map<string, string>();
- return (key, values) => {
- const cacheKey = JSON.stringify([key, values]);
- const previous = labels.get(cacheKey);
- if (previous !== undefined) return previous;
- const label = i18n.t(key, values);
- labels.set(cacheKey, label);
- return label;
- };
+ return memoizedTranslation;
}
export function buildRemoteSessionSections(