So I work on a mod that uses the Source Engine. I did a performance profile and found we were losing a LOT of FPS from it computing animations. The biggest offender here being stricmp()
For every animated model, it iterates through all of its sequences comparing the requested animation name with its list using stricmp. So with lots of animations and with longer animation names, that has to kill performance a significant amount especially since they're C strings which probably need to be measured each and every frame.
This is what that looks like:
for (int i = 0; i < pstudiohdr->GetNumSeq(); i++)
{
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( i );
if (stricmp( seqdesc.pszLabel(), label ) == 0)
return i;
}
So I want to optimize this a bit better. I was thinking std::strings would be better in this scenario, and I was also thinking of using std::map to map std::strings to the sequence index, so I could do something like:
return sequenceMap[label];
Instead of iterating over a for loop. Does std::map iterate over a for loop internally? I guess what I'm getting at is would using an std::map give a performance gain over the above for loop?
Lastly, is there any way to control the auto initialization of std::map data? For instance, if I do std::map < std::string, int >, trying to access a key value that doesn't exist should auto initialize the int to 0. I would like to say auto initialize it to -1 instead.
> is there any way to control the auto initialization of std::map data? For instance,
> if I do std::map < std::string, int >, trying to access a key value that doesn't exist
> should auto initialize the int to 0. I would like to say auto initialize it to -1 instead.
Yes; wrap it in a user-defined type with a custom default initialised value.
For example, for a mapped type that can appear as the type of a temple non-type parameter:
Note that if the mapped type is not a type that can appear as the type of a temple non-type parameter, the code for the wrapper would be quite a bit more involved.