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

View file

@ -41,26 +41,30 @@ class SimObject;
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#ifndef _SIM_H_
#include "console/sim.h"
#endif
#include "core/strings/stringFunctions.h" #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<StringTableEntry, SimObject*, StringTableEntryHash, StringTableEntryEq> StringDictDef;
typedef std::unordered_map<U32, SimObject*> U32DictDef; typedef std::unordered_map<SimObjectId, SimObject*> SimObjectIdDictDef;
#endif #endif
@ -137,7 +141,7 @@ class SimIdDictionary
}; };
SimObject *table[DefaultTableSize]; SimObject *table[DefaultTableSize];
#else #else
U32DictDef root; SimObjectIdDictDef root;
#endif #endif
void *mutex; void *mutex;