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): stop recomputing what was just computed

Olcay Seygan24f77b2204a3
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Weapon.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Weapon.h
index 838f7f42c..a9c1a48f7 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Weapon.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Weapon.h
@@ -883,6 +883,11 @@ private:
};
std::vector<WeaponTemplate*> m_weaponTemplateVector;
+
+ // Looking a weapon up by name walked that whole vector. The same templates are indexed here
+ // by name key; the vector stays because load order is what iterates it.
+ typedef std::hash_map<NameKeyType, WeaponTemplate*, rts::hash<NameKeyType>, rts::equal_to<NameKeyType> > WeaponTemplateMap;
+ WeaponTemplateMap m_weaponTemplateHashMap;
std::list<WeaponDelayedDamageInfo> m_weaponDDI;
};
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp
index 35a5f6aeb..c6b1dec7f 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp
@@ -3701,16 +3701,23 @@ void ParticleSystemDebugDisplay( DebugDisplayInterface *dd, void *, FILE *fp )
// ------------------------------------------------------------------------------------------------
static Real angleBetween(const Coord2D *vecA, const Coord2D *vecB)
{
- if (!(vecA && vecA->length() && vecB && vecB->length())) {
- return 0.0;
+ if (!vecA || !vecB) {
+ return 0.0f;
+ }
+
+ // Each length was a square root, and the guard above computed both of them and then threw
+ // them away so the two lines below could compute them again.
+ const Real lengthA = vecA->length();
+ const Real lengthB = vecB->length();
+
+ if (lengthA == 0.0f || lengthB == 0.0f) {
+ return 0.0f;
}
- Real lengthA = vecA->length();
- Real lengthB = vecB->length();
Real dotProduct = (vecA->x * vecB->x + vecA->y * vecB->y);
Real cosTheta = dotProduct / (lengthA * lengthB);
- // If the dotproduct is 0.0, then they are orthogonal
+ // If the dot product is 0.0, then they are orthogonal
if (dotProduct == 0.0f) {
if (vecB->x > 0) {
return PI;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp
index da4c3ce6b..e20c221ee 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp
@@ -1583,6 +1583,7 @@ WeaponStore::~WeaponStore()
wt->deleteInstance();
}
m_weaponTemplateVector.clear();
+ m_weaponTemplateHashMap.clear();
}
//-------------------------------------------------------------------------------------------------
@@ -1630,10 +1631,9 @@ const WeaponTemplate *WeaponStore::findWeaponTemplate( AsciiString name ) const
//-------------------------------------------------------------------------------------------------
WeaponTemplate *WeaponStore::findWeaponTemplatePrivate( NameKeyType key ) const
{
- // search weapon list for name
- for (Int i = 0; i < m_weaponTemplateVector.size(); i++)
- if( m_weaponTemplateVector[ i ]->getNameKey() == key )
- return m_weaponTemplateVector[i];
+ WeaponTemplateMap::const_iterator it = m_weaponTemplateHashMap.find( key );
+ if( it != m_weaponTemplateHashMap.end() )
+ return it->second;
return NULL;
@@ -1653,6 +1653,9 @@ WeaponTemplate *WeaponStore::newWeaponTemplate(AsciiString name)
wt->m_nameKey = TheNameKeyGenerator->nameToKey( name );
m_weaponTemplateVector.push_back(wt);
+ // insert, never overwrite: the linear scan this replaces answered with the first match
+ m_weaponTemplateHashMap.insert( WeaponTemplateMap::value_type( wt->m_nameKey, wt ) );
+
return wt;
}
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/HeightMap.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/HeightMap.h
index 3cd1b8f89..e51499ed0 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/HeightMap.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/HeightMap.h
@@ -111,8 +111,34 @@ protected:
UnsignedInt doTheDynamicLight(VERTEX_FORMAT *vb, VERTEX_FORMAT *vbMirror, Vector3*light, Vector3*normal, W3DDynamicLight *pLights[], Int numLights);
- Int getXWithOrigin(Int x);
- Int getYWithOrigin(Int x);
+ //
+ // Gets the index that corresponds to the data. For example, if the columns are shifted by 3,
+ // index 3 is actually the first row of polygons, or 0. Yes it is confusing, but it makes
+ // sliding the map 10x faster.
+ //
+ // Inline because the vertex fill loops call these eight or more times per vertex, on a whole
+ // tile at a time. Out of line they were real calls, and the compiler could not see that the
+ // repeated calls with the same argument all return the same thing.
+ //
+ Int getXWithOrigin(Int x) const
+ {
+ x -= m_originX;
+ if (x<0) x+= m_x-1;
+ if (x>= m_x-1) x-=m_x-1;
+ if (x<0) x = 0;
+ if (x>= m_x-1) x=m_x-1;
+ return x;
+ }
+
+ Int getYWithOrigin(Int y) const
+ {
+ y -= m_originY;
+ if (y<0) y+= m_y-1;
+ if (y>= m_y-1) y-=m_y-1;
+ if (y<0) y = 0;
+ if (y>= m_y-1) y=m_y-1;
+ return y;
+ }
///update vertex diffuse color for dynamic lights inside given rectangle
Int updateVBForLight(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, W3DDynamicLight *pLights[], Int numLights);
Int updateVBForLightOptimized(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, W3DDynamicLight *pLights[], Int numLights);
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
index 702cfac37..97ad972ca 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
@@ -262,48 +262,6 @@ UnsignedInt HeightMapRenderObjClass::doTheDynamicLight(VERTEX_FORMAT *vb, VERTEX
#endif
}
-//=============================================================================
-// HeightMapRenderObjClass::getXWithOrigin
-//=============================================================================
-/** Gets the x index that corresponds to the data. For example, if the columns
-are shifted by 3, index 3 is actually the first row of polygons, or 0. Yes it
-is confusing, but it makes sliding the map 10x faster. */
-//=============================================================================
-Int HeightMapRenderObjClass::getXWithOrigin(Int x)
-{
- x -= m_originX;
- if (x<0) x+= m_x-1;
- if (x>= m_x-1) x-=m_x-1;
-#ifdef _DEBUG
- DEBUG_ASSERTCRASH (x>=0, ("X out of range."));
- DEBUG_ASSERTCRASH (x<m_x-1, ("X out of range."));
-#endif
- if (x<0) x = 0;
- if (x>= m_x-1) x=m_x-1;
- return x;
-}
-
-//=============================================================================
-// HeightMapRenderObjClass::getYWithOrigin
-//=============================================================================
-/** Gets the y index that corresponds to the data. For example, if the rows
-are shifted by 3, index 3 is actually the first row of polygons, or 0. Yes it
-is confusing, but it makes sliding the map 10x faster. */
-//=============================================================================
-Int HeightMapRenderObjClass::getYWithOrigin(Int y)
-{
- y -= m_originY;
- if (y<0) y+= m_y-1;
- if (y>= m_y-1) y-=m_y-1;
-#ifdef _DEBUG
- DEBUG_ASSERTCRASH (y>=0, ("Y out of range."));
- DEBUG_ASSERTCRASH (y<m_y-1, ("Y out of range."));
-#endif
- if (y<0) y = 0;
- if (y>= m_y-1) y=m_y-1;
- return y;
-}
-
//=============================================================================
// HeightMapRenderObjClass::updateVB
//=============================================================================
@@ -1864,7 +1822,10 @@ void HeightMapRenderObjClass::updateCenter(CameraClass *camera , RefRenderObjLis
}
if (abs(deltaX)>CENTER_LIMIT || abs(deltaY)>CENTER_LIMIT) {
- if (abs(deltaY) >= CENTER_LIMIT) {
+ // The row test was >= where the guard above and the column test below are both >, so a
+ // sideways scroll of exactly CENTER_LIMIT rows also rebuilt the rows - and then took
+ // the early return below and left the columns for the next frame.
+ if (abs(deltaY) > CENTER_LIMIT) {
if (m_map->setDrawOrg(m_map->getDrawOrgX(), newOrgY)) {
Int minY = 0;
Int maxY = 0;