Skip to content

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.

perf(gamenetwork): sort the command queue by the key it claims to use

Olcay Seyganae9e00271618
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fdd5debff..ad959980e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -321,6 +321,18 @@ map. Ctrl+A is still everything at once.
worst moment in a match — right after someone has already dropped. The list is now read properly,
and when it genuinely runs out the game says so instead of inventing somebody.
+- **The busier the game got, the more time it wasted filing the replies.** Every order anyone gives
+ is confirmed back to them, and in a full eight-player match those confirmations are far and away
+ the most numerous thing on the wire. They are kept in a sorted queue, and the queue had a
+ shortcut for the normal case where the next one belongs on the end — but the shortcut compared
+ the wrong number, one that never changes from one confirmation to the next, so the shortcut could
+ never tell them apart and never once fired for them. The
+ confirmations were also the kind that sorts to the *front* of the queue, so each new one was
+ filed by reading past every confirmation already waiting. Twice the traffic meant four times the
+ filing work, right at the moment the game can least afford it. The shortcut now compares the
+ number the queue is actually sorted by, and the work grows with the traffic instead of with its
+ square.
+
- **A dropped order used to make the game wait longer each time it went missing — up to two full
seconds.** When a packet goes astray it is sent again, and the wait before each attempt was
doubling: the third and fourth time the same order went missing, everybody sat there for a second
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetCommandList.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetCommandList.h
index 641acdf77..c4a1d98b2 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetCommandList.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetCommandList.h
@@ -33,7 +33,7 @@
/**
* The NetCommandList is a ordered linked list of NetCommandRef objects.
- * The list is ordered based on the command id, player id, and command type.
+ * The list is ordered by command type, then player id, then sort number.
* It is ordered in this way to aid in constructing the packets efficiently.
* The list keeps track of the last message inserted in order to accommodate
* adding commands in order more efficiently since that is whats going to be
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h
index b4bdce55b..aceeefbe9 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/networkutil.h
@@ -29,6 +29,7 @@
#include "GameNetwork/NetworkDefs.h"
#include "GameNetwork/NetworkInterface.h"
+#include "GameNetwork/NetCommandMsg.h"
UnsignedInt ResolveIP(AsciiString host);
UnsignedShort GenerateNextCommandID();
@@ -48,6 +49,38 @@ inline Bool IsCommandIdNewer( UnsignedShort newVal, UnsignedShort oldVal )
return diff != 0 && diff < 0x8000;
}
+//
+// The command list is sorted by command type, then player id, then sort number. The sort number
+// is the command id for everything except the three ack types, which sort by the id of the command
+// they acknowledge - an ack's own id is not a key at all: it stays at the constructor's zero when
+// the ack is made to be sent, and NetPacket::getCommandList sets it to the packet's running command
+// counter when one is read off the wire, which acks do not advance. Either way it is the same
+// number for every ack in a run. Every comparison in the list has to use the same key, so all of
+// them ask these.
+//
+inline Bool IsCommandFromSamePlayerGroup( NetCommandMsg *first, NetCommandMsg *second )
+{
+ return first->getNetCommandType() == second->getNetCommandType() &&
+ first->getPlayerID() == second->getPlayerID();
+}
+
+inline Bool IsCommandNewerInSamePlayerGroup( NetCommandMsg *newCommand, NetCommandMsg *oldCommand )
+{
+ return IsCommandFromSamePlayerGroup( newCommand, oldCommand ) &&
+ IsCommandIdNewer( (UnsignedShort)newCommand->getSortNumber(), (UnsignedShort)oldCommand->getSortNumber() );
+}
+
+inline Bool IsCommandNewer( NetCommandMsg *newCommand, NetCommandMsg *oldCommand )
+{
+ if( newCommand->getNetCommandType() != oldCommand->getNetCommandType() )
+ return newCommand->getNetCommandType() > oldCommand->getNetCommandType();
+
+ if( newCommand->getPlayerID() != oldCommand->getPlayerID() )
+ return newCommand->getPlayerID() > oldCommand->getPlayerID();
+
+ return IsCommandIdNewer( (UnsignedShort)newCommand->getSortNumber(), (UnsignedShort)oldCommand->getSortNumber() );
+}
+
#ifdef DEBUG_LOGGING
extern "C" {
void dumpBufferToLog(const void *vBuf, Int len, const char *fname, Int line);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandList.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandList.cpp
index 3eb407d0a..c545f8a4f 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandList.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetCommandList.cpp
@@ -124,7 +124,7 @@ void NetCommandList::reset() {
/**
* Insert sorts msg. Assumes that all the previous message inserts were done using this function.
- * The message is sorted in based first on command type, then player id, and then command id.
+ * The message is sorted in based first on command type, then player id, and then sort number.
*/
NetCommandRef * NetCommandList::addMessage(NetCommandMsg *cmdMsg) {
if (cmdMsg == NULL) {
@@ -149,12 +149,18 @@ NetCommandRef * NetCommandList::addMessage(NetCommandMsg *cmdMsg) {
// So saving the placement of the last message inserted can give us a huge boost in
// efficiency.
NetCommandRef *theNext = m_lastMessageInserted->getNext();
- if ((m_lastMessageInserted->getCommand()->getNetCommandType() == msg->getCommand()->getNetCommandType()) &&
- (m_lastMessageInserted->getCommand()->getPlayerID() == msg->getCommand()->getPlayerID()) &&
- IsCommandIdNewer(msg->getCommand()->getID(), m_lastMessageInserted->getCommand()->getID()) &&
- ((theNext == NULL) || ((theNext->getCommand()->getNetCommandType() > msg->getCommand()->getNetCommandType()) ||
- (theNext->getCommand()->getPlayerID() > msg->getCommand()->getPlayerID()) ||
- IsCommandIdNewer(theNext->getCommand()->getID(), msg->getCommand()->getID())))) {
+
+ // This asked whether the new command's *id* was newer, while the scan below orders the list
+ // by sort number. The two agree for every command except an ack, whose id is the same number
+ // for every ack in a run - so IsCommandIdNewer was false for every pair and the shortcut never
+ // fired for the one kind of message that arrives in bulk. Acks sort to the head of the list,
+ // so each one then walked every ack already queued: N acks cost N*N/2 steps instead of N.
+ Bool canInsertAfterLast = IsCommandNewerInSamePlayerGroup(msg->getCommand(), m_lastMessageInserted->getCommand());
+ if (canInsertAfterLast && (theNext != NULL)) {
+ canInsertAfterLast = IsCommandNewer(theNext->getCommand(), msg->getCommand());
+ }
+
+ if (canInsertAfterLast) {
// Make sure this command isn't already in the list.
if (isEqualCommandMsg(m_lastMessageInserted->getCommand(), msg->getCommand())) {
@@ -273,9 +279,8 @@ NetCommandRef * NetCommandList::addMessage(NetCommandMsg *cmdMsg) {
return msg;
}
- // Find the position within the player's section based on the command ID.
- // If the command type doesn't require a command ID, sort by whatever it should be sorted by.
- while ((tempmsg != NULL) && (msg->getCommand()->getNetCommandType() == tempmsg->getCommand()->getNetCommandType()) && (msg->getCommand()->getPlayerID() == tempmsg->getCommand()->getPlayerID()) && IsCommandIdNewer((UnsignedShort)msg->getCommand()->getSortNumber(), (UnsignedShort)tempmsg->getCommand()->getSortNumber())) {
+ // Find the position within the player's section based on the sort number.
+ while ((tempmsg != NULL) && IsCommandNewerInSamePlayerGroup(msg->getCommand(), tempmsg->getCommand())) {
tempmsg = tempmsg->getNext();
}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index c117bdee2..b00686d8b 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -40,6 +40,7 @@
#include "GameLogic/LogicRandomValue.h"
#include "GameNetwork/CrcAgreement.h"
#include "GameNetwork/NetworkUtil.h"
+#include "GameNetwork/NetCommandList.h"
#include "GameNetwork/GameInfo.h"
#include <float.h>
#include "GameClient/Water.h"
@@ -3572,3 +3573,105 @@ TEST(hotkey_squad_index_covers_group_zero_and_stops_at_the_last_squad)
allInRange = FALSE;
CHECK( allInRange );
}
+
+//////////////////////////////////////////////////////////////////////////////
+// Network command list ordering
+//////////////////////////////////////////////////////////////////////////////
+
+/* Helper: an ack for command "commandID" from player "playerID". An ack's own id is always zero;
+ the number it sorts by is the id of the command it acknowledges. */
+static NetAckStage1CommandMsg *makeAck( UnsignedShort commandID, UnsignedByte playerID )
+{
+ NetAckStage1CommandMsg *ack = newInstance( NetAckStage1CommandMsg );
+ ack->setCommandID( commandID );
+ ack->setOriginalPlayerID( playerID );
+ ack->setPlayerID( playerID );
+ ack->setID( 0 );
+ return ack;
+}
+
+/* The command list sorts by type, then player, then sort number - and for an ack the sort number
+ is the acknowledged command's id, not the ack's own id, which is always zero. The insertion
+ shortcut compared ids instead, so IsCommandIdNewer(0, 0) was false for every ack and the
+ shortcut was dead for the one message that arrives in bulk. */
+TEST(netcommand_ordering_uses_the_sort_number_not_the_always_zero_ack_id)
+{
+ NetAckStage1CommandMsg *older = makeAck( 3, 2 );
+ NetAckStage1CommandMsg *newer = makeAck( 7, 2 );
+
+ // what the old comparison looked at: identical, and so never "newer"
+ CHECK_EQ( (Int)older->getID(), 0 );
+ CHECK_EQ( (Int)newer->getID(), 0 );
+ CHECK( !IsCommandIdNewer( newer->getID(), older->getID() ) );
+
+ // what the list actually orders by
+ CHECK_EQ( older->getSortNumber(), 3 );
+ CHECK_EQ( newer->getSortNumber(), 7 );
+
+ CHECK( IsCommandFromSamePlayerGroup( older, newer ) );
+ CHECK( IsCommandNewerInSamePlayerGroup( newer, older ) );
+ CHECK( !IsCommandNewerInSamePlayerGroup( older, newer ) );
+
+ // a different player is a different group, however the numbers compare
+ NetAckStage1CommandMsg *otherPlayer = makeAck( 7, 5 );
+ CHECK( !IsCommandFromSamePlayerGroup( newer, otherPlayer ) );
+ CHECK( !IsCommandNewerInSamePlayerGroup( otherPlayer, older ) );
+
+ // the full ordering is lexicographic: type, then player, then sort number
+ CHECK( IsCommandNewer( otherPlayer, newer ) ); // same type, higher player
+ CHECK( !IsCommandNewer( newer, otherPlayer ) );
+ CHECK( IsCommandNewer( newer, older ) ); // same type and player, higher sort number
+
+ NetAckStage2CommandMsg *laterType = newInstance( NetAckStage2CommandMsg );
+ laterType->setCommandID( 0 );
+ laterType->setPlayerID( 2 );
+ CHECK( IsCommandNewer( laterType, newer ) ); // higher type beats a higher sort number
+ CHECK( !IsCommandNewer( newer, laterType ) );
+
+ older->detach();
+ newer->detach();
+ otherPlayer->detach();
+ laterType->detach();
+}
+
+/* Acks sort to the head of the list, so the shortcut is what keeps a burst of them linear. Order
+ has to come out the same whether it is taken or not: in order, out of order, and duplicated. */
+TEST(netcommand_list_orders_a_burst_of_acks_by_acknowledged_command)
+{
+ NetCommandList *list = newInstance( NetCommandList );
+ list->init();
+
+ // in ascending order - the path the shortcut is there for
+ static const UnsignedShort inOrder[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
+ Int i;
+ for( i = 0; i < 8; ++i )
+ {
+ NetAckStage1CommandMsg *ack = makeAck( inOrder[i], 1 );
+ list->addMessage( ack );
+ ack->detach();
+ }
+
+ // out of order, and one exact duplicate of a command already on the list
+ static const UnsignedShort outOfOrder[] = { 12, 9, 11, 10, 5 };
+ for( i = 0; i < 5; ++i )
+ {
+ NetAckStage1CommandMsg *ack = makeAck( outOfOrder[i], 1 );
+ list->addMessage( ack );
+ ack->detach();
+ }
+
+ CHECK_EQ( list->length(), 12 ); // the duplicate 5 was dropped, 1..12 remain
+
+ Int expected = 1;
+ Bool ascending = TRUE;
+ for( NetCommandRef *ref = list->getFirstMessage(); ref != NULL; ref = ref->getNext() )
+ {
+ if( ref->getCommand()->getSortNumber() != expected )
+ ascending = FALSE;
+ ++expected;
+ }
+ CHECK( ascending );
+ CHECK_EQ( expected, 13 );
+
+ list->deleteInstance();
+}