From e7801562c3e5b4ef4f7550a34e3c16fecc99c66b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 19 May 2026 23:15:01 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20production=20loo?= =?UTF-8?q?p=20and=20population=20calculation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Update `TurnEngine.ts` to use pre-calculated `BUILDING_TYPES_SET` and `UNIT_TYPES_SET` for O(1) lookups during construction processing, avoiding redundant `Object.values()` calls and array allocations. - Refactor `calculatePopulation` in `Settlement.ts` to use a manual loop with a counter instead of `.filter().length`, eliminating intermediate array allocations in a frequently called logic path. --- src/shared/game/entities/Settlement.ts | 20 +++++++++++++------- src/shared/game/systems/TurnEngine.ts | 8 ++++++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/shared/game/entities/Settlement.ts b/src/shared/game/entities/Settlement.ts index 5a1aa1c..986032f 100644 --- a/src/shared/game/entities/Settlement.ts +++ b/src/shared/game/entities/Settlement.ts @@ -65,13 +65,19 @@ export function createSettlement( * - Available units (RURE occupation) do not count. */ export function calculatePopulation(settlement: Settlement): number { - return settlement.units.filter((u) => { - const occ = u.occupation; + // ⚡ Bolt: Use a simple loop with a counter instead of filter().length to avoid intermediate array allocation. + let count = 0; + for (const unit of settlement.units) { + const occ = unit.occupation; // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - if (!occ) return false; + if (!occ) continue; // JobType is a string - if (typeof occ === 'string') return true; - // FieldWork and Rure are objects - return occ.kind === 'FIELD_WORK'; - }).length; + if (typeof occ === 'string') { + count++; + } else if (occ.kind === 'FIELD_WORK') { + // FieldWork is an object + count++; + } + } + return count; } diff --git a/src/shared/game/systems/TurnEngine.ts b/src/shared/game/systems/TurnEngine.ts index ddd64e8..29103df 100644 --- a/src/shared/game/systems/TurnEngine.ts +++ b/src/shared/game/systems/TurnEngine.ts @@ -16,6 +16,9 @@ export interface TurnEngineResult { readonly effects: readonly GameEffect[]; } +const BUILDING_TYPES_SET = new Set(Object.values(BuildingType)); +const UNIT_TYPES_SET = new Set(Object.values(UnitType)); + /* eslint-disable-next-line @typescript-eslint/no-extraneous-class */ export class TurnEngine { private constructor() { @@ -155,8 +158,9 @@ export class TurnEngine { return currentNamingStats; } - const isBuilding = Object.values(BuildingType).includes(currentItem as BuildingType); - const isUnit = Object.values(UnitType).includes(currentItem as UnitType); + // ⚡ Bolt: Use pre-calculated Sets for O(1) lookup instead of creating new arrays with Object.values() + const isBuilding = BUILDING_TYPES_SET.has(currentItem); + const isUnit = UNIT_TYPES_SET.has(currentItem); const cost = this.getProductionCost(currentItem, isBuilding); if (!this.canAffordConstruction(settlement, cost)) {