Skip to content

perf(pathfinder): Avoid redundant work during fine pathfinding - #3713

Open
Mauller wants to merge 3 commits into
TheSuperHackers:mainfrom
Mauller:codex/pathfinding-fine-pass-optimisations
Open

Mauller wants to merge 3 commits into
TheSuperHackers:mainfrom
Mauller:codex/pathfinding-fine-pass-optimisations

Conversation

@Mauller

@Mauller Mauller commented Nov 21, 2026 •

Copy link
Copy Markdown

This PR was driven by me and reviewed locally,
Codex found a few instances where extra work could be removed.
The details are bellow.


Reduces redundant work during fine pathfinding:

  • Samples and caches parent terrain height only when downhill or cliff checks require it.
  • Defers corridor lookups until a neighbour reaches cost calculation.
  • Calculates the line-of-sight heuristic only when updating a cell’s route.

Synthetic fine-search benchmarks on current `main` measured:

Pathfinding mode Release time reduction Debug time reduction
Retail-compatible 11.0% 8.8%
Non-retail 11.9% 10.6%

Results compare summed median search times across ten scenarios and seven paired rounds. Search traces, cell counts, and path costs remained unchanged. These are fine-search measurements, not full-game timings; replay testing so far has shown no determinism mismatches.

For some extra info;

  • The unnecessary calls to TheTerrainLogic->getGroundHeight on every neighbour cell was a reasonably expensive call to TheTerrainRenderObject->getHeightMapHeight
  • The deferred corridor lookup removes excess work from being performed on paths that haven't even reached the cost requirements yet.
  • Moving the costToGoal allows a cheaper execution path through the code: it can now return early when the existing route is equal or better, without calculating costToGoal

With the extra suggestions from Caball009:

  • defer candidate cost allocation until necessary.
  • reject blocked diagonal steps earlier to prevent unneeded processing.

we get some extra performance squeezed out as shown by Codex bellow.

These are the additional time reductions from the two reviewer changes, compared with the original three optimisations:

Pathfinding mode Additional Release time reduction Additional Debug time reduction
Retail-compatible 2.7% 4.5%
Non-retail 2.2% 5.5%

Synthetic fine-search benchmarks across ten scenarios and seven paired rounds. Search counts, path costs, search traces, and final state hashes remained unchanged. Results measure the two additional changes together, relative to the original three optimisations; individual timings varied.

This next change is more speculative, changing the tunneling early return for an assert could gain some extra performance, but this is likely to be less in a real game.

Pathfinding mode Release time reduction Confirmation run
Retail-compatible 3.2% 3.3%
Non-retail 5.1% 6.6%

Additional improvement over the existing five optimisations, measured across ten synthetic fine-search scenarios and seven paired rounds per run. Search counts, path costs, search traces, and final state hashes remained unchanged. No meaningful Debug improvement was established. Release results assume debug crashing is disabled; these are not full-game timings.

@Mauller Mauller self-assigned this Nov 21, 2026
@Mauller Mauller added Minor Severity: Minor < Major < Critical < Blocker Performance Is a performance concern Gen Relates to Generals ZH Relates to Zero Hour labels Nov 21, 2026
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • No new commits to review - use @coderabbitai full review for a full pass

No actionable comments were generated in the recent review.

Walkthrough

The pathfinding code delays candidate-cost and heuristic calculations until route checks require them. Neighbor evaluation also checks diagonal access before neighbor lookup and samples parent terrain height and ground-zone passability when needed.

Changes

Pathfinding evaluation

Layer / File(s) Summary
Defer pathfinding checks
Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp
Candidate path cost is calculated after cliff and allocation checks. The goal-distance heuristic is calculated when the candidate route is updated. Neighbor evaluation checks diagonal-side availability before neighbor lookup, reuses parent ground height for downhill and cliff checks, and checks ground-zone passability during path-cost calculation. The tunneling check in examineCellsCallback is now a debug assertion instead of an early return.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Refactor

Possibly related issues

Possibly related PRs

Suggested reviewers: skyaero42, caball009, xezon

Merge Risk: Minimal · previous review 225e7

This change reduces redundant work in fine pathfinding without altering intended results. The author reports unchanged paths and costs, and local retail replays have passed so far. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a performance refactor that removes redundant work during fine pathfinding. The raw summary confirms edits to examineCellsCallback and examineNeighboringCells in AIPat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description relates directly to the changeset. It describes the fine-pathfinding optimizations in AIPathfind.cpp: caching parent terrain height for downhill and cliff checks, deferring corridor lo…
Full details: Title check

Explanation

The title names the main change: a performance refactor that removes redundant work during fine pathfinding. The raw summary confirms edits to examineCellsCallback and examineNeighboringCells in AIPathfind.cpp that defer height, cost, and line-of-sight work. The title is specific and matches the changeset and the PR objectives.

Full details: Description check

Explanation

The description relates directly to the changeset. It describes the fine-pathfinding optimizations in AIPathfind.cpp: caching parent terrain height for downhill and cliff checks, deferring corridor lookups to cost calculation, and computing the line-of-sight heuristic only when updating a cell's route. It also covers the reviewer-suggested diagonal-step and candidate-cost changes, and the tunneling early return replaced by an assert. The description contains benchmark data and rationale. The HTML markup and some typos do not affect relevance.

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8ae6f264-d6de-44d4-bb56-bfc7e5f44e88
Commits

Reviewing files that changed from the base of the PR and between 1f91e15 and 225e7d5.

Files selected for processing: 1

  • Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

@greptile-apps

greptile-apps Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable defects were found.

Summary

Reduces repeated work in fine pathfinding:

  • Straight-line pathfinding skips goal scoring for routes it will not keep.
  • Neighbor searches delay terrain and corridor work until it is needed.

No actionable issues found.

Reviews (4) · Last reviewed commit: "perf(pathfinder): Assert the line-of-sig..." · Reviewed by Greptile

if ( locomotorSet.isDownhillOnly() )
{
if (!fromHeightValid) {
fromPos.z = TheTerrainLogic->getGroundHeight(fromPos.x, fromPos.y);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could perhaps change Coord3D fromPos to Real fromPosZ = 0.0f, and do this:
fromPosZ = TheTerrainLogic->getGroundHeight(parentCell->getXIndex() * PATHFIND_CELL_SIZE_F, parentCell->getYIndex() * PATHFIND_CELL_SIZE_F);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I don't think it's really necessary, the calculation is performed once anyway. And the code is a bit cleaner to understand as it is.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fair enough.

@Mauller
Mauller force-pushed the codex/pathfinding-fine-pass-optimisations branch from 9250a2b to 1f91e15 Compare November 21, 2026 19:18
@Mauller

Mauller commented Nov 21, 2026

Copy link
Copy Markdown
Author

Tested a mixture of local retail replays that i have and they all pass without mismatch so far.

The sole caller already excludes tunneling before invoking line-of-sight expansion. Replace the repeated callback abort check with a debug assertion so normal release builds omit the redundant test.
…diagonal steps earlier in examineNeighboringCells()
The sole caller already excludes tunneling before invoking line-of-sight expansion. Replace the repeated callback abort check with a debug assertion so normal release builds omit the redundant test.
@Mauller
Mauller force-pushed the codex/pathfinding-fine-pass-optimisations branch from 225e7d5 to f9beb07 Compare November 22, 2026 15:39
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