Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

perf(editor): status bar readouts subscribe per-field, not per-snapshot

yhh9b4681d05455
diff --git a/desktop/src/engine/StatsStore.ts b/desktop/src/engine/StatsStore.ts
index 240a400b5..a0a91e862 100644
--- a/desktop/src/engine/StatsStore.ts
+++ b/desktop/src/engine/StatsStore.ts
@@ -96,7 +96,16 @@ class StatsStoreImpl {
const vram = sampleVram();
if (fps !== cur.fps || entities !== cur.entities
|| !selEq(selection, cur.selection) || !vramEq(vram, cur.vram)) {
- this.store.setState({ fps, entities, selection, vram });
+ // Preserve the reference of any field whose value is unchanged, so a
+ // leaf that subscribes to only that field (see StatusBar) bails out of
+ // re-rendering. Without this, replacing the whole snapshot hands every
+ // leaf a fresh object each 333ms tick even when its value held.
+ this.store.setState({
+ fps,
+ entities,
+ selection: selEq(selection, cur.selection) ? cur.selection : selection,
+ vram: vramEq(vram, cur.vram) ? cur.vram : vram,
+ });
}
}
};
diff --git a/desktop/src/layout/StatusBar.tsx b/desktop/src/layout/StatusBar.tsx
index 7489208fb..89135c18c 100644
--- a/desktop/src/layout/StatusBar.tsx
+++ b/desktop/src/layout/StatusBar.tsx
@@ -13,6 +13,44 @@ import { t } from '@/i18n';
const formatMb = (bytes: number) => (bytes / (1024 * 1024)).toFixed(1);
+// Live readouts each subscribe to just their field of the stats snapshot, so the
+// 333ms stats tick — and a dragged entity's constantly-moving selection readout —
+// re-render only the affected text node, never the footer shell (its icons + i18n
+// labels + buttons). Field references stay stable when unchanged (see StatsStore),
+// so an unchanged field's leaf bails out. Mirrors CursorReadout below.
+function FpsValue() {
+ const fps = useSyncExternalStore(StatsStore.subscribe, () => StatsStore.getSnapshot().fps);
+ return <>{fps}</>;
+}
+
+function EntitiesValue() {
+ const entities = useSyncExternalStore(StatsStore.subscribe, () => StatsStore.getSnapshot().entities);
+ return <>{t('layout.status.entities', { count: entities })}</>;
+}
+
+function SelectionReadout() {
+ const selection = useSyncExternalStore(StatsStore.subscribe, () => StatsStore.getSnapshot().selection);
+ if (!selection) return null;
+ return (
+ <span className="sitem mono" title={t('layout.status.selectionTooltip')}>
+ {selection.x}, {selection.y}
+ <span className="smute"> · {selection.rot}°</span>
+ </span>
+ );
+}
+
+function VramReadout() {
+ const vram = useSyncExternalStore(StatsStore.subscribe, () => StatsStore.getSnapshot().vram);
+ if (!vram) return null;
+ return (
+ <span className="sitem mono" title={t('layout.status.vramTooltip', { count: vram.evictable })}>
+ <MemoryStick size={11} strokeWidth={1.85} />
+ {formatMb(vram.bytes)}
+ <span className="smute">/{formatMb(vram.budget)} MB</span>
+ </span>
+ );
+}
+
// Only this text node re-renders on mouse move, never the footer.
function CursorReadout() {
const cursor = useSyncExternalStore(StatsStore.subscribeCursor, StatsStore.getCursor);
@@ -26,7 +64,6 @@ export function StatusBar() {
const contentDrawer = useEditorStore((s) => s.contentDrawer);
const toggleContentDrawer = useEditorStore((s) => s.toggleContentDrawer);
const selectedIds = useSelection((s) => s.selectedIds);
- const stats = useSyncExternalStore(StatsStore.subscribe, StatsStore.getSnapshot);
return (
<footer className="status">
@@ -48,12 +85,7 @@ export function StatusBar() {
<span className="sitem">
{selectedIds.size ? t('layout.status.selected', { count: selectedIds.size }) : t('layout.status.noSelection')}
</span>
- {stats.selection && (
- <span className="sitem mono" title={t('layout.status.selectionTooltip')}>
- {stats.selection.x}, {stats.selection.y}
- <span className="smute"> · {stats.selection.rot}°</span>
- </span>
- )}
+ <SelectionReadout />
</div>
<span className="sp" />
@@ -63,21 +95,12 @@ export function StatusBar() {
<CursorReadout />
</span>
<span className="sitem mono">
- <Gauge size={11} strokeWidth={1.85} /> {stats.fps} fps
+ <Gauge size={11} strokeWidth={1.85} /> <FpsValue /> fps
</span>
<span className="sitem mono">
- <Boxes size={11} strokeWidth={1.85} /> {t('layout.status.entities', { count: stats.entities })}
+ <Boxes size={11} strokeWidth={1.85} /> <EntitiesValue />
</span>
- {stats.vram && (
- <span
- className="sitem mono"
- title={t('layout.status.vramTooltip', { count: stats.vram.evictable })}
- >
- <MemoryStick size={11} strokeWidth={1.85} />
- {formatMb(stats.vram.bytes)}
- <span className="smute">/{formatMb(stats.vram.budget)} MB</span>
- </span>
- )}
+ <VramReadout />
<span
className="sitem mono"
title={t('layout.status.backendTooltip')}