1

Let's say I have a simple C++ class that is used to store some data:

class MyClass
{
    std::string Name;
    int Data = 0;

    MyClass(std::string n, int d) : Name(n), Data(d)
    { }
};

Now I would like to have some kind of management class, which stores a mapping of instances to MyClass. I want to use that second class to access these instances later at runtime.

class MyClassMgmt
{
    std::map<std::string, MyClass*> Mapping;

    MyClassMgmt()
    { }

    void AddMyClass(MyClass* MyCls)
    {
        Mapping[MyCls->Name] = MyCls;
    }

    MyClass* GetMyClass(const std::string& Name) const
    {
        return Mapping.at(Name);
    }
};

My question is: If I would instead use MyClass directly with the help of static members to implement this management functionality, does that have a negative impact on overall performance then? Or are there any other drawbacks with this approach?

Something like this:

class MyClass
{
    std::string Name;
    int Data = 0;

    static std::map<std::string, MyClass*> Mapping;

    MyClass(std::string n, int d) : Name(n), Data(d)
    { }

    static void AddMyClass(MyClass* MyCls)
    {
        Mapping[MyCls->Name] = MyCls;
    }

    static MyClass* GetMyClass(const std::string& Name) const
    {
        return Mapping.at(Name);
    }
};
Matthias
  • 9,817
  • 14
  • 66
  • 125
  • It's uglier, that's for sure. You should keep the separation of concerns that you had going in the first place. Also, static variables (`Mapping` in your last example) are essentially globals. I'm sure there are enough Q&As on this site explaining the evil of those so I shouldn't have to repeat it. – dbandstra May 17 '17 at 23:47
  • Here readability and maitenance are much more important than performance. – Code-Apprentice May 17 '17 at 23:54
  • 1
    To expand on what @dbandstra mentioned: Static members are essentially global variables contained within the class' scope. They can be accessed from anywhere, but anything can do things it shouldn't be able to with them, and it can be hard to keep track of what reads & what writes. They can share data between threads, but aren't threadsafe. And so on. There'll likely be a slight performance benefit to using statics (no dereferencing pointers/references), but it's minimal enough to be insignificant (except possibly on embedded systems with limited memory). – Justin Time - Reinstate Monica May 18 '17 at 00:05
  • 2
    non trivial statics can cause huge headaches with double destruction with Linux linkers. – Nir Friedman May 18 '17 at 00:32

1 Answers1

4

You're not going to see performance problems related to the use of static.

Performance considerations will be affected more by your choice of container (e.g. std::map or std::unordered_map), the way you pass your parameters, and how you use (or avoid) dynamic memory management.

Other things you may want to consider are:

  • Protect your manager from multi-threaded access, and performance issues related to that;
  • Define what your program's behavior is if you construct a class with a duplicate name;
  • Correctly follow the Rule of Three (or Five);
  • Avoid the Static Initialization Order Fiasco, regarding your static member in MyClass;
  • Make MyClassMgmt follow a proper singleton pattern if it is indeed a singleton;
  • Pass arguments by const-reference if you don't want to modify or copy them;
  • Use move-semantics when appropriate, if you have identified copies as a bottleneck;
  • Use memory pools, if you have identified allocations as a bottleneck;
  • Actually profile your code before engaging in premature optimization.
Community
  • 1
  • 1
paddy
  • 60,864
  • 6
  • 61
  • 103