Conversation
Goal: Optimized NukeExecution.detonate() to cut per-tile allocations by caching config, per-tile numTilesOwned, and per-owner TransportShip lists via a Map (src/core/execution/NukeExecution.ts:250). Microbench: tests/perf/NukeDetonatePerf.ts (PERF_WARMUP=0 PERF_RUNS=2 PERF_OUTGOING_ATTACKS=200 PERF_TRANSPORT_SHIPS=200) avgMs 264.463 -> 83.049 (p50 264.463 -> 83.049)
Goal: Avoid per-tick potentialTargets allocation + sort by selecting best target in one pass (src/core/execution/WarshipExecution.ts:78). Microbench: tests/perf/WarshipFindTargetPerf.ts (PERF_WARMUP=0 PERF_RUNS=3 PERF_ITERS=2000 PERF_SHIPS=1500) avgMs 1978.341 -> 431.092 (p50 1968.576 -> 420.816)
Goal: Avoid per-call Set creation and reduce call overhead by fast-pathing single-type queries + caching query coords (src/core/game/UnitGrid.ts:135). Microbench: tests/perf/UnitGridNearbyUnitsPerf.ts (PERF_WARMUP=0 PERF_RUNS=3 PERF_ITERS=5000 PERF_UNITS=2000) avgMs 395.661 -> 371.690 (p50 405.020 -> 365.366)
Goal: Avoid Set allocation and filter callback overhead for common 1–3 type queries (src/core/game/PlayerImpl.ts:216). Microbench: tests/perf/PlayerUnitsPerf.ts (PERF_WARMUP=0 PERF_RUNS=3 PERF_ITERS=20000 PERF_UNITS=5000) avgMs 5355.536 -> 3142.176 (p50 4939.107 -> 3152.285)
Goal: Reuse per-UnitType UnitInfo objects to avoid repeated object + closure creation in hot paths (src/core/configuration/DefaultConfig.ts:349). Microbench: tests/perf/UnitInfoPerf.ts (PERF_WARMUP=0 PERF_RUNS=3 PERF_ITERS=20000) avgMs 13.857 -> 3.240 (p50 12.118 -> 2.233)
Goal: Avoid targets.sort + reduce per-trajectory overhead by precomputing SAM rangeSquared and using single-pass Hydrogen priority (src/core/execution/SAMLauncherExecution.ts:27). Microbench: tests/perf/SAMTargetingPerf.ts (PERF_WARMUP=0 PERF_RUNS=3 PERF_ITERS=2000 PERF_TRAJ_LEN=80) avgMs 4518.498 -> 4363.653 (p50 4597.366 -> 4356.109)
WalkthroughIntroduces caching for unit info, several performance optimizations in execution and game layers (reducing allocations and repeated property access), and refactors targeting logic to pick a single best target instead of building/sorting candidate lists. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✏️ 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. Comment |
There was a problem hiding this comment.
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/UnitGrid.ts`:
- Around line 180-183: TypeScript isn't narrowing the union for `types` before
it's used in the nested loops (causing TS2345 on `Map.get`), so add an explicit
assertion to treat it as a single UnitType; change the local binding used in the
loops (the `type` constant derived from `types`) to be asserted as `UnitType`
(or assert at the `get` call) so `this.grid[cy][cx].get(...)` receives a
`UnitType` rather than `UnitType | readonly UnitType[]`; update the binding
where `const type = types` is declared to use the assertion (or replace the
argument to `get` with `types as UnitType`) to satisfy the type checker.
Switch nearbyUnits’ type-guard from Array.isArray to typeof types !== "string" so types: UnitType | readonly UnitType[] narrows reliably and the single-type path only ever passes a UnitType into Map.get.
If this PR fixes an issue, link it below. If not, delete these two lines.
Resolves #(issue number)
Description:
PR Title
perf(core): reduce hot-path allocations & safe optimizations
This PR brings in a set of allocation-focused optimizations in core hot paths
Scope
src/core/execution/NukeExecution.tssrc/core/execution/WarshipExecution.tssrc/core/game/UnitGrid.tssrc/core/game/PlayerImpl.tssrc/core/configuration/DefaultConfig.tssrc/core/execution/SAMLauncherExecution.tsWhat Changed
NukeExecution.detonate: reduced call overhead/allocations by cachingmg/config, avoiding repeated lookups, and using allocation-free loops (noforEachclosures) in the diminishing-effect pass.WarshipExecution.findTargetUnit: replaced allocate+sort flow with single-pass best-target selection.UnitGrid.nearbyUnits: reduced call overhead and allocations via single-type fast path and cached query coordinates.PlayerImpl.units: added fast paths for common small-arity type queries (1-3 unit types).DefaultConfig.unitInfo: cachedUnitInfoobjects perUnitTypeto avoid repeated object/closure creation.SAMLauncherExecutiontargeting: removed sort churn and streamlined target selection with single-pass hydrogen prioritization.Rebase
NukeExecution.detonateby keepingmain's diminishing-effect-per-impacted-tile behavior, while retaining the allocation-reduction refactors.Please complete the following:
Please put your Discord username so you can be contacted if a bug or regression is found:
DISCORD_USERNAME