Skip to content

pathfinding: compute flow field once per move order, not every frame - #68

Merged
corepunch merged 1 commit into
mainfrom
copilot/optimize-pathfinding-performance
Jun 9, 2026
Merged

corepunch merged 1 commit into
mainfrom
copilot/optimize-pathfinding-performance

Conversation

Copilot AI commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

M_RefreshHeatmap was called every frame from unit_changeangle, unconditionally invoking CM_BuildHeatmap. The 4-slot LRU cache inside CM_BuildHeatmap helped for identical goals, but with >4 concurrent move groups or attack targets it thrashed constantly — causing bake_flow_field + build_heatmap to re-run on every frame and account for ~49% of total CPU time.

Changes

  • common/routing.c — Added CM_ActivateCachedFlow(DWORD generation): looks up a cached flow field by generation number and sets active_flow without triggering a rebuild. O(N) over 4 slots.

  • common/common.h — Declared CM_ActivateCachedFlow.

  • games/warcraft-3/game/g_monster.c — M_RefreshHeatmap: For fixed waypoints (non-SVF_MONSTER goals), skip CM_BuildHeatmap if route->heatmap2 is already set and the generation is still cached — the full rebuild only happens once per move order. Monster targets (chased enemies) continue to rebuild every frame since their position changes.

if (!(route->svflags & SVF_MONSTER) &&
    route->heatmap2 &&
    CM_ActivateCachedFlow(route->heatmap2)) {
    return route->heatmap2;
}
route->heatmap2 = CM_BuildHeatmap(route);

Waypoint_add already zeroes heatmap2 on allocation, so each new move order triggers exactly one rebuild and all subsequent frames hit the fast path.

@corepunch
corepunch marked this pull request as ready for review June 9, 2026 07:23
@corepunch
corepunch merged commit 43125ce into main Jun 9, 2026
2 checks passed
@corepunch
corepunch deleted the copilot/optimize-pathfinding-performance branch June 9, 2026 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants