From 011d795361e9fd9d86e22781085940958347b1b9 Mon Sep 17 00:00:00 2001 From: LINGYE Date: Thu, 30 Oct 2025 23:27:37 +0800 Subject: [PATCH] =?UTF-8?q?perf(core):=20=E4=BC=98=E5=8C=96=20Scene.system?= =?UTF-8?q?s=20getter=20=E9=81=BF=E5=85=8D=E6=AF=8F=E5=B8=A7=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E6=8E=92=E5=BA=8F=20(#197)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/ECS/Scene.ts | 42 ++++++++++++++++++- packages/core/src/ECS/Systems/EntitySystem.ts | 2 + 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/core/src/ECS/Scene.ts b/packages/core/src/ECS/Scene.ts index 8efb2aa6..b7ac506e 100644 --- a/packages/core/src/ECS/Scene.ts +++ b/packages/core/src/ECS/Scene.ts @@ -108,15 +108,31 @@ export class Scene implements IScene { */ private _didSceneBegin: boolean = false; + /** + * 系统列表缓存 + */ + private _cachedSystems: EntitySystem[] | null = null; + + /** + * 系统顺序脏标记 + * + * 当系统增删或 updateOrder 改变时标记为 true,下次访问 systems 时重新构建缓存 + */ + private _systemsOrderDirty: boolean = true; + /** * 获取场景中所有已注册的EntitySystem * - * 按updateOrder排序。 + * 按updateOrder排序。使用缓存机制,仅在系统变化时重新排序。 * * @returns 系统列表 */ public get systems(): EntitySystem[] { - // 从ServiceContainer获取所有EntitySystem实例 + if (!this._systemsOrderDirty && this._cachedSystems) { + return this._cachedSystems; + } + + // 重新构建系统列表 const services = this._services.getAll(); const systems: EntitySystem[] = []; @@ -129,6 +145,10 @@ export class Scene implements IScene { // 按updateOrder排序 systems.sort((a, b) => a.updateOrder - b.updateOrder); + // 缓存结果 + this._cachedSystems = systems; + this._systemsOrderDirty = false; + return systems; } @@ -150,6 +170,14 @@ export class Scene implements IScene { return this._services.tryResolve(systemType) as T | null; } + /** + * 标记系统顺序为脏 + * + * 当系统列表或顺序发生变化时调用,使缓存失效 + */ + public markSystemsOrderDirty(): void { + this._systemsOrderDirty = true; + } /** * 获取场景的服务容器 @@ -266,6 +294,10 @@ export class Scene implements IScene { // 清空服务容器(会调用所有服务的dispose方法,包括所有EntitySystem) this._services.clear(); + // 清空系统缓存 + this._cachedSystems = null; + this._systemsOrderDirty = true; + // 调用卸载方法 this.unload(); } @@ -599,6 +631,9 @@ export class Scene implements IScene { this._services.registerInstance(constructor, system); + // 标记系统列表已变化 + this.markSystemsOrderDirty(); + injectProperties(system, this._services); system.initialize(); @@ -672,6 +707,9 @@ export class Scene implements IScene { // 从ServiceContainer移除 this._services.unregister(constructor); + // 标记系统列表已变化 + this.markSystemsOrderDirty(); + // 重置System状态 processor.reset(); } diff --git a/packages/core/src/ECS/Systems/EntitySystem.ts b/packages/core/src/ECS/Systems/EntitySystem.ts index 601c9819..de07a734 100644 --- a/packages/core/src/ECS/Systems/EntitySystem.ts +++ b/packages/core/src/ECS/Systems/EntitySystem.ts @@ -222,7 +222,9 @@ export abstract class EntitySystem< * @param order 更新时序 */ public setUpdateOrder(order: number): void { + if (this._updateOrder === order) return; this._updateOrder = order; + this._scene?.markSystemsOrderDirty(); } /**