Files
OpenFrontIO/src/core
Mike Zaugg 23b8eaa04f perf(AttackExecution): migrate hot loops to forEachNeighbor (#3820)
Resolves [#3815
](https://github.com/openfrontio/OpenFrontIO/issues/3815)
   
## Description:
Migrates the five `mg.neighbors(tile)` call sites in
`src/core/execution/AttackExecution.ts` to the existing zero-allocation
`forEachNeighbor(tile, cb)` helper (`src/core/game/GameImpl.ts:1107`).
`neighbors()` allocates
a fresh `TileRef[]` of length up to 4 on every call; `forEachNeighbor`
invokes a callback with no allocation. The helper is already used in
`PlayerExecution`, `GameImpl.updateBorders`, and similar hot paths —
this PR
  finishes that rollout in the hottest attack loop.

  Affected sites in `src/core/execution/AttackExecution.ts`:

  | Original line | Context |
  | --- | --- |
  | 277 | `tick()` border check around `tileToConquer` |
  | 326 | `addNeighbors()` outer neighbor loop |
  | 335 | `addNeighbors()` nested `numOwnedByMe` count |
  | 370 | `handleDeadDefender()` border test |
  | 375 | `handleDeadDefender()` neighbor scan |

  Notes:

- `handleDeadDefender` previously narrowed `this.target` from `Player |
TerraNullius` to `Player` via the early-return check. Inside an arrow
callback that narrowing isn't preserved by TypeScript, so the function
now
captures `target: Player` once after the check. Same pattern as
`tick()`'s `targetPlayer` cache.
- For loops that previously used `break` or `.some()`, I used a small
flag variable to short-circuit the callback body. With at most 4
neighbors per tile the extra callback invocations are negligible
compared to the
  eliminated array allocation.

  Behavioral guarantees:

- Iteration order is identical (`forEachNeighbor` enumerates the same
tiles in the same order as `neighbors()`).
  - No RNG, no float math, no wire-format changes.
  - Determinism preserved.

  Verification:

- `npm test` — all 994 tests pass across 104 files. The 22
attack-related tests in `Attack.test.ts`, `AttackStats.test.ts`, and
`AiAttackBehavior.test.ts` exercise the migrated code paths end-to-end.
  - `npx tsc --noEmit` — clean for the changed file.
  - `npx prettier --check` — clean.

Briefly flagged in `#development` per the contribution guidelines before
opening.

  ## Please complete the following:

  - [ ] I have added screenshots for all UI updates
- [ ] I process any text displayed to the user through translateText()
and I've added it to the en.json file
  - [ ] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

sxndrexe
2026-05-06 12:44:08 -06:00
..
2026-03-17 15:55:47 -07:00
2026-05-01 18:39:23 -06:00
2025-05-12 11:51:40 -07:00
2025-07-15 00:41:24 -04:00
2026-04-16 13:18:55 -07:00
2026-03-24 12:53:32 -07:00
2025-07-23 14:28:38 -04:00