Skip to content

Commit fa4aabf

Browse files
authored
perf(particlesys): Reduce cost of ParticleSystemManager::findParticleSystem() by 80% (TheSuperHackers#2217)
1 parent 41bdc8a commit fa4aabf

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

Core/GameEngine/Include/Common/STLTypedefs.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class Object;
6464
enum NameKeyType CPP_11(: Int);
6565
enum ObjectID CPP_11(: Int);
6666
enum DrawableID CPP_11(: Int);
67+
enum ParticleSystemID CPP_11(: Int);
6768

6869
#include <algorithm>
6970
#include <bitset>
@@ -147,6 +148,7 @@ namespace rts
147148
}
148149
};
149150

151+
#ifdef USING_STLPORT
150152
template<> struct hash<NameKeyType>
151153
{
152154
size_t operator()(NameKeyType nkt) const
@@ -174,6 +176,16 @@ namespace rts
174176
}
175177
};
176178

179+
template<> struct hash<ParticleSystemID>
180+
{
181+
size_t operator()(ParticleSystemID nkt) const
182+
{
183+
std::hash<UnsignedInt> tmp;
184+
return tmp((UnsignedInt)nkt);
185+
}
186+
};
187+
#endif // USING_STLPORT
188+
177189
template<> struct hash<const Char*>
178190
{
179191
size_t operator()(const Char* s) const

Core/GameEngine/Include/GameClient/ParticleSys.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,8 @@ class ParticleSystemManager : public SubsystemInterface,
742742
public:
743743

744744
typedef std::list<ParticleSystem*> ParticleSystemList;
745-
typedef std::list<ParticleSystem*>::iterator ParticleSystemListIt;
745+
typedef ParticleSystemList::iterator ParticleSystemListIt;
746+
typedef std::hash_map<ParticleSystemID, ParticleSystem *, rts::hash<ParticleSystemID>, rts::equal_to<ParticleSystemID> > ParticleSystemIDMap;
746747
typedef std::hash_map<AsciiString, ParticleSystemTemplate *, rts::hash<AsciiString>, rts::equal_to<AsciiString> > TemplateMap;
747748

748749
ParticleSystemManager( void );
@@ -831,6 +832,7 @@ class ParticleSystemManager : public SubsystemInterface,
831832

832833
private:
833834
TemplateMap m_templateMap; ///< a hash map of all particle system templates
835+
ParticleSystemIDMap m_systemMap; ///< a hash map of all particle systems
834836
};
835837

836838
/// The particle system manager singleton

Core/GameEngine/Source/GameClient/System/ParticleSys.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,6 +2945,7 @@ void ParticleSystemManager::reset( void )
29452945
deleteInstance(m_allParticleSystemList.front());
29462946
}
29472947
DEBUG_ASSERTCRASH(m_particleSystemCount == 0, ("ParticleSystemManager::reset: m_particleSystemCount is %u, not 0", m_particleSystemCount));
2948+
DEBUG_ASSERTCRASH(m_systemMap.size() == 0, ("ParticleSystemManager::reset: m_systemMap size is %zu, not 0", m_systemMap.size()));
29482949

29492950
// sanity, our lists must be empty!!
29502951
for( Int i = 0; i < NUM_PARTICLE_PRIORITIES; ++i )
@@ -3040,21 +3041,15 @@ ParticleSystemID ParticleSystemManager::createAttachedParticleSystemID(
30403041
ParticleSystem *ParticleSystemManager::findParticleSystem( ParticleSystemID id )
30413042
{
30423043
if (id == INVALID_PARTICLE_SYSTEM_ID)
3043-
return nullptr; // my, that was easy
3044-
3045-
ParticleSystem *system = nullptr;
3046-
3047-
for( ParticleSystemListIt it = m_allParticleSystemList.begin(); it != m_allParticleSystemList.end(); ++it ) {
3048-
system = *it;
3049-
DEBUG_ASSERTCRASH(system != nullptr, ("ParticleSystemManager::findParticleSystem: ParticleSystem is null"));
3044+
return nullptr;
30503045

3051-
if( system->getSystemID() == id ) {
3052-
return system;
3053-
}
3046+
ParticleSystemIDMap::const_iterator it = m_systemMap.find(id);
3047+
if (it != m_systemMap.end())
3048+
{
3049+
return it->second;
30543050
}
30553051

30563052
return nullptr;
3057-
30583053
}
30593054

30603055
// ------------------------------------------------------------------------------------------------
@@ -3218,6 +3213,7 @@ void ParticleSystemManager::friend_addParticleSystem( ParticleSystem *particleSy
32183213
{
32193214
DEBUG_ASSERTCRASH(particleSystemToAdd != nullptr, ("ParticleSystemManager::friend_addParticleSystem: ParticleSystem is null"));
32203215
m_allParticleSystemList.push_back(particleSystemToAdd);
3216+
m_systemMap[particleSystemToAdd->getSystemID()] = particleSystemToAdd;
32213217
++m_particleSystemCount;
32223218
}
32233219

@@ -3228,6 +3224,7 @@ void ParticleSystemManager::friend_removeParticleSystem( ParticleSystem *particl
32283224
{
32293225
ParticleSystemListIt it = std::find(m_allParticleSystemList.begin(), m_allParticleSystemList.end(), particleSystemToRemove);
32303226
if (it != m_allParticleSystemList.end()) {
3227+
m_systemMap.erase((*it)->getSystemID());
32313228
m_allParticleSystemList.erase(it);
32323229
--m_particleSystemCount;
32333230
} else {

0 commit comments

Comments
 (0)