I have a class that has 3-4 data members of type std::map<string, vector<string>>
which is used for caching the data. Its instance is created once and the data is filled in all the maps from a service call. I have getter functions that are used to access these maps. (this has some thread locking logic in it as well)
These getter functions gets called a lot of times and I am worried about the performance as the map objects are being copied a lot of times.
class A {
private:
map<string, vector<string>> m1;
map<string, vector<string>> m2;
map<string, vector<string>> m3;
public:
map<string, vector<string>> getm1() { return m1; }
map<string, vector<string>> getm2() { return m2; }
map<string, vector<string>> getm3() { return m3; }
}
class B {
B() { }
static A a;
map<string, vector<string>> getm1() { return a.m1; }
map<string, vector<string>> getm2() { return a.m2; }
map<string, vector<string>> getm3() { return a.m3; }
}
There are multiple times these getter functions get called from class B
. Having intermediate knowledge on cpp, I know that the getter would return an entire map by value. But, would it be better to pass it by reference or using a shared pointer to the map like storing the member variables m1
, m2
, m3
as shared_ptr<map>
shared_ptr<map<string, vector<string>>> getm1() { return a.m1; }
Is this not a performance concern and will be taken care of, by the compiler ?
After reading a little bit about Return value optimization and understanding it a little bit, compilers can handle some optimizations. Is this a part of RVO ?
Thank you in advance.