Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
perf(gamelogic): index the pathfinder open list by cost instead of walking it
Olcay Seygan261b85f0a1a3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 40ad5b036..04e675c2a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -827,6 +827,13 @@ about four hundred steps every time, eight times for every square examined. Theso it now comes in from whichever end is closer, and the two ordinary cases — cheaper thaneverything, dearer than everything — take no steps at all. Same order out, same routes, less work.+**Then the queue itself, properly.** Coming in from the nearer end still cost about eighty steps+for every square the search looked at — four hundred and thirty thousand steps in a single turn on+a busy map, more work than the searching. So the list is not read at all any more. The game keeps a+direct pointer to where each price belongs, so adding a square to the queue is one step instead of+eighty, however long the queue has grown. What comes out is in exactly the same order, which is the+point: same queue, same routes, same game on every machine — without the reading.+**And the ten-second hiccup.** Every ten seconds or so the game redraws its map of which areasconnect to which — it has to, because you and seven opponents keep putting buildings in the way.Joining two areas together meant rewriting the entire table, once per join, and on a big map thatdiff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.hindex 288464713..4a372c23b 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPathfind.h@@ -297,6 +297,12 @@ protected:/// @todo Do we need both mark values in this cell? Can't store a single value and compare it?UnsignedInt m_open:1; ///< place for marking this cell as on the open listUnsignedInt m_closed:1; ///< place for marking this cell as on the closed list+ /* The open list is indexed by cost (see the bucket queue in AIPathfind.cpp). This is the+ bucket the cell was filed under, which is not always m_totalCost any more: callers change+ the cost of a cell that is still linked into the list and only then take it off, so the+ cell has to remember where it actually went. Sixteen bits, the width of m_totalCost, and+ it rides in the spare room of this bitfield word so PathfindCellInfo does not grow. */+ UnsignedInt m_openBucket:16;};/**diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cppindex 522b9063e..2c803dd39 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPathfind.cpp@@ -122,13 +122,13 @@ enum { PF_QUEUE, PF_FIND, PF_CLOSEST, PF_ATTACK, PF_SAFE, PF_PATCH, PF_MOVEAWAY,PF_ZONES, PF_ZONEMOD,// count-only from here down: these sit in the innermost loops, where two// QueryPerformanceCounter calls each would cost more than the thing being measured- PF_EXPAND, PF_MC_EXPAND, PF_MC_LINE, PF_MC_LINEPASS, PF_OPENWALK, PF_ZONEMERGE,+ PF_EXPAND, PF_MC_EXPAND, PF_MC_LINE, PF_MC_LINEPASS, PF_NEWBUCKET, PF_ZONEMERGE,PF_SLOTS };static const char *thePFSlotName[ PF_SLOTS ] ={ "queue", "find", "closest", "attack", "safe", "patch", "moveaway","adjust", "goal", "pos", "footprint", "exist", "hier", "movecheck","zones", "zonemod",- "expand", "mc.expand", "mc.line", "mc.linepass", "openwalk", "zone.merge" };+ "expand", "mc.expand", "mc.line", "mc.linepass", "newbucket", "zone.merge" };static __int64 thePFTicks[ PF_SLOTS ];static Int thePFCalls[ PF_SLOTS ];static Int thePFDepth = 0;@@ -1250,6 +1250,7 @@ PathfindCellInfo *PathfindCellInfo::getACellInfo(PathfindCell *cell,const ICoordinfo->m_totalCost = 0;info->m_open = 0;info->m_closed = 0;+ info->m_openBucket = 0;info->m_goalUnitID = INVALID_ID;info->m_posUnitID = INVALID_ID;info->m_goalAircraftID = INVALID_ID;@@ -1604,24 +1605,151 @@ Bool PathfindCell::removeObstacle( Object *obstacle )/* EA walked this list from the head on every insert and stopped only past the last cell of equalcost, so a search with a wide frontier paid O(open list) per cell it expanded. Measured with the`openwalk` counter on a 1-human / 7-AI skirmish: 62,490,219 walk steps over 108 slow frames, with- 8,871,712 of them in a single frame - about 400 steps per insert. The list is doubly linked, so- keep a tail pointer and walk in from whichever end is nearer the new cell's cost, taking the two- common cases (cheaper than everything, dearer than everything) in constant time. The insertion- point is unchanged - still after every cell of equal cost - so the search expands exactly the- cells it did before and lockstep holds. */+ 8,871,712 of them in a single frame - about 400 steps per insert. Walking in from whichever end+ of the doubly linked list was nearer took that to ~80 steps per expanded cell, which was still+ the largest single cost in the search: 434,496 walk steps against 5,463 expansions on one slow+ frame.++ So index the list instead of walking it. A* keys on m_totalCost and that is an UnsignedShort,+ so the whole key space fits in an array - this is Dial's bucket queue laid over the list EA+ already had. theOpenBucketTail[c] is the last cell of cost c currently on the list, so an+ insert is "link in after theOpenBucketTail[c]" and touches nothing else. The only search left+ is finding the nearest occupied bucket below c the first time a cost appears, and that is a+ three level bit index - 65536 bits, 2048 bits of summary over those, 64 bits over those - not a+ scan.++ The list itself is untouched: same cells, same order, still inserted after every cell of equal+ cost. The search therefore expands exactly the cells it expanded before, which is what lockstep+ depends on - a different expansion order is a different path on one machine and a mismatch.++ The `openwalk` counter in the pathfinder profile line is gone with the walk it counted; the slot+ is now `newbucket` and counts inserts that were the first at their cost, which is the only work+ the index still does per insert. */static PathfindCellInfo *theOpenListTail = NULL;+enum { OPEN_BUCKET_COUNT = 65536 }; // m_totalCost is an UnsignedShort++static PathfindCellInfo *theOpenBucketTail[OPEN_BUCKET_COUNT]; // last cell of each cost, or NULL+static UnsignedInt theOpenBucketBits0[OPEN_BUCKET_COUNT/32]; // bit c: bucket c is not empty+static UnsignedInt theOpenBucketBits1[OPEN_BUCKET_COUNT/1024]; // bit w: bits0[w] is not zero+static UnsignedInt theOpenBucketBits2[OPEN_BUCKET_COUNT/32768]; // bit w: bits1[w] is not zero++/// index of the most significant set bit; the word must not be zero+static Int openBucketHighestBit( UnsignedInt word )+{+ Int bit = 0;+ if (word & 0xFFFF0000) { bit += 16; word >>= 16; }+ if (word & 0x0000FF00) { bit += 8; word >>= 8; }+ if (word & 0x000000F0) { bit += 4; word >>= 4; }+ if (word & 0x0000000C) { bit += 2; word >>= 2; }+ if (word & 0x00000002) { bit += 1; }+ return bit;+}++static void openBucketMark( UnsignedInt cost )+{+ theOpenBucketBits0[cost>>5] |= 1U << (cost & 31);+ theOpenBucketBits1[cost>>10] |= 1U << ((cost>>5) & 31);+ theOpenBucketBits2[cost>>15] |= 1U << ((cost>>10) & 31);+}++static void openBucketClear( UnsignedInt cost )+{+ UnsignedInt w0 = cost >> 5;+ theOpenBucketBits0[w0] &= ~(1U << (cost & 31));+ if (theOpenBucketBits0[w0] == 0) {+ UnsignedInt w1 = w0 >> 5;+ theOpenBucketBits1[w1] &= ~(1U << (w0 & 31));+ if (theOpenBucketBits1[w1] == 0) {+ theOpenBucketBits2[w1>>5] &= ~(1U << (w1 & 31));+ }+ }+}++/// the highest occupied bucket strictly below cost, or -1 when there is none+static Int openBucketBelow( UnsignedInt cost )+{+ UnsignedInt w0 = cost >> 5;+ UnsignedInt mask = (cost & 31) ? ((1U << (cost & 31)) - 1) : 0;+ UnsignedInt bits = theOpenBucketBits0[w0] & mask;+ if (bits)+ return (Int)((w0 << 5) | openBucketHighestBit(bits));++ // nothing lower in our own word - find the last non-empty word below it, through the summaries+ UnsignedInt w1 = w0 >> 5;+ mask = (w0 & 31) ? ((1U << (w0 & 31)) - 1) : 0;+ bits = theOpenBucketBits1[w1] & mask;+ if (bits == 0) {+ UnsignedInt w2 = w1 >> 5;+ mask = (w1 & 31) ? ((1U << (w1 & 31)) - 1) : 0;+ UnsignedInt top = theOpenBucketBits2[w2] & mask;+ while (top == 0) {+ if (w2 == 0)+ return -1; // nothing anywhere below us+ --w2;+ top = theOpenBucketBits2[w2];+ }+ w1 = (w2 << 5) | openBucketHighestBit(top);+ bits = theOpenBucketBits1[w1];+ }+ w0 = (w1 << 5) | openBucketHighestBit(bits);+ return (Int)((w0 << 5) | openBucketHighestBit(theOpenBucketBits0[w0]));+}++/// empty the index; walks the occupied buckets, not the 65536 the array can hold+static void openBucketReset( void )+{+ UnsignedInt w2;+ for (w2 = 0; w2 < OPEN_BUCKET_COUNT/32768; w2++) {+ UnsignedInt top = theOpenBucketBits2[w2];+ while (top) {+ Int b2 = openBucketHighestBit(top);+ top &= ~(1U << b2);+ UnsignedInt w1 = (w2 << 5) | b2;+ UnsignedInt mid = theOpenBucketBits1[w1];+ while (mid) {+ Int b1 = openBucketHighestBit(mid);+ mid &= ~(1U << b1);+ UnsignedInt w0 = (w1 << 5) | b1;+ UnsignedInt low = theOpenBucketBits0[w0];+ while (low) {+ Int b0 = openBucketHighestBit(low);+ low &= ~(1U << b0);+ theOpenBucketTail[(w0 << 5) | b0] = NULL;+ }+ theOpenBucketBits0[w0] = 0;+ }+ theOpenBucketBits1[w1] = 0;+ }+ theOpenBucketBits2[w2] = 0;+ }+}+void PathfindCell::setOpenListSeed( PathfindCell *cell ){+ // a search starts by assigning its start cell to m_openList by hand rather than inserting it,+ // so the index has to be told about that cell here - otherwise the first real insert files+ // itself against an index that does not know the list has anything in it+ openBucketReset();theOpenListTail = cell ? cell->m_info : NULL;+ if (theOpenListTail) {+ theOpenListTail->m_openBucket = theOpenListTail->m_totalCost;+ theOpenBucketTail[theOpenListTail->m_openBucket] = theOpenListTail;+ openBucketMark( theOpenListTail->m_openBucket );+ }}PathfindCell *PathfindCell::putOnSortedOpenList( PathfindCell *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"));+ const UnsignedInt cost = m_info->m_totalCost;+ m_info->m_openBucket = cost;+if (list == NULL){+ // an empty list means an empty index - nothing can still be holding a bucket+ openBucketReset();list = this;m_info->m_prevOpen = NULL;m_info->m_nextOpen = NULL;@@ -1630,40 +1758,13 @@ PathfindCell *PathfindCell::putOnSortedOpenList( PathfindCell *list )else{DEBUG_ASSERTCRASH(theOpenListTail && theOpenListTail->m_nextOpen==NULL, ("Open list tail is stale."));- const UnsignedInt cost = m_info->m_totalCost;- PathfindCellInfo *tail = theOpenListTail;- PathfindCellInfo *before; // insert after this one, NULL means at the head-- if (cost >= tail->m_totalCost)- {- before = tail; // dearer than everything - append- }- else if (cost < list->m_info->m_totalCost)- {- before = NULL; // cheaper than everything - prepend- }- else if (cost - list->m_info->m_totalCost <= tail->m_totalCost - cost)+ PathfindCellInfo *before = theOpenBucketTail[cost]; // after the last cell of our own cost+ if (before == NULL){- // nearer the cheap end - walk forward to the first cell that costs more than us- PathfindCellInfo *c;- before = NULL;- for( c = list->m_info; c; c = c->m_nextOpen )- {- pfBump( PF_OPENWALK );- if (c->m_totalCost > cost)- break;- before = c;- }- }- else- {- // nearer the dear end - walk back to the last cell that does not cost more than us- before = tail;- while (before && before->m_totalCost > cost)- {- pfBump( PF_OPENWALK );- before = before->m_prevOpen;- }+ // first cell at this cost - go in after the dearest cell that is still cheaper than us+ pfBump( PF_NEWBUCKET );+ Int below = openBucketBelow( cost );+ before = (below < 0) ? NULL : theOpenBucketTail[below];}if (before)@@ -1685,6 +1786,10 @@ PathfindCell *PathfindCell::putOnSortedOpenList( PathfindCell *list )}}+ // we went in after every other cell of our cost, so we are that bucket's tail now+ theOpenBucketTail[cost] = m_info;+ openBucketMark( cost );+// mark newCell as being on open listm_info->m_open = true;m_info->m_closed = false;@@ -1698,6 +1803,20 @@ PathfindCell *PathfindCell::removeFromOpenList( PathfindCell *list ){DEBUG_ASSERTCRASH(m_info, ("Has to have info."));DEBUG_ASSERTCRASH(m_info->m_closed==FALSE && m_info->m_open==TRUE, ("Serious error - Invalid flags. jba"));+ /* By the bucket we were filed under, not by m_totalCost: callers raise a cell's cost while it+ is still linked and only then take it off, and filing the removal by the new cost would+ leave the old bucket pointing at a cell that is no longer on the list. */+ const UnsignedInt bucket = m_info->m_openBucket;+ if (theOpenBucketTail[bucket] == m_info)+ {+ if (m_info->m_prevOpen && m_info->m_prevOpen->m_openBucket == bucket) {+ theOpenBucketTail[bucket] = m_info->m_prevOpen; // the cell in front of us holds it now+ } else {+ theOpenBucketTail[bucket] = NULL; // we were the only one at this cost+ openBucketClear( bucket );+ }+ }+if (theOpenListTail == m_info)theOpenListTail = m_info->m_prevOpen;if (m_info->m_nextOpen)@@ -1718,6 +1837,8 @@ PathfindCell *PathfindCell::removeFromOpenList( PathfindCell *list )/// remove all cells from "open" listInt PathfindCell::releaseOpenList( PathfindCell *list ){+ // the cells are about to hand their infos back, so nothing may still be filed against them+ openBucketReset();theOpenListTail = NULL;Int count = 0;while (list) {diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 923f38eb5..e62445a47 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -1692,6 +1692,152 @@ TEST(pathfind_open_list_insert_keeps_ascending_cost_and_insertion_order_on_ties)}+/* The open list is not walked any more, it is indexed by cost: a bucket per possible m_totalCost,+ with a three level bit index over the occupied ones. That index is the part that can be wrong+ in ways the previous test would not notice, because its costs all live inside one 32 bit word of+ the bottom level. This one spreads them across the whole 16 bit key space, so finding the+ nearest occupied bucket below a new cost has to climb the summaries and come back down, and it+ empties buckets from both ends so the clear paths run too.++ It also reproduces the one thing the game does that the index cannot see coming: raising a+ cell's cost while it is still linked into the list, and only then taking it off. That is+ findAttackPath's decrease-key, and it is why a cell remembers which bucket it went into. */+TEST(pathfind_open_list_bucket_index_finds_the_right_neighbour_across_the_whole_cost_range)+{+ CHECK(bootOnce());+ PathfindCellInfo::allocateCellInfos();++ const Int count = 300;+ const Int spares = 8; // held back, to be inserted at costs that were vacated+ PathfindCell *cells = MSGNEW("PathfindCellInfo") PathfindCell[ count + spares ];+ theOpenTestCells = cells;++ std::vector<Int> expected;+ PathfindCell *list = NULL;+ Int seed = 987654321;+ Int i;++ /* costs spread over the whole UnsignedShort range, including both ends, with enough repeats to+ keep the tie order under test as well */+ for( i = 0; i < count; i++ )+ {+ seed = seed * 1103515245 + 12345;+ UnsignedInt cost;+ switch( i % 10 )+ {+ case 0: cost = 0; break; // the very bottom bucket+ case 1: cost = 65535; break; // the very top one+ case 2: cost = 31; break; // last bit of word 0+ case 3: cost = 32; break; // first bit of word 1+ case 4: cost = 1023; break; // last bucket under summary word 0+ case 5: cost = 1024; break; // first bucket over it+ case 6: cost = 32767; break; // last bucket under the top word 0+ case 7: cost = 32768; break; // first bucket in top word 1+ default: cost = (UnsignedInt)((seed >> 8) & 0xFFFF); break;+ }+ ICoord2D pos;+ pos.x = (UnsignedShort)(i % 64);+ pos.y = (UnsignedShort)(i / 64);+ CHECK(cells[ i ].allocateInfo( pos ));+ cells[ i ].setTotalCost( cost );++ std::vector<Int>::iterator it = expected.begin();+ while( it != expected.end() && cells[ *it ].getTotalCost() <= cost )+ ++it;+ expected.insert( it, i );++ list = cells[ i ].putOnSortedOpenList( list );+ }+ CHECK(openListOrderMatches( list, expected ));++ /* the game's decrease-key, in the game's order: change the cost first, take the cell off+ second. If the removal is filed under the new cost instead of the bucket the cell is+ actually in, the old bucket is left pointing at a cell that is no longer on the list and+ the next insert at that cost links itself into nothing. */+ Int rekeyed[ 6 ];+ rekeyed[ 0 ] = expected.front();+ rekeyed[ 1 ] = expected.back();+ rekeyed[ 2 ] = expected[ expected.size() / 3 ];+ rekeyed[ 3 ] = expected[ expected.size() / 2 ];+ rekeyed[ 4 ] = expected[ 1 ];+ rekeyed[ 5 ] = expected[ expected.size() - 2 ];+ for( i = 0; i < 6; i++ )+ {+ Int c = rekeyed[ i ];+ UnsignedInt was = cells[ c ].getTotalCost();+ UnsignedInt cost = (UnsignedInt)(i * 9973) & 0xFFFF;+ cells[ c ].setTotalCost( cost ); // cost changes while still linked+ list = cells[ c ].removeFromOpenList( list );+ expected.erase( std::find( expected.begin(), expected.end(), c ) );++ std::vector<Int>::iterator it = expected.begin();+ while( it != expected.end() && cells[ *it ].getTotalCost() <= cost )+ ++it;+ expected.insert( it, c );+ list = cells[ c ].putOnSortedOpenList( list );+ CHECK(openListOrderMatches( list, expected ));++ /* and now a fresh cell at the cost the rekeyed one used to have. This is what makes the+ stale bucket visible: if the removal was filed under the new cost, the old cost still+ points at the rekeyed cell, which has since moved somewhere else entirely, and this+ insert lands next to it instead of where its own cost belongs. */+ Int spare = count + i;+ ICoord2D spos;+ spos.x = (UnsignedShort)(spare % 64);+ spos.y = (UnsignedShort)(spare / 64);+ CHECK(cells[ spare ].allocateInfo( spos ));+ cells[ spare ].setTotalCost( was );+ it = expected.begin();+ while( it != expected.end() && cells[ *it ].getTotalCost() <= was )+ ++it;+ expected.insert( it, spare );+ list = cells[ spare ].putOnSortedOpenList( list );+ CHECK(openListOrderMatches( list, expected ));+ }++ /* empty it from the cheap end - an A* pop loop - and check after every pop, because a bucket+ left marked occupied after its last cell leaves is exactly the failure that would send the+ next insert to a freed cell */+ while( expected.size() > 100 )+ {+ Int c = expected.front();+ expected.erase( expected.begin() );+ list = cells[ c ].removeFromOpenList( list );+ CHECK(openListOrderMatches( list, expected ));+ }++ /* and the rest from the dear end, which drains the top summary word */+ while( !expected.empty() )+ {+ Int c = expected.back();+ expected.pop_back();+ list = cells[ c ].removeFromOpenList( list );+ CHECK(openListOrderMatches( list, expected ));+ }+ CHECK(list == NULL);++ /* an emptied index must be empty: two cells at costs that were both heavily used above, in+ the wrong order, still come out sorted */+ cells[ 0 ].setTotalCost( 65535 );+ list = cells[ 0 ].putOnSortedOpenList( NULL );+ cells[ 1 ].setTotalCost( 0 );+ list = cells[ 1 ].putOnSortedOpenList( list );+ cells[ 2 ].setTotalCost( 32768 );+ list = cells[ 2 ].putOnSortedOpenList( list );+ CHECK(list == &cells[ 1 ]);+ CHECK(list->getNextOpen() == &cells[ 2 ]);+ CHECK(list->getNextOpen()->getNextOpen() == &cells[ 0 ]);+ CHECK(list->getNextOpen()->getNextOpen()->getNextOpen() == NULL);++ PathfindCell::releaseOpenList( list );+ for( i = 0; i < count + spares; i++ )+ cells[ i ].releaseInfo();+ delete [] cells;+ theOpenTestCells = NULL;+ PathfindCellInfo::releaseCellInfos();+}++//-------------------------------------------------------------------------------------------------// Zone equivalency sets (pathfindZoneFind / pathfindZoneUnion / pathfindZoneFlatten).//