From e2842ade3fda3ccc74693a1541c48ecc2e00e42c Mon Sep 17 00:00:00 2001 From: Erik Onarheim Date: Fri, 1 Nov 2024 22:32:31 -0500 Subject: [PATCH] perf: Low hanging perf loop changes --- src/engine/Debug/DebugSystem.ts | 3 ++- src/engine/Graphics/GraphicsSystem.ts | 6 ++++-- src/engine/Graphics/OffscreenSystem.ts | 3 ++- src/engine/Scene.ts | 15 ++++++++------- src/engine/TileMap/IsometricEntitySystem.ts | 3 ++- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/engine/Debug/DebugSystem.ts b/src/engine/Debug/DebugSystem.ts index 81e09cff..3aba0494 100644 --- a/src/engine/Debug/DebugSystem.ts +++ b/src/engine/Debug/DebugSystem.ts @@ -70,7 +70,8 @@ export class DebugSystem extends System { const bodySettings = this._engine.debug.body; const cameraSettings = this._engine.debug.camera; - for (const entity of this.query.entities) { + for (let i = 0; i < this.query.entities.length; i++) { + const entity = this.query.entities[i]; if (entity.hasTag('offscreen')) { // skip offscreen entities continue; diff --git a/src/engine/Graphics/GraphicsSystem.ts b/src/engine/Graphics/GraphicsSystem.ts index 4279d296..484868ea 100644 --- a/src/engine/Graphics/GraphicsSystem.ts +++ b/src/engine/Graphics/GraphicsSystem.ts @@ -252,7 +252,8 @@ export class GraphicsSystem extends System { */ private _applyTransform(entity: Entity): void { const ancestors = entity.getAncestors(); - for (const ancestor of ancestors) { + for (let i = 0; i < ancestors.length; i++) { + const ancestor = ancestors[i]; const transform = ancestor?.get(TransformComponent); const optionalBody = ancestor?.get(BodyComponent); if (transform) { @@ -274,7 +275,8 @@ export class GraphicsSystem extends System { private _applyOpacity(entity: Entity): void { const ancestors = entity.getAncestors(); - for (const ancestor of ancestors) { + for (let i = 0; i < ancestors.length; i++) { + const ancestor = ancestors[i]; const maybeGraphics = ancestor?.get(GraphicsComponent); this._graphicsContext.opacity *= maybeGraphics?.opacity ?? 1; } diff --git a/src/engine/Graphics/OffscreenSystem.ts b/src/engine/Graphics/OffscreenSystem.ts index eca38e00..69840688 100644 --- a/src/engine/Graphics/OffscreenSystem.ts +++ b/src/engine/Graphics/OffscreenSystem.ts @@ -36,7 +36,8 @@ export class OffscreenSystem extends System { let graphics: GraphicsComponent; let maybeParallax: ParallaxComponent | undefined; - for (const entity of this.query.entities) { + for (let i = 0; i < this.query.entities.length; i++) { + const entity = this.query.entities[i]; graphics = entity.get(GraphicsComponent); transform = entity.get(TransformComponent); maybeParallax = entity.get(ParallaxComponent); diff --git a/src/engine/Scene.ts b/src/engine/Scene.ts index 8102ff4b..c19af98e 100644 --- a/src/engine/Scene.ts +++ b/src/engine/Scene.ts @@ -718,14 +718,15 @@ export class Scene implements CanInitialize, CanActiv } private _collectActorStats(engine: Engine) { - const screenElements = this.actors.filter((a) => a instanceof ScreenElement) as ScreenElement[]; - for (const _ui of screenElements) { - engine.stats.currFrame.actors.ui++; - } - - for (const actor of this.actors) { + const actors = this.actors; + for (let i = 0; i < actors.length; i++) { + const actor = actors[i]; + if (actor instanceof ScreenElement) { + engine.stats.currFrame.actors.ui++; + } engine.stats.currFrame.actors.alive++; - for (const child of actor.children) { + for (let j = 0; j < actor.children.length; j++) { + const child = actor.children[j]; if (isScreenElement(child as Actor)) { // TODO not true engine.stats.currFrame.actors.ui++; diff --git a/src/engine/TileMap/IsometricEntitySystem.ts b/src/engine/TileMap/IsometricEntitySystem.ts index 6575c5e6..d518dd35 100644 --- a/src/engine/TileMap/IsometricEntitySystem.ts +++ b/src/engine/TileMap/IsometricEntitySystem.ts @@ -16,7 +16,8 @@ export class IsometricEntitySystem extends System { update(): void { let transform: TransformComponent; let iso: IsometricEntityComponent; - for (const entity of this.query.entities) { + for (let i = 0; i < this.query.entities.length; i++) { + const entity = this.query.entities[i]; transform = entity.get(TransformComponent); iso = entity.get(IsometricEntityComponent); -- 2.51.2