How much can the access cost of an unodered_map hinder the performance of a game?

Asked

Viewed 42 times

0

The cost of access to unordered_map can cause FPS to drop in a game if it is accessed thousands of times per second?

class var
{
public:
    template < typename t = double > 
    static inline auto get( const std::string& var_name ) -> t& 
    { 
         return ( t& ) m_vars[ var_name ]; 
    }
private:
    static inline std::unordered_map< std::string, double > m_vars;
};

// Isso vai ficar rodando infinitamente em vários loops
// Não vai ser apenas uma vez que vou chamar o método get, vou chamar varias e varias vezes em vários loops diferente
// Acredito que o acesso ao map vai comprometer muito o desempenho do jogo ocasionando perda de fps
if ( var::get< bool >( "ativar_efeitos" ) )
{

}

1 answer

0


Difficult to answer without a context, but it’s highly unlikely that this will affect significantly, at least not for him. Of course the function that calculates the code hash used to map, the quality of it, the keys to use, and how it’s used in general could affect something, but still, it would all have to be very tragic to affect a little bit. This rotating infinitely already worries a little more, I do not know if it should, but the problem has nothing to do with the map.

And if you need a map, there’s no point switching to something else. If you can use something else good and it’s something better, then you should use.

I also wonder if this is a good strategy, or if C++ is the appropriate language if you are doing this. But only a very detailed context to know.

I don’t even know if this code should be written like this. In the posted form it should not.

To tell you the truth the question is a little naive, which probably indicates that it will make several other mistakes much more serious than the one that will affect much more, and you won’t even notice.

  • This access will happen while the game is running, all the function that needs to use a variable I would use the get function instead of the variable because in my view it is much more organized than filling the project with global variables or putting everything inside a namespace. About the hash function I am using a library(cx_fnv1) to hash the string, in my tests it got much faster. And why do you think the code shouldn’t be written like that? I’m doing something wrong?

  • Do not use global variables. Use variables in a specific scope, but use variables, not a map, they are not created for this. I cannot give details without knowing the whole, but a class that only has an empty map and a get() seems wrong, the class name seems wrong, the generalization seems wrong. But from what I’ve commented now, everything seems wrong.

  • The class is not complete yet I have not finished writing, yet I intend to create methods to save/load the variable in a file, why I think creating a class with a map would be ideal as I could save/read all values of all variables with a simple for each.

  • 1

    he is.... No....

  • Could you indicate a better alternative? The only thing I can think of that meets my needs at the moment would be a map.

  • Make a good static class, there’s no need to create a map. Or do it in Ruby, Lua, another language. If you’re going to do it in C++ use the C features++.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.