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(gameengine): index the map's scripts by name

Olcay Seygandb5477beee8c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f5b3df8de..3f201f23b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -881,6 +881,12 @@ turn, and the one after. A squad that used to leave on turn 4,120 leaves on turn
is a thirtieth of a second - you cannot see it, and it was sitting still waiting for orders either
way.
+**And the map's own scripts, looked up by name.** A map is a few hundred little scripts, and one
+script calling another means finding it by name — which meant reading down every player's list of
+scripts, and every folder inside those lists, until the name turned up. The computer opponents' own
+behaviour is written in exactly those scripts and they call each other constantly. The names are
+fixed the moment a map is loaded, so they are now looked up directly instead of hunted for.
+
**And every burning building asking where its smoke is.** A damaged structure, a laser beam, a
beacon, a shockwave — each of them holds on to an effect by number and asks the game to hand it back,
every frame, for as long as it lives. Asking meant reading down the list of every effect alive in the
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/ScriptEngine.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/ScriptEngine.h
index 49074b9d3..df394cc37 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/ScriptEngine.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/ScriptEngine.h
@@ -400,6 +400,7 @@ protected:
void executeScript( Script *pScript );
Script *findScript(const AsciiString& name);
ScriptGroup *findGroup(const AsciiString& name);
+ void buildScriptNameCache( void ); ///< index every script and group in TheSidesList by name
void setSway( ScriptAction *pAction );
void setCounter( ScriptAction *pAction );
void addCounter( ScriptAction *pAction );
@@ -455,6 +456,16 @@ protected:
Int m_numCounters;
TFlag m_flags[MAX_FLAGS];
Int m_numFlags;
+ // Looking a script up by name used to walk every side's script list, and its groups' lists, on
+ // every call - and a script that calls subroutines does that several times per evaluation. The
+ // lists are fixed for the life of a map, so they are indexed once and the index is thrown away
+ // whenever the map is.
+ typedef std::hash_map< AsciiString, Script *, rts::hash<AsciiString>, rts::equal_to<AsciiString> > ScriptNameMap;
+ typedef std::hash_map< AsciiString, ScriptGroup *, rts::hash<AsciiString>, rts::equal_to<AsciiString> > ScriptGroupNameMap;
+ ScriptNameMap m_scriptsByName;
+ ScriptGroupNameMap m_groupsByName;
+ Bool m_scriptNameCacheValid;
+
AttackPriorityInfo m_attackPriorityInfo[MAX_ATTACK_PRIORITIES];
Int m_numAttackInfo;
Int m_endGameTimer;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/ScriptEngine/ScriptEngine.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/ScriptEngine/ScriptEngine.cpp
index 16bb56dd2..50dbe7a45 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/ScriptEngine/ScriptEngine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/ScriptEngine/ScriptEngine.cpp
@@ -445,6 +445,7 @@ void ScriptEngine::addConditionTemplateInfo( Template *actionTemplate)
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
ScriptEngine::ScriptEngine():
+m_scriptNameCacheValid(FALSE),
m_numCounters(0),
m_numFlags(0),
m_callingTeam(NULL),
@@ -5285,6 +5286,10 @@ void ScriptEngine::reset( void )
if (TheScriptConditions) {
TheScriptConditions->reset();
}
+ m_scriptNameCacheValid = FALSE;
+ m_scriptsByName.clear();
+ m_groupsByName.clear();
+
m_numCounters = 1;
m_numAttackInfo = 1;
m_numFlags = 1;
@@ -5432,6 +5437,11 @@ void ScriptEngine::reset( void )
//-------------------------------------------------------------------------------------------------
void ScriptEngine::newMap( void )
{
+ // a new map means a new set of scripts, so the name index is stale
+ m_scriptNameCacheValid = FALSE;
+ m_scriptsByName.clear();
+ m_groupsByName.clear();
+
m_numCounters = 1;
Int i;
for (i=0; i<MAX_COUNTERS; i++) {
@@ -6404,45 +6414,51 @@ Int ScriptEngine::allocateFlag( const AsciiString& name)
//-------------------------------------------------------------------------------------------------
ScriptGroup *ScriptEngine::findGroup(const AsciiString& name)
{
- Int i;
- for (i=0; i<TheSidesList->getNumSides(); i++) {
- ScriptList *pSL = TheSidesList->getSideInfo(i)->getScriptList();
- if (pSL==NULL) continue;
- ScriptGroup *pGroup;
- for (pGroup = pSL->getScriptGroup(); pGroup; pGroup=pGroup->getNext()) {
- if (pGroup->getName() == name) {
- return pGroup;
- }
- }
+ if (!m_scriptNameCacheValid) {
+ buildScriptNameCache();
}
- return 0; // Shouldn't ever happen.
+ ScriptGroupNameMap::const_iterator it = m_groupsByName.find(name);
+ return (it != m_groupsByName.end()) ? it->second : 0; // Shouldn't ever happen.
}
//-------------------------------------------------------------------------------------------------
-/** Locates a script by name. */
+/** Indexes every script and script group in TheSidesList by name. Insert-if-absent, walked in
+ * exactly the order the old linear searches used, so a duplicated name still resolves to the same
+ * script it always did. */
//-------------------------------------------------------------------------------------------------
-Script *ScriptEngine::findScript(const AsciiString& name)
+void ScriptEngine::buildScriptNameCache( void )
{
- Int i;
- for (i=0; i<TheSidesList->getNumSides(); i++) {
+ m_scriptsByName.clear();
+ m_groupsByName.clear();
+ m_scriptNameCacheValid = TRUE;
+
+ for (Int i=0; i<TheSidesList->getNumSides(); i++) {
ScriptList *pSL = TheSidesList->getSideInfo(i)->getScriptList();
if (pSL==NULL) continue;
Script *pScr;
for (pScr = pSL->getScript(); pScr; pScr=pScr->getNext()) {
- if ((name==pScr->getName())) {
- return pScr;
- }
+ m_scriptsByName.insert(std::make_pair(pScr->getName(), pScr));
}
ScriptGroup *pGroup;
for (pGroup = pSL->getScriptGroup(); pGroup; pGroup=pGroup->getNext()) {
+ m_groupsByName.insert(std::make_pair(pGroup->getName(), pGroup));
for (pScr = pGroup->getScript(); pScr; pScr=pScr->getNext()) {
- if ((name==pScr->getName())) {
- return pScr;
- }
+ m_scriptsByName.insert(std::make_pair(pScr->getName(), pScr));
}
}
}
- return 0; // Shouldn't ever happen.
+}
+
+//-------------------------------------------------------------------------------------------------
+/** Locates a script by name. */
+//-------------------------------------------------------------------------------------------------
+Script *ScriptEngine::findScript(const AsciiString& name)
+{
+ if (!m_scriptNameCacheValid) {
+ buildScriptNameCache();
+ }
+ ScriptNameMap::const_iterator it = m_scriptsByName.find(name);
+ return (it != m_scriptsByName.end()) ? it->second : 0; // Shouldn't ever happen.
}
//-------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index da48f03fc..e3dda3b4c 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -50,6 +50,8 @@
#include "GameLogic/AIPlayer.h"
#include "GameLogic/AIPathfind.h"
#include "GameLogic/ScriptEngine.h"
+#include "GameLogic/Scripts.h"
+#include "GameLogic/SidesList.h"
#include "Common/TunnelTracker.h"
#include "Common/StateMachine.h"
@@ -2744,6 +2746,76 @@ TEST(logicrandom_unchanged_honours_its_bounds)
CHECK_EQ( GetGameLogicRandomValueRealUnchanged( 3.0f, 3.0f ), 3.0f );
}
+// ---------------------------------------------------------------------------------------------
+// Looking a script up by name. This used to walk every side's script list and every group inside
+// it, on every call, and a subroutine call does it twice.
+// ---------------------------------------------------------------------------------------------
+
+TEST(scriptengine_finds_a_script_by_name_wherever_it_lives)
+{
+ SidesList sides;
+ SidesList *savedSides = TheSidesList;
+ TheSidesList = &sides;
+
+ Dict emptyDict;
+ sides.addSide( &emptyDict );
+ sides.addSide( &emptyDict );
+
+ // side 0: one loose script, and one inside a group
+ ScriptList *list0 = newInstance(ScriptList);
+ sides.getSideInfo( 0 )->setScriptList( list0 );
+
+ Script *loose = newInstance(Script);
+ loose->setName( AsciiString( "LooseScript" ) );
+ list0->addScript( loose, 0 );
+
+ ScriptGroup *group = newInstance(ScriptGroup);
+ group->setName( AsciiString( "TheGroup" ) );
+ list0->addGroup( group, 0 );
+
+ Script *inGroup = newInstance(Script);
+ inGroup->setName( AsciiString( "GroupedScript" ) );
+ group->addScript( inGroup, 0 );
+
+ // side 1: another loose one, to prove the search does not stop at the first side
+ ScriptList *list1 = newInstance(ScriptList);
+ sides.getSideInfo( 1 )->setScriptList( list1 );
+
+ Script *otherSide = newInstance(Script);
+ otherSide->setName( AsciiString( "OtherSideScript" ) );
+ list1->addScript( otherSide, 0 );
+
+ ScriptEngine engine;
+ CHECK( engine.findScriptByName( AsciiString( "LooseScript" ) ) == loose );
+ CHECK( engine.findScriptByName( AsciiString( "GroupedScript" ) ) == inGroup );
+ CHECK( engine.findScriptByName( AsciiString( "OtherSideScript" ) ) == otherSide );
+ CHECK( engine.findScriptByName( AsciiString( "NoSuchScript" ) ) == NULL );
+ CHECK( engine.findScriptByName( AsciiString() ) == NULL );
+
+ // asking twice must answer the same, whether the index was built on this call or the last one
+ CHECK( engine.findScriptByName( AsciiString( "GroupedScript" ) ) == inGroup );
+
+ // a new map throws the index away; the same engine must pick up the new scripts
+ sides.getSideInfo( 0 )->setScriptList( NULL );
+ list0->deleteInstance();
+ sides.getSideInfo( 1 )->setScriptList( NULL );
+ list1->deleteInstance();
+
+ ScriptList *reloaded = newInstance(ScriptList);
+ sides.getSideInfo( 0 )->setScriptList( reloaded );
+ Script *afterLoad = newInstance(Script);
+ afterLoad->setName( AsciiString( "AfterLoad" ) );
+ reloaded->addScript( afterLoad, 0 );
+
+ engine.newMap();
+ CHECK( engine.findScriptByName( AsciiString( "AfterLoad" ) ) == afterLoad );
+ CHECK( engine.findScriptByName( AsciiString( "LooseScript" ) ) == NULL );
+
+ sides.getSideInfo( 0 )->setScriptList( NULL );
+ reloaded->deleteInstance();
+ TheSidesList = savedSides;
+}
+
// ---------------------------------------------------------------------------------------------
// The particle system manager's bookkeeping. Finding a system by id, and unlinking a dead one,
// both used to be linear walks of every live system - on paths the renderer and the logic take