Skip to content

Comments

Perf/Fix: spawn and other functions that need closest by unit#3243

Open
VariableVince wants to merge 11 commits intomainfrom
spawn-perf
Open

Perf/Fix: spawn and other functions that need closest by unit#3243
VariableVince wants to merge 11 commits intomainfrom
spawn-perf

Conversation

@VariableVince
Copy link
Contributor

@VariableVince VariableVince commented Feb 19, 2026

Description:

Performance improvements.

  • PlayerImpl: for nukeSpawn, cache config to const.
  • Other files: for nukeSpawn and other functions doing the same, introduce findClosestBy function.
  • for TradeShipExecution, with the move from distSortUnit to findClosestBy, also add check if port isActive, !isMarkedForDeletion and !isUnderConstruction. These checks should have been there already, so now do it in one go to make use of the predicate isCandidate in findClosestBy.
  • for TradeShipExectution.test.ts, add mock functions for isMarkedForDeletion and isUnderConstruction because of the above. Also, set Unit tiles and Pathfinding node to actual valid TileRefs for the testing map. This prevents NaN as return value from manhattanDist. This problem was already present with the use of distSortUnit, but that function just did NaN - NaN, returned the first and only port unit in the array and called it a day. For findClosestBy we have to make sure the predicate manhattanDist actually returns a number instead of NaN so we need actually valid tiles. We now have a working test instead of a test that actually silently failed like before.
  • PlayerImpl: warshipSpawn and nukeSpawn: Make use of the isCandidate predicate of findClosestBy to have warshipSpawn not return ports under construction or (smaller change) inactive. This fixes a bug i have seen right away (where Warship spawns from under construction Port).
    Same for nukeSpawn silos, don't return inactive silo just to be sure now that we can easily add it to isCandidate predicate anyway. This costs no performance in the nukeSpawn benchmarks actually. This should as a by-effecft fix an edge case bug i have seen, where a nuke is sent from a phantom silo.

Some of this goes along with PR #3220 since playerImpl buildableUnits makes use of the underlying spawn functions via canBuild. Just like ConstructionExecution does. But i didn't want to add more to PR 3220 since there's already a lot in there.

The new function findClosestBy could also be applied to some other parts of code to benefit of it being faster, so i did that.

findClosestBy uses findMinimumBy, which is a little more generic in name. I think findMinimumBy could be used by other parts of code, while findClosestBy is more clear naming for what it does now. But we could ditch findMinimumBy and just leave findClosestBy?

Examples of synthetic benchmarks (not included in this PR):

BEFORE CHANGES (before Scamiv's PR #3241)
image

image

AFTER CHANGES (before Scamiv's PR #3241)
findunittoupgrade for loop 5th run

nukespawn for loop 3rd run

BEFORE CHANGES (after Scamiv's PR #3241)
findunittoupgrade BEFORE

nukespawn BEFORE

AFTER CHANGES (after Scamiv's PR #3241)
image

image

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
  • 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:

tryout33

@VariableVince VariableVince added this to the v30 milestone Feb 19, 2026
@VariableVince VariableVince self-assigned this Feb 19, 2026
@VariableVince VariableVince requested a review from a team as a code owner February 19, 2026 05:53
@VariableVince VariableVince added the Performance Performance optimization label Feb 19, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 19, 2026

Walkthrough

Adds generic utilities findMinimumBy and findClosestBy, replaces ad-hoc nearest-item sorts with findClosestBy across client and core code, makes selection results null-safe, and updates related tests/mocks and spawn/return-port logic.

Changes

Cohort / File(s) Summary
Core Utilities
src/core/Util.ts
Adds exported findMinimumBy<T> and findClosestBy<T> helpers that return the best item or null, with optional candidate predicates.
Client selection logic
src/client/ClientGameRunner.ts, src/client/graphics/layers/RadialMenuElements.ts
Replaces manual sort-by-distance selections with findClosestBy; adds null checks before emitting upgrade events or removing units.
Executions: Trade ship return
src/core/execution/TradeShipExecution.ts
Switches port selection from sorted lists to findClosestBy; reads ship current tile earlier and handles missing port by deleting the ship or early returning.
Game: Player spawn & upgrade
src/core/game/PlayerImpl.ts
Refactors nearest-unit/port/missile-silo/warship selection to use findClosestBy; caches config/vars and consolidates owner/team checks; spawn functions return tile or false.
Tests (mocks)
tests/core/executions/TradeShipExecution.test.ts
Updates mock tiles, adds a second pirate port, and aligns pathfinding/tradeShip node values to match nearest-port assumptions.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

Finder walks the scattered land,
Picks the nearest with a steady hand.
Null-safety keeps the flow serene,
Mocks adjusted, tests convene,
Small util, big tidy scene. 🧭

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Perf/Fix: spawn and other functions that need closest by unit' accurately describes the main changes—introducing findClosestBy utility and applying it to spawn functions and other selection logic for performance improvement.
Description check ✅ Passed The pull request description clearly outlines performance improvements, including new utility functions (findClosestBy, findMinimumBy), their application across multiple files, test updates, and bug fixes related to port/silo selection logic.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/core/Util.ts (1)

95-101: findClosestBy is a thin alias over findMinimumBy — consider whether both exports are needed long-term.

Right now findClosestBy just forwards to findMinimumBy with zero added logic. The only difference is the parameter name (distance vs score). This is fine for readability at call sites — "closest by distance" reads more naturally than "minimum by score" when dealing with spatial lookups. Just keep in mind that maintaining two nearly identical public exports adds surface area. If findMinimumBy never gets used directly outside of findClosestBy, you could collapse them later.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/core/Util.ts` around lines 95 - 101, findClosestBy is a trivial wrapper
around findMinimumBy with no added behavior (only a parameter rename), which
risks duplicated public surface area; either remove the redundant export or make
the intent explicit: search the repo for external usages of findClosestBy and
findMinimumBy and if both are used keep both, otherwise collapse into a single
export (prefer the clearer name for call sites, e.g., keep findClosestBy and
remove or inline findMinimumBy), or add a short comment above findClosestBy
documenting it's an intentional alias for readability; update any imports
accordingly to use the chosen single function (references: findClosestBy,
findMinimumBy).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/core/Util.ts`:
- Around line 95-101: findClosestBy is a trivial wrapper around findMinimumBy
with no added behavior (only a parameter rename), which risks duplicated public
surface area; either remove the redundant export or make the intent explicit:
search the repo for external usages of findClosestBy and findMinimumBy and if
both are used keep both, otherwise collapse into a single export (prefer the
clearer name for call sites, e.g., keep findClosestBy and remove or inline
findMinimumBy), or add a short comment above findClosestBy documenting it's an
intentional alias for readability; update any imports accordingly to use the
chosen single function (references: findClosestBy, findMinimumBy).

coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 19, 2026
@evanpelle
Copy link
Collaborator

looks like a test is failing

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/core/Util.ts (1)

110-116: findClosestBy is a pure alias — consider whether it pulls its own weight.

Right now findClosestBy adds no behaviour on top of findMinimumBy; they share the same signature and the body is a single delegation line. Two options worth considering:

  1. Keep as-is — the distinct name signals "this is a spatial distance query" at call sites, which is fine.
  2. Remove findClosestBy and have callers import findMinimumBy directly, reducing surface area.

Either is valid; this is just a nudge to settle the "possible consolidation" point the PR already raises before more callers are added.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/core/Util.ts` around lines 110 - 116, findClosestBy is a no-op alias of
findMinimumBy; decide whether to remove it or clarify intent: either delete the
findClosestBy function and replace all its call sites/imports to use
findMinimumBy (update tests and exports accordingly), or keep findClosestBy but
add a brief doc comment above the function explaining it's a semantic alias for
spatial/distance queries so callers understand the intent and keep the
single-line delegation as-is; reference the symbols findClosestBy and
findMinimumBy when making the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/core/Util.ts`:
- Around line 102-109: The JSDoc example for findClosestBy uses the incorrect
GameMap variable name "mg" — change it to "gm" to match the file's convention
and other helpers like distSort and distSortUnit; update the example line
containing mg.manhattanDist(...) to use gm.manhattanDist(...) so examples are
consistent and copy-pasteable for readers referencing findClosestBy.

---

Nitpick comments:
In `@src/core/Util.ts`:
- Around line 110-116: findClosestBy is a no-op alias of findMinimumBy; decide
whether to remove it or clarify intent: either delete the findClosestBy function
and replace all its call sites/imports to use findMinimumBy (update tests and
exports accordingly), or keep findClosestBy but add a brief doc comment above
the function explaining it's a semantic alias for spatial/distance queries so
callers understand the intent and keep the single-line delegation as-is;
reference the symbols findClosestBy and findMinimumBy when making the change.

@github-project-automation github-project-automation bot moved this from Triage to Development in OpenFront Release Management Feb 20, 2026
coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 20, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
src/core/game/PlayerImpl.ts (1)

1183-1193: warshipSpawn still uses this.mg instead of a local cache.

Not a bug, but nukeSpawn caches this.mg into a local mg for speed, while warshipSpawn does not. If this method is also on a hot path, the same local-cache pattern would be consistent. Low priority since warshipSpawn does fewer lookups.

Optional: cache mg locally for consistency
 warshipSpawn(tile: TileRef): TileRef | false {
-    if (!this.mg.isOcean(tile)) {
+    const mg = this.mg;
+    if (!mg.isOcean(tile)) {
       return false;
     }
 
-    const bestPort = findClosestBy(this.units(UnitType.Port), (port) =>
-      this.mg.manhattanDist(port.tile(), tile),
+    const bestPort = findClosestBy(this.units(UnitType.Port), (port) =>
+      mg.manhattanDist(port.tile(), tile),
     );
 
     return bestPort?.tile() ?? false;
   }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/core/game/PlayerImpl.ts` around lines 1183 - 1193, warshipSpawn currently
accesses this.mg repeatedly; mirror the nukeSpawn optimization by caching
this.mg into a local const (e.g., const mg = this.mg) at the top of the
warshipSpawn method and then use mg.isOcean and mg.manhattanDist in the
findClosestBy callback (leave findClosestBy, units(UnitType.Port), and
UnitType.Port unchanged) so lookups are consistent and slightly faster on hot
paths.
src/core/Util.ts (1)

102-116: findClosestBy is a pure alias for findMinimumBy — consider if both are needed.

Right now findClosestBy just calls findMinimumBy with identical signature/behavior. The only difference is the parameter name (distance vs score). This is fine for readability at call sites, but be aware the two could drift apart if someone later adds logic to only one of them.

If the intent is purely a naming alias, a brief inline comment like // Semantic alias – keep in sync with findMinimumBy would help future readers.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/core/Util.ts` around lines 102 - 116, findClosestBy currently just
forwards to findMinimumBy and should be documented as an intentional semantic
alias to avoid future drift; update the findClosestBy declaration to include a
short inline comment like "// Semantic alias of findMinimumBy — keep
behavior/signature in sync" (or alternatively remove the alias and replace call
sites with findMinimumBy if duplication isn't desired), referencing the
functions findClosestBy and findMinimumBy so readers know this is deliberate.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/core/game/PlayerImpl.ts`:
- Around line 1183-1193: warshipSpawn currently accesses this.mg repeatedly;
mirror the nukeSpawn optimization by caching this.mg into a local const (e.g.,
const mg = this.mg) at the top of the warshipSpawn method and then use
mg.isOcean and mg.manhattanDist in the findClosestBy callback (leave
findClosestBy, units(UnitType.Port), and UnitType.Port unchanged) so lookups are
consistent and slightly faster on hot paths.

In `@src/core/Util.ts`:
- Around line 102-116: findClosestBy currently just forwards to findMinimumBy
and should be documented as an intentional semantic alias to avoid future drift;
update the findClosestBy declaration to include a short inline comment like "//
Semantic alias of findMinimumBy — keep behavior/signature in sync" (or
alternatively remove the alias and replace call sites with findMinimumBy if
duplication isn't desired), referencing the functions findClosestBy and
findMinimumBy so readers know this is deliberate.

@VariableVince VariableVince marked this pull request as draft February 22, 2026 01:47
…or map "ocean_and_land" which lead to NaN as outcome of manhattanDist. Until now we used distSortUnit which just returned the first and only port it was given in the test, even though manhattanDist both returned NaN. Now with findClosestBy we need it to have actual existing tiles because we need actual comparable number to come out of manhattanDist.)

Also for TradeShipExecution use some already cached values as input for findClosestBy.
coderabbitai[bot]

This comment was marked as resolved.

…eletion, still under construction (even if it might be finished when we arrive, we don't know that) or is inactive.
@VariableVince VariableVince marked this pull request as ready for review February 22, 2026 04:09
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/core/execution/TradeShipExecution.ts`:
- Around line 87-103: The port mocks used in tests (dstPort, piratePort,
piratePort2, srcPort) are missing stubs for Unit methods isMarkedForDeletion()
and isUnderConstruction(), causing CI failures when TradeShipExecution calls
these; update the test beforeEach where those port mocks are created to add
vi.fn() stubs for both methods on each mock (e.g., isMarkedForDeletion: vi.fn(()
=> false), isUnderConstruction: vi.fn(() => false)) so findClosestBy/filtering
in TradeShipExecution works as expected.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@src/core/execution/TradeShipExecution.ts`:
- Around line 87-103: Tests are failing because the port mocks used in
TradeShipExecution.test.ts lack the new filter-method stubs; in the beforeEach
add isMarkedForDeletion() and isUnderConstruction() stubs on each port mock
(dstPort, srcPort, piratePort, piratePort2) returning appropriate values (e.g.,
false) so findClosestBy's filter in TradeShipExecution (which calls
isMarkedForDeletion and isUnderConstruction) can run without error.

coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 22, 2026
…d unit, if it can be upgraded. So canUpgradeUnit should not be send as a predicate for isCandidate in findClosestBy, but only be checked afterwards for the one found unit.

This makes findClosestBy faster and with that findUnitToUpgrade so it's only good.
coderabbitai[bot]

This comment was marked as resolved.

coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 22, 2026
…pSpawn not return ports under construction or (smaller change) inactive. This fixes a bug right away.

Same for nukeSpawn silos, don't return inactive silo just to be sure now that we can easily add it to isCandidate predicate anyway
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/core/game/PlayerImpl.ts (1)

992-1001: Consider using isCandidate for parity with nukeSpawn/warshipSpawn.

Both nukeSpawn and warshipSpawn pass eligibility checks as the isCandidate predicate so that findClosestBy skips ineligible candidates early and returns the closest valid target. Here, the predicate is absent — findClosestBy returns the single closest unit regardless of eligibility, and if that unit fails canUpgradeUnit (e.g., it belongs to another player, is under construction, or is marked for deletion) the call returns false even if a slightly-farther eligible unit exists.

In practice this is rare because structureMinDist spacing makes it unlikely for two same-type units to coexist within the search range, but it creates a pattern inconsistency and a latent correctness gap.

♻️ Proposed refactor using `isCandidate`
  public findUnitToUpgrade(type: UnitType, targetTile: TileRef): Unit | false {
    const range = this.mg.config().structureMinDist();
-   const closest = findClosestBy(
+   const closest = findClosestBy(
      this.mg.nearbyUnits(targetTile, range, type, undefined, true),
      (entry) => entry.distSquared,
+     (entry) => this.canUpgradeUnit(entry.unit),
    );
-
-   return closest === null
-     ? false
-     : this.canUpgradeUnit(closest.unit)
-       ? closest.unit
-       : false;
+   return closest?.unit ?? false;
  }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/core/game/PlayerImpl.ts` around lines 992 - 1001, findClosestBy is
currently called without a predicate so it returns the nearest unit even if
ineligible; replace that with the same isCandidate-style predicate used by
nukeSpawn/warshipSpawn so findClosestBy skips ineligible units early.
Specifically, call findClosestBy(this.mg.nearbyUnits(targetTile, range, type,
undefined, true), entry => ({ pass: this.canUpgradeUnit(entry.unit), key:
entry.distSquared })) or use the existing isCandidate helper pattern so only
units passing canUpgradeUnit(...) are considered, ensuring the returned closest
is a valid upgradable unit instead of post-filtering.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/core/game/PlayerImpl.ts`:
- Around line 1155-1158: Prettier flagged formatting around the findClosestBy
call: run npx prettier --write on the file and reformat the call to
findClosestBy that uses this.units(UnitType.MissileSilo) and the two predicate
functions (the distance extractor using mg.manhattanDist(silo.tile(), tile) and
the boolean check using silo.isActive(), silo.isInCooldown(),
silo.isUnderConstruction()) so the long single-line predicate is
wrapped/indented per Prettier rules and the file passes CI.

---

Nitpick comments:
In `@src/core/game/PlayerImpl.ts`:
- Around line 992-1001: findClosestBy is currently called without a predicate so
it returns the nearest unit even if ineligible; replace that with the same
isCandidate-style predicate used by nukeSpawn/warshipSpawn so findClosestBy
skips ineligible units early. Specifically, call
findClosestBy(this.mg.nearbyUnits(targetTile, range, type, undefined, true),
entry => ({ pass: this.canUpgradeUnit(entry.unit), key: entry.distSquared })) or
use the existing isCandidate helper pattern so only units passing
canUpgradeUnit(...) are considered, ensuring the returned closest is a valid
upgradable unit instead of post-filtering.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/core/game/PlayerImpl.ts`:
- Around line 992-1001: findUnitToUpgrade currently calls findClosestBy over
this.mg.nearbyUnits without supplying an isCandidate predicate, so it picks the
nearest unit even if not upgradeable; change the findClosestBy invocation in
findUnitToUpgrade to pass an isCandidate function that returns true only when
this.canUpgradeUnit(entry.unit) is true (use the same entry.distSquared for the
comparator), so findClosestBy returns the closest eligible unit (or null) and
then return that unit or false as before.

Comment on lines +992 to +1001
const closest = findClosestBy(
this.mg.nearbyUnits(targetTile, range, type, undefined, true),
(entry) => entry.distSquared,
);

return closest === null
? false
: this.canUpgradeUnit(closest.unit)
? closest.unit
: false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

findUnitToUpgrade returns false for the wrong reason when the closest unit is not upgradable

findClosestBy has no isCandidate predicate here, so it always returns the nearest unit regardless of eligibility. canUpgradeUnit is then checked only on that single result. If the nearest unit is under construction (or marked for deletion, or otherwise ineligible), the function returns false — even when a slightly farther unit would have been eligible.

The isCandidate slot exists precisely for this: skip ineligible entries during the scan so the closest eligible unit is returned.

🐛 Proposed fix
  public findUnitToUpgrade(type: UnitType, targetTile: TileRef): Unit | false {
    const range = this.mg.config().structureMinDist();
    const closest = findClosestBy(
      this.mg.nearbyUnits(targetTile, range, type, undefined, true),
      (entry) => entry.distSquared,
+     (entry) => this.canUpgradeUnit(entry.unit),
    );

-   return closest === null
-     ? false
-     : this.canUpgradeUnit(closest.unit)
-       ? closest.unit
-       : false;
+   return closest?.unit ?? false;
  }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/core/game/PlayerImpl.ts` around lines 992 - 1001, findUnitToUpgrade
currently calls findClosestBy over this.mg.nearbyUnits without supplying an
isCandidate predicate, so it picks the nearest unit even if not upgradeable;
change the findClosestBy invocation in findUnitToUpgrade to pass an isCandidate
function that returns true only when this.canUpgradeUnit(entry.unit) is true
(use the same entry.distSquared for the comparator), so findClosestBy returns
the closest eligible unit (or null) and then return that unit or false as
before.

@VariableVince VariableVince changed the title Performance improvements Perf/Fix: spawn and other functions that need closest by unit Feb 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Performance Performance optimization

Projects

Status: Development

Development

Successfully merging this pull request may close these issues.

2 participants