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(ecs): avoid duplicate component checks during inspection
yhh3b14e72bd015
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 58702c259..225cc6b5d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -22,6 +22,8 @@ published separately; it ships inside the editor.### Fixed+- **Runtime inspection avoids duplicate builtin component checks.** Entity component enumeration checks cached builtin bindings once and reads the shared builtin catalogue instead of allocating a merged builtin/script map for every entity. Live tree snapshots retain uncached membership checks and immediately reflect component additions and removals.+- **Unused transient streams no longer reserve GPU buffers.** Renderer pools initialize pipeline layouts without reserving vertex/index buffers or CPU staging for every stream. Each stream allocates storage on its first non-empty write, preserving buffer reuse, growth and recovery while avoiding unused particle, shape and sky allocations in 2D scenes.- **GPU memory estimates include buffers and render targets.** Runtime reports and editor captures count live buffer capacity, texture mip chains, compressed blocks and target attachments without double-counting framebuffer references. WebGPU also includes private fallback, surface and readback resources. Lost or destroyed resources stop contributing; the profiler labels this as an estimate that excludes driver overhead, shader binaries and host-owned swapchain images.diff --git a/sdk/src/ecs/component.ts b/sdk/src/ecs/component.tsindex 3b6fa4583..63b9bc5f0 100644--- a/sdk/src/ecs/component.ts+++ b/sdk/src/ecs/component.ts@@ -582,6 +582,11 @@ export function getComponentRegistry(): Map<string, AnyComponentDef> {return all;}+/** @internal Shared builtin definitions, without constructing a merged catalogue. */+export function builtinComponentRegistry(): ReadonlyMap<string, AnyComponentDef> {+ return builtinRegistry;+}+/** Just the current context's user/script components (excludes builtins). */export function getUserComponents(): Map<string, AnyComponentDef> {return userComponents();diff --git a/sdk/src/ecs/world.ts b/sdk/src/ecs/world.tsindex 0c212d304..7b5dcb6a2 100644--- a/sdk/src/ecs/world.ts+++ b/sdk/src/ecs/world.ts@@ -6,7 +6,7 @@*/import { Entity, entityGeneration, entityIndex, makeEntity, INVALID_ENTITY } from '../types';-import { AnyComponentDef, ComponentDef, ComponentData, BuiltinComponentDef, isBuiltinComponent, getComponentRegistry, getUserComponents, getComponent, Disabled, Name, Parent, Children, type ParentData, type ChildrenData } from './component';+import { AnyComponentDef, ComponentDef, ComponentData, BuiltinComponentDef, isBuiltinComponent, builtinComponentRegistry, getUserComponents, getComponent, Disabled, Name, Parent, Children, type ParentData, type ChildrenData } from './component';import type { CppRegistry, ESEngineModule } from '../wasm';import { handleWasmError } from '../wasm/wasmError';import { BuiltinBridge, builtinFromWasm, numberListFieldsOf, convertForWasm, type BridgeConnectOptions,@@ -1087,12 +1087,13 @@ export class World {* a system asks {@link has} about the definitions it knows. */getComponentTypes(entity: Entity): string[] {const types = new Set<string>();- for (const [name, methods] of this.builtin_.getMethodCache()) {- try { if (methods.has(entity)) types.add(name); } catch (e) { log.warn('world', `Component check failed for ${name}`, e); }+ const methods = this.builtin_.getMethodCache();+ for (const [name, binding] of methods) {+ try { if (binding.has(entity)) types.add(name); } catch (e) { log.warn('world', `Component check failed for ${name}`, e); }}if (this.builtin_.hasCpp) {- for (const [name, comp] of getComponentRegistry()) {- if (isBuiltinComponent(comp) && !types.has(name)) {+ for (const [name, comp] of builtinComponentRegistry()) {+ if (isBuiltinComponent(comp) && !methods.has(name)) {try {const m = this.builtin_.getBuiltinMethods(comp._cppName);if (m.has(entity)) types.add(name);diff --git a/sdk/tests/world.test.ts b/sdk/tests/world.test.tsindex 9e54196f9..f78bd2093 100644--- a/sdk/tests/world.test.ts+++ b/sdk/tests/world.test.ts@@ -2,7 +2,7 @@// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Teamimport { describe, it, expect, beforeEach, vi } from 'vitest';import { World, computeQueryCacheKey } from '../src/ecs/world';-import { defineComponent, defineTag, defineBuiltin, Name } from '../src/ecs/component';+import { defineComponent, defineTag, defineBuiltin, Name, Transform } from '../src/ecs/component';import { createMockModule } from './mocks/wasm';describe('World', () => {@@ -791,6 +791,28 @@ describe('World', () => {// =========================================================================describe('getComponentTypes', () => {+ it('checks each known builtin once and reflects component mutations immediately', () => {+ const module = createMockModule();+ const registry = module.getRegistry();+ const original = registry.hasTransform!;+ const has = vi.fn((entity: number) => original(entity));+ registry.hasTransform = has;+ const inspected = new World();+ inspected.connectCpp(registry, module);+ const entity = inspected.spawn();+ has.mockClear();+ expect(inspected.getComponentTypes(entity)).not.toContain('Transform');+ expect(has).toHaveBeenCalledTimes(1);+ inspected.insert(entity, Transform);+ expect(inspected.getComponentTypes(entity)).toContain('Transform');+ inspected.remove(entity, Transform);+ expect(inspected.getComponentTypes(entity)).not.toContain('Transform');+ inspected.insert(entity, Position, { x: 1, y: 2 });+ expect(inspected.getComponentTypes(entity)).toContain('TestPosition');+ inspected.remove(entity, Position);+ expect(inspected.getComponentTypes(entity)).not.toContain('TestPosition');+ });+it('should return an array (may be empty for script-only without registry)', () => {const entity = world.spawn();world.insert(entity, Position, { x: 0, y: 0 });diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 0e2264a91..8ee8ebe77 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'perf(ecs): avoid duplicate component checks during inspection':+ { note: 'Runtime inspection avoids duplicate builtin component checks.' },'perf(renderer): allocate transient stream storage on first use':{ note: 'Unused transient streams no longer reserve GPU buffers.' },'fix(profiler): label live gpu storage estimates':