Hi, I was skimming through scriptdictionary.cpp today to see some implementation details, and I couldn't help but notice the quite wordy implementation of CScriptDictionary::operator[]. Let me quote:
CScriptDictValue *CScriptDictionary::operator[](const string &key)
{
// Return the existing value if it exists, else insert an empty value
map<string, CScriptDictValue>::iterator it;
it = dict.find(key);
if( it == dict.end() )
it = dict.insert(map<string, CScriptDictValue>::value_type(key, CScriptDictValue())).first;
return &it->second;
}
It came to my attention that this is essentially a lengthy rewording of the following code:
CScriptDictValue *CScriptDictionary::operator[](const string &key)
{
return &dict[key];
}
(see http://en.cppreference.com/w/cpp/container/map/operator_at or http://www.cplusplus.com/reference/map/map/operator[]/ for reference for std::map operator[]). I wanted to see if something escapes my attention so I tested performance of both of these implementations with the following script:
void test() {
array<string> keys;
for (uint i = 0; i < 100000; i++)
keys.insertLast(formatInt(i, "", 10));
uint insertionTime = 0, accessTime = 0;
for (uint i = 0; i < 100; i++) {
dictionary d;
uint time1 = GetSystemTime();
for (uint j = 0; j < 100000; j++)
d[keys[j]] = j;
uint time2 = GetSystemTime();
for (uint j = 0; j < 100000; j++)
if (uint(d[keys[j]]) != j)
return 0;
uint time3 = GetSystemTime();
insertionTime += time2 - time1;
accessTime += time3 - time2;
}
Print("Insertion time: " + insertionTime + " ms\nAccess time: " + accessTime + " ms\n");
}
Using AngelScript compiled in MSVC 2013 I obtained the following results:
Current implementation:
In Release mode: Insertion time: 18334 ms; Access time: 11434 ms
Proposed implementation:
In Release mode: Insertion time: 15181 ms; Access time: 12121 ms
I.e., in all situations except for Release mode access the simpler implementation was faster. It may be a bit of a tradeoff but looks worth it. I don't know how other compilers would react though. Either way I thought you may be interested.