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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
perf(editor): read engine telemetry only while the Profiler panel is open
esenginec07b564c6dae
diff --git a/desktop/src/engine/EditorControlSurface.ts b/desktop/src/engine/EditorControlSurface.tsindex 93b2de80d..9dbc4353f 100644--- a/desktop/src/engine/EditorControlSurface.ts+++ b/desktop/src/engine/EditorControlSurface.ts@@ -380,7 +380,7 @@ export class EditorControlSurfaceImpl {/** Live counts for quick assertions (entity count + last frame's merged draw calls). */getStats(): { entities: number; drawCalls: number } {return {- entities: EngineHost.world?.getAllEntities().length ?? 0,+ entities: EngineHost.world?.entityCount() ?? 0,drawCalls: EngineHost.module?.renderer_getDrawCalls?.() ?? 0,};}diff --git a/desktop/src/engine/EngineHost.ts b/desktop/src/engine/EngineHost.tsindex 423d4eb37..0e069b0fb 100644--- a/desktop/src/engine/EngineHost.ts+++ b/desktop/src/engine/EngineHost.ts@@ -163,7 +163,7 @@ class EngineHostImpl {drawCalls: m?.renderer_getDrawCalls?.() ?? 0,triangles: m?.renderer_getTriangles?.() ?? 0,sprites: m?.renderer_getSprites?.() ?? 0,- entities: this.world?.getAllEntities().length ?? 0,+ entities: this.world?.entityCount() ?? 0,gpuMs: m?.renderer_getGpuTimeMs?.() ?? -1,cppScopes,cppCounters,diff --git a/desktop/src/engine/PerfMonitor.ts b/desktop/src/engine/PerfMonitor.tsindex 7560573ed..0a331bfa0 100644--- a/desktop/src/engine/PerfMonitor.ts+++ b/desktop/src/engine/PerfMonitor.ts@@ -173,6 +173,7 @@ class PerfMonitorImpl {private worstPhase: string | null = null;private longTaskMs = 0;private engineSource: (() => EngineFrame | null) | null = null;+ private engineConsumers_ = 0;private engineSum = 0;private editorSum = 0;private winFrames = 0;@@ -222,6 +223,23 @@ class PerfMonitorImpl {setEnabled(on: boolean): void { this.enabled = on; this.patch({ enabled: on }); }toggleOverlay(): void { this.patch({ visible: !this.store.getState().visible }); }+ /**+ * Register interest in the engine frame — the only expensive per-frame read+ * (cross-boundary counters + CPU/GPU scope JSON marshalling). The loop skips+ * it entirely while nothing is mounted to display it (e.g. the Profiler panel+ * is closed), so an ordinary editing session pays nothing for it. Returns a+ * disposer for the caller's unmount.+ */+ addEngineConsumer(): () => void {+ this.engineConsumers_++;+ let disposed = false;+ return () => {+ if (disposed) return;+ disposed = true;+ this.engineConsumers_ = Math.max(0, this.engineConsumers_ - 1);+ };+ }+/** Freeze the rolling capture and pin a frame for inspection. */pin(id: number): void {this.frozen = true;@@ -376,7 +394,7 @@ class PerfMonitorImpl {if (this.enabled) {const editorFrameMs = sumMs(editorPhases);this.editorSum += editorFrameMs;- const ef = this.engineSource?.();+ const ef = (this.engineConsumers_ > 0 || this.recording) ? this.engineSource?.() : null;let engineFrameMs = 0;let enginePhases: Record<string, number> = {};let cppScopes: Record<string, number> = {};diff --git a/desktop/src/engine/StatsStore.ts b/desktop/src/engine/StatsStore.tsindex 7b5bba8ff..240a400b5 100644--- a/desktop/src/engine/StatsStore.ts+++ b/desktop/src/engine/StatsStore.ts@@ -88,7 +88,7 @@ class StatsStoreImpl {const elapsed = t - this.windowStart;if (elapsed >= 333) {const fps = Math.round((this.frames * 1000) / elapsed);- const entities = EngineHost.world?.getAllEntities().length ?? 0;+ const entities = EngineHost.world?.entityCount() ?? 0;this.frames = 0;this.windowStart = t;const cur = this.store.getState();diff --git a/desktop/src/engine/schema.ts b/desktop/src/engine/schema.tsindex 13a9fa840..3dc8b4c3d 100644--- a/desktop/src/engine/schema.ts+++ b/desktop/src/engine/schema.ts@@ -26,7 +26,7 @@ export type WorldT = App['world'];*/export type ReadonlyWorldT = Pick<WorldT,- 'valid' | 'has' | 'get' | 'getAllEntities' | 'getWorldVersion'+ 'valid' | 'has' | 'get' | 'getAllEntities' | 'entityCount' | 'getWorldVersion'>;export type AnyComp = Parameters<WorldT['has']>[1];diff --git a/desktop/src/panels/ProfilerPanel.tsx b/desktop/src/panels/ProfilerPanel.tsxindex 69b424bec..73ef88c73 100644--- a/desktop/src/panels/ProfilerPanel.tsx+++ b/desktop/src/panels/ProfilerPanel.tsx@@ -186,6 +186,8 @@ function Seg({ label, ms, frame, color }: { label: string; ms: number; frame: nuexport function ProfilerPanel() {const s = useSyncExternalStore(PerfMonitor.subscribe, PerfMonitor.getSnapshot);+ // Only while this panel is mounted does the loop pay for the engine-frame read.+ useEffect(() => PerfMonitor.addEngineConsumer(), []);const pinned = s.pinnedId != null ? PerfMonitor.getSample(s.pinnedId) : null;const [hidden, toggleGroup] = useHiddenGroups();const show = (id: string) => !hidden.has(id);