【问题标题】:returning a map using getter in c++ performance issues在 C++ 性能问题中使用 getter 返回地图
【发布时间】:2018-02-20 22:05:30
【问题描述】:

我有一个类,它有 3-4 个 std::map<string, vector<string>> 类型的数据成员,用于缓存数据。它的实例被创建一次,并且数据被填充到来自服务调用的所有映射中。我有用于访问这些地图的 getter 函数。 (这也有一些线程锁定逻辑)

这些 getter 函数被调用了很多次,我担心地图对象被复制很多次时的性能。

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; } 
} 

从类B 中多次调用这些getter 函数。对 cpp 有中级知识,我知道 getter 会按值返回整个映射。但是,最好是通过引用传递它还是使用指向映射的共享指针,例如将成员变量m1、m2、m3 存储为shared_ptr&lt;map&gt;

  shared_ptr<map<string, vector<string>>> getm1() { return a.m1; } 

这不是性能问题,编译器会处理吗?

在阅读了一些关于返回值优化的内容并稍微理解之后,编译器可以处理一些优化。这是 RVO 的一部分吗?

提前谢谢你。

【问题讨论】:

  • 为什么不公开地图?或者,你的吸气剂的目的是什么?
  • 返回地图副本的原因是什么?除非客户想使用他们自己的副本,否则如果意图返回原始地图,则无论如何您都应该返回对地图的引用,而不是副本。
  • 您是打算返回副本,还是只想访问地图?
  • @CoolGeek 我只想访问地图 -- 那么这一切都变成了一个有争议的问题。返回对地图的引用。如果您确实返回了一个副本,那么一旦您开始使用该副本,就好像您正在访问原始版本一样,请准备好与迭代器相关的错误。
  • 如果你有多线程并且任何一个线程将修改地图而其他人使用它的机会,那么你不应该直接访问地图。没有引用,也没有共享指针。

标签: c++ std stdmap rvo


【解决方案1】:

将引用 (&amp;) 返回到 const 映射。

class A
{    
    using Map_type = std::map<std::string, std::vector<std::string>>;

    Map_type m1;
    Map_type m2;
    Map_type m3;

  public:
    const auto& getm1() const { return m1; }
    const auto& getm2() const { return m2; } 
    const auto& getm3() const { return m3; }
};

这将允许调用函数“只读”访问地图,而无需支付副本费用。


对于 C++11 及更低版本,必须在函数上声明返回类型,而不是 auto 作为返回类型。 此外,using 仅适用于 C++11 及更高版本,但对于早期的编译器,必须使用 typedef。

class A
{    
    typedef std::map<std::string, std::vector<std::string>> Map_type;

    Map_type m1;
    Map_type m2;
    Map_type m3;

  public:
    const Map_type& getm1() const { return m1; }
    const Map_type& getm2() const { return m2; } 
    const Map_type& getm3() const { return m3; }
};

【讨论】:

  • @Robinson const auto&amp; 可以。
  • @SergeyA 我从不喜欢 auto 函数返回类型。不过,其他工作人员也在使用它。
【解决方案2】:

如果您有内部锁定,那么您不能只返回对缓存地图的引用而不引起竞争条件。您还需要为调用者提供一种锁定数据不被修改的方法。如果您正在寻找的是效率,那么要考虑的一种设计是这样的:

class Cache
{
public:
    using mutex_type   = std::shared_timed_mutex;
    using reading_lock = std::shared_lock<mutex_type>;
    using writing_lock = std::unique_lock<mutex_type>;

    using map_type = std::map<std::string, std::vector<std::string>>;

    reading_lock lock_for_reading() const { return reading_lock{mtx}; }
    writing_lock lock_for_writing()       { return writing_lock{mtx}; }

    map_type const& use() const { return m; }

private:

    void update_map()
    {
        // protect every update with a writing_lock
        auto lock = lock_for_writing();

        // safely update the cached map
        m["wibble"] = {"fee", "fie", "foe", "fum"};
    }

    mutable mutex_type mtx;
    map_type   m = {{"a", {"big", "wig"}}, {"b", {"fluffy", "bunny"}}};

};

int main()
{
    Cache cache;

    { // start a scope just for using the map

        // protect every access with a reading_lock
        auto lock = cache.lock_for_reading();

        // safely use the cached map
        for(auto const& s: cache.use().at("a"))
            std::cout << s << '\n';

    } // the lock is released here

    // ... etc ...
}

通过向调用者提供锁定,您可以让他们在读取数据时保护数据免受竞争条件的影响。通过同时使用读写锁,您可以获得性能,因为您知道调用者不会修改缓存的数据。

在内部,当您更新缓存的地图时,您需要使用 writing_lock 以确保在更新时没有其他人正在阅读它们。

为了提高效率,您可能希望为每张地图使用单独的mutex,但这取决于您的具体情况。

注意:此解决方案让调用者负责正确锁定数据。这里建议了一个更强大(和复杂)的解决方案:An idea for the GSL to make multithreading code safer,这里有一个示例实现:gsl_lockable。

【讨论】:

  • 这样的设计不允许在阅读时修改容器吗?
  • @RobertAndrzejuk 如果您按照示例中所示应用锁,则不会。 write_lock 将在继续之前踢出所有读者。但它确实让调用者负责正确锁定资源。对此进行改进变得相当复杂。
  • 我有一个类似的读写锁定机制。感谢您的详细解释。真的很感激!
猜你喜欢
  • 2018-03-23
  • 2013-01-08
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-24
相关资源
最近更新 更多