Skip to content

perf(pathfinder): Skip redundant path-exists check in checkForAdjust#2791

Open
stephanmeesters wants to merge 1 commit into
TheSuperHackers:mainfrom
stephanmeesters:perf/pathfinding-checkforadjust
Open

perf(pathfinder): Skip redundant path-exists check in checkForAdjust#2791
stephanmeesters wants to merge 1 commit into
TheSuperHackers:mainfrom
stephanmeesters:perf/pathfinding-checkforadjust

Conversation

@stephanmeesters

@stephanmeesters stephanmeesters commented Jun 14, 2026

Copy link
Copy Markdown

In Pathfinder::checkForAdjust there could be an unnecessary computation of adjustedPathExists because it could be overwritten later.

The gain is minor: adjustedPathExists takes around 200 +- 150 ns, but can be called many times in an update frame

@greptile-apps

greptile-apps Bot commented Jun 14, 2026

Copy link
Copy Markdown

Greptile Summary

This PR optimizes Pathfinder::checkForAdjust by eliminating a redundant clientSafeQuickDoesPathExist call in the non-aircraft code path.

  • Previously, adjustedPathExists (path from object position to the adjusted destination) was always computed eagerly, then unconditionally overwritten to true when !pathExists && C — wasting one pathfinding query in that branch. The new code short-circuits: only compute the adjusted-path check when the override condition is false.
  • The logic is semantically identical across all three cases (pathExists=T, pathExists=F && C=T, pathExists=F && C=F), and adjustedPathExists is always initialized through the exhaustive if/else branches.

Confidence Score: 5/5

Safe to merge — the refactored branch produces identical Boolean outcomes across all three input combinations while skipping one pathfinding call in the !pathExists && dest→adjustDest case.

The logic change is a straightforward short-circuit optimization. All three cases (path exists, path missing with fallback reachable, path missing with fallback unreachable) were verified to yield the same value for adjustedPathExists as the original code. adjustedPathExists is always initialized through the exhaustive if/else, and the scope of pathExists is cleanly narrowed to the non-aircraft branch.

No files require special attention.

Important Files Changed

Filename Overview
Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp Refactors checkForAdjust to skip a redundant path-existence query; logic is equivalent to the original across all three Boolean cases, and adjustedPathExists is always initialized.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[checkForAdjust called] --> B{obj is AIRCRAFT?}
    B -- Yes --> C[adjustedPathExists = true]
    B -- No --> D["pathExists = doesPathExist(pos → dest)"]
    D --> E{"!pathExists AND\ndoesPathExist(dest → adjustDest)?"}
    E -- Yes --> F[adjustedPathExists = true\n⚡ Skip redundant B call]
    E -- No --> G["adjustedPathExists =\ndoesPathExist(pos → adjustDest)"]
    C --> H{adjustedPathExists?}
    F --> H
    G --> H
    H -- Yes --> I["*dest = adjustDest\nreturn true"]
    H -- No --> J[return false]

    style F fill:#d4edda,stroke:#28a745
    style G fill:#fff3cd,stroke:#ffc107
Loading

Reviews (1): Last reviewed commit: "perf(pathfinding): Skip redundant path-e..." | Re-trigger Greptile

@stephanmeesters stephanmeesters changed the title perf(pathfinding): Skip redundant path-exists check in checkForAdjust perf(pathfinder): Skip redundant path-exists check in checkForAdjust Jun 14, 2026
@xezon xezon added Minor Severity: Minor < Major < Critical < Blocker Performance Is a performance concern Gen Relates to Generals ZH Relates to Zero Hour labels Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Gen Relates to Generals Minor Severity: Minor < Major < Critical < Blocker Performance Is a performance concern ZH Relates to Zero Hour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants