Use find instead of [] and call StringTable->insert less.

This commit is contained in:
Daniel Buckmaster 2014-12-10 14:04:26 +11:00
parent 116276a105
commit 7004ec627e
2 changed files with 25 additions and 18 deletions

View file

@ -101,7 +101,7 @@ void SimNameDictionary::insert(SimObject* obj)
hashTableSize = newHashTableSize;
}
#else
root[StringTable->insert(obj->objectName)] = obj;
root[obj->objectName] = obj;
#endif
Mutex::unlockMutex(mutex);
}
@ -131,7 +131,8 @@ SimObject* SimNameDictionary::find(StringTableEntry name)
return NULL;
#else
Mutex::lockMutex(mutex);
SimObject* f = root[StringTable->insert(name)];
StringDictDef::iterator it = root.find(name);
SimObject* f = (it == root.end() ? NULL : it->second);
Mutex::unlockMutex(mutex);
return f;
#endif
@ -159,8 +160,8 @@ void SimNameDictionary::remove(SimObject* obj)
walk = &((*walk)->nextNameObject);
}
#else
const char* name = StringTable->insert(obj->objectName);
if (root[name])
const char* name = obj->objectName;
if (root.find(name) != root.end())
root.erase(name);
#endif
Mutex::unlockMutex(mutex);
@ -231,7 +232,7 @@ void SimManagerNameDictionary::insert(SimObject* obj)
hashTableSize = newHashTableSize;
}
#else
root[StringTable->insert(obj->objectName)] = obj;
root[obj->objectName] = obj;
#endif
Mutex::unlockMutex(mutex);
}
@ -258,7 +259,8 @@ SimObject* SimManagerNameDictionary::find(StringTableEntry name)
return NULL;
#else
SimObject* f = root[StringTable->insert(name)];
StringDictDef::iterator it = root.find(name);
SimObject* f = (it == root.end() ? NULL : it->second);
Mutex::unlockMutex(mutex);
return f;
#endif
@ -287,8 +289,8 @@ void SimManagerNameDictionary::remove(SimObject* obj)
walk = &((*walk)->nextManagerNameObject);
}
#else
const char* name = StringTable->insert(obj->objectName);
if (root[name])
StringTableEntry name = obj->objectName;
if (root.find(name) != root.end())
root.erase(name);
#endif
Mutex::unlockMutex(mutex);
@ -345,7 +347,8 @@ SimObject* SimIdDictionary::find(S32 id)
return NULL;
#else
SimObject* f = root[id];
SimObjectIdDictDef::iterator it = root.find(id);
SimObject* f = (it == root.end() ? NULL : it->second);
Mutex::unlockMutex(mutex);
return f;
#endif

View file

@ -41,26 +41,30 @@ class SimObject;
#include <string>
#include <unordered_map>
#ifndef _SIM_H_
#include "console/sim.h"
#endif
#include "core/strings/stringFunctions.h"
struct DictionaryHash
struct StringTableEntryHash
{
inline size_t operator()(const char* val) const
inline size_t operator()(StringTableEntry val) const
{
return (long)val;
return (size_t)val;
}
};
struct eqstr
struct StringTableEntryEq
{
inline bool operator()(const char *s1, const char *s2) const
inline bool operator()(StringTableEntry s1, StringTableEntry s2) const
{
return dStrcmp(s1, s2) == 0;
return s1 == s2;
}
};
typedef std::unordered_map<const char *, SimObject*, DictionaryHash, eqstr> StringDictDef;
typedef std::unordered_map<U32, SimObject*> U32DictDef;
typedef std::unordered_map<StringTableEntry, SimObject*, StringTableEntryHash, StringTableEntryEq> StringDictDef;
typedef std::unordered_map<SimObjectId, SimObject*> SimObjectIdDictDef;
#endif
@ -137,7 +141,7 @@ class SimIdDictionary
};
SimObject *table[DefaultTableSize];
#else
U32DictDef root;
SimObjectIdDictDef root;
#endif
void *mutex;