forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 174
perf(pathfinder): Reduce cost of long pathfinding by 50-66% by implementing a conditional reverse insertion sorting algorithm #2450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mauller
wants to merge
2
commits into
TheSuperHackers:main
Choose a base branch
from
Mauller:Mauller/perf-reverse-insertionsort
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1235,6 +1235,16 @@ void PathfindCellInfo::releaseACellInfo(PathfindCellInfo *theInfo) | |
|
|
||
| //----------------------------------------------------------------------------------- | ||
|
|
||
| Bool PathfindCellList::canReverseSort(PathfindCell& currentCell) const | ||
| { | ||
| if (m_head && m_tail) | ||
| return m_head->getTotalCostDifference(currentCell) > m_tail->getTotalCostDifference(currentCell); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| //----------------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Constructor | ||
| */ | ||
|
|
@@ -1338,6 +1348,18 @@ inline void PathfindCell::setBlockedByAlly(Bool blocked) | |
| #endif | ||
| } | ||
|
|
||
| /** | ||
| * Determine absolute total path cost difference between two cells. | ||
| * Returns UINT_MAX if used with an uninitialised cell, so will be sorted as maximally dissimilar. | ||
| */ | ||
| inline UnsignedInt PathfindCell::getTotalCostDifference(PathfindCell& other) const | ||
| { | ||
| if (m_info && other.m_info) | ||
| return abs((Int)m_info->m_totalCost - (Int)other.m_info->m_totalCost); | ||
|
|
||
| return UINT_MAX; | ||
| } | ||
|
|
||
| /** | ||
| * Set the parent pointer. | ||
| */ | ||
|
|
@@ -1734,7 +1756,7 @@ void PathfindCell::forwardInsertionSortRetailCompatible(PathfindCellList& list) | |
| } | ||
| #endif | ||
|
|
||
| // Forward insertion sort, returns early if the list is being initialised or we are prepending the list | ||
| // Forward insertion sort, returns early if the list is being initialized or we are prepending the list | ||
| void PathfindCell::forwardInsertionSort(PathfindCellList& list) | ||
| { | ||
| DEBUG_ASSERTCRASH(m_info, ("Has to have info.")); | ||
|
|
@@ -1748,6 +1770,7 @@ void PathfindCell::forwardInsertionSort(PathfindCellList& list) | |
| m_info->m_prevOpen = nullptr; | ||
| m_info->m_nextOpen = nullptr; | ||
| list.m_head = this; | ||
| list.m_tail = this; | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -1771,10 +1794,60 @@ void PathfindCell::forwardInsertionSort(PathfindCellList& list) | |
| if (current->m_info->m_nextOpen != nullptr) { | ||
| current->m_info->m_nextOpen->m_prevOpen = this->m_info; | ||
| } | ||
| else { | ||
| list.m_tail = this; | ||
| } | ||
|
|
||
| current->m_info->m_nextOpen = this->m_info; | ||
Skyaero42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| m_info->m_prevOpen = current->m_info; | ||
| } | ||
|
|
||
| // Reverse insertion sort, returns early if the list is being initialized or we are appending the list | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could add |
||
| void PathfindCell::reverseInsertionSort(PathfindCellList& list) | ||
| { | ||
| DEBUG_ASSERTCRASH(m_info, ("Has to have info.")); | ||
| DEBUG_ASSERTCRASH(m_info->m_closed == FALSE && m_info->m_open == FALSE, ("Serious error - Invalid flags. jba")); | ||
|
|
||
| // mark the new cell as being on the open list | ||
| m_info->m_open = true; | ||
| m_info->m_closed = false; | ||
|
|
||
| if (list.m_tail == nullptr) { | ||
| m_info->m_prevOpen = nullptr; | ||
| m_info->m_nextOpen = nullptr; | ||
| list.m_tail = this; | ||
| list.m_head = this; | ||
| return; | ||
Skyaero42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // If the node needs inserting after the current list tail | ||
| if (m_info->m_totalCost >= list.m_tail->m_info->m_totalCost) { | ||
| m_info->m_prevOpen = list.m_tail->m_info; | ||
| list.m_tail->m_info->m_nextOpen = this->m_info; | ||
| m_info->m_nextOpen = nullptr; | ||
| list.m_tail = this; | ||
| return; | ||
| } | ||
|
|
||
| // Traverse the list to find correct position | ||
| PathfindCell* current = list.m_tail; | ||
| while (current->m_info->m_prevOpen && current->m_info->m_prevOpen->m_totalCost > m_info->m_totalCost) { | ||
| current = current->getPrevOpen(); | ||
| } | ||
|
|
||
| // Insert the new node in the correct position | ||
| m_info->m_prevOpen = current->m_info->m_prevOpen; | ||
| if (current->m_info->m_prevOpen != nullptr) { | ||
| current->m_info->m_prevOpen->m_nextOpen = this->m_info; | ||
| } | ||
| else { | ||
| list.m_head = this; | ||
| } | ||
|
|
||
| current->m_info->m_prevOpen = this->m_info; | ||
Skyaero42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| m_info->m_nextOpen = current->m_info; | ||
| } | ||
|
|
||
| /// put self on "open" list in ascending cost order, return new list | ||
| void PathfindCell::putOnSortedOpenList( PathfindCellList &list ) | ||
| { | ||
|
|
@@ -1785,7 +1858,15 @@ void PathfindCell::putOnSortedOpenList( PathfindCellList &list ) | |
| } | ||
| #endif | ||
|
|
||
| forwardInsertionSort(list); | ||
| // TheSuperHackers @performance Mauller 20/03/2026 Implement reverse insertion sorting. | ||
| // Long and complex paths often append PathfindCell's, with high total path costs, to the open list. | ||
| // Appending and reverse traversal allow faster insertion of these cells, reducing pathfinding overhead by 50 - 66%. | ||
| if (list.canReverseSort(*this)) { | ||
| reverseInsertionSort(list); | ||
| } | ||
| else { | ||
| forwardInsertionSort(list); | ||
| } | ||
| } | ||
|
|
||
| /// remove self from "open" list | ||
|
|
@@ -1795,6 +1876,9 @@ void PathfindCell::removeFromOpenList( PathfindCellList &list ) | |
| DEBUG_ASSERTCRASH(m_info->m_closed==FALSE && m_info->m_open==TRUE, ("Serious error - Invalid flags. jba")); | ||
| if (m_info->m_nextOpen) | ||
| m_info->m_nextOpen->m_prevOpen = m_info->m_prevOpen; | ||
| else { | ||
| list.m_tail = getPrevOpen(); | ||
| } | ||
|
|
||
| if (m_info->m_prevOpen) | ||
| m_info->m_prevOpen->m_nextOpen = m_info->m_nextOpen; | ||
|
|
@@ -1832,7 +1916,7 @@ Int PathfindCell::releaseOpenList( PathfindCellList &list ) | |
| if (curInfo->m_nextOpen) { | ||
| list.m_head = curInfo->m_nextOpen->m_cell; | ||
| } else { | ||
| list.m_head = nullptr; | ||
| list.reset(); | ||
| } | ||
| DEBUG_ASSERTCRASH(cur == curInfo->m_cell, ("Bad backpointer in PathfindCellInfo")); | ||
| curInfo->m_nextOpen = nullptr; | ||
|
|
@@ -1867,7 +1951,7 @@ Int PathfindCell::releaseClosedList( PathfindCellList &list ) | |
| if (curInfo->m_nextOpen) { | ||
| list.m_head = curInfo->m_nextOpen->m_cell; | ||
| } else { | ||
| list.m_head = nullptr; | ||
| list.reset(); | ||
| } | ||
| DEBUG_ASSERTCRASH(cur == curInfo->m_cell, ("Bad backpointer in PathfindCellInfo")); | ||
| curInfo->m_nextOpen = nullptr; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.