C ++ weak_ptr创建性能 [英] C++ weak_ptr creation performance

查看:186
本文介绍了C ++ weak_ptr创建性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过,创建或复制std :: shared_ptr涉及一些开销(引用计数器的原子增量等)。



但是创建std :: weak_ptr:

  Obj * obj = new Obj 
// fast
Obj * o = obj;
// slow
std :: shared_ptr< Obj> a(o);
// slow
std :: shared_ptr< Obj> b(a);
//慢?
std :: weak_ptr< Obj> c(b);



我希望在一些更快的性能,但我知道共享指针仍然必须递增弱引用计数器。
这是否仍然像复制一个shared_ptr到另一个慢?

解决方案

a href =http://stackoverflow.com/users/2112028/alec-teal> Alec的非常有趣的描述在他以前的项目中使用的共享/ weak_ptr系统,我想给一些更多的细节一个典型的 std :: shared_ptr / weak_ptr 实现可能会发生什么:

  // slow 
std :: shared_ptr< Obj> a(o);

上述结构中的主要费用是分配一块内存来保存两个引用计数。这里不需要进行原子操作(除了在 operator new 下可以执行的操作)。

  // slow 
std :: shared_ptr< Obj> b(a);

复制构造中的主要费用通常是单个原子增量。

  // slow? 
std :: weak_ptr< Obj> c(b);

weak_ptr 构造函数中的主要费用通常是单个原子增量。我希望此构造函数的性能几乎与 shared_ptr 复制构造函数的性能相同。



两个重要的构造函数需要注意的是:

  std :: shared_ptr< Obj> d(std :: move(a)); // shared_ptr(shared_ptr&&); 
std :: weak_ptr< Obj> e(std :: move(c)); // weak_ptr(weak_ptr&&);

(以及匹配的移动分配操作符)



move构造函数不需要任何原子操作。它们只是将引用计数从rhs复制到lhs,并使rhs == nullptr。



移动赋值运算符只有在lhs!= nullptr之前的赋值。在移动分配之前的大部分时间(例如,在向量< shared_ptr< T>< / code>中)lhs == nullptr,因此没有原子操作所有。



后者( weak_ptr 移动成员​​)实际上不是C ++ 11,作者: LWG 2315 。但我希望它已经被大多数实现实现(我知道它已经在 libc ++ 中实现)。



这些移动成员将用于在容器中扫描智能指针时,例如在矢量< shared_ptr< T>> :: insert / erase 下,并且与使用智能指针复制成员相比具有可测量的积极影响。

我指出来,这样你就知道如果你有机会移动而不是复制 shared_ptr / weak_ptr 是值得的麻烦,键入几个额外的字符这样做。


I've read that creating or copying a std::shared_ptr involves some overhead (atomic increment of reference counter etc..).

But what about creating a std::weak_ptr from it instead:

Obj * obj = new Obj();
// fast
Obj * o = obj;
// slow
std::shared_ptr<Obj> a(o);
// slow
std::shared_ptr<Obj> b(a);
// slow ?
std::weak_ptr<Obj> c(b);

I was hoping in some faster performance, but i know that the shared pointer still have to increment the weak references counter.. So is this still as slow as copying a shared_ptr into another?

解决方案

In addition to Alec's very interesting description of the shared/weak_ptr system used in his previous projects, I wanted to give a little more detail on what is likely to be happening for a typical std::shared_ptr/weak_ptr implementation:

// slow
std::shared_ptr<Obj> a(o);

The main expense in the above construction is to allocate a block of memory to hold the two reference counts. No atomic operations need be done here (aside from what the implementation may or may not do under operator new).

// slow
std::shared_ptr<Obj> b(a);

The main expense in the copy construction is typically a single atomic increment.

// slow ?
std::weak_ptr<Obj> c(b);

The main expense in the this weak_ptr constructor is typically a single atomic increment. I would expect the performance of this constructor to be nearly identical to that of the shared_ptr copy constructor.

Two other important constructors to be aware of are:

std::shared_ptr<Obj> d(std::move(a));  // shared_ptr(shared_ptr&&);
std::weak_ptr<Obj> e(std::move( c ));  // weak_ptr(weak_ptr&&);

(And matching move assignment operators as well)

The move constructors do not require any atomic operations at all. They just copy the reference count from the rhs to the lhs, and make the rhs == nullptr.

The move assignment operators require an atomic decrement only if the lhs != nullptr prior to the assignment. The bulk of the time (e.g. within a vector<shared_ptr<T>>) the lhs == nullptr prior to a move assignment, and so there are no atomic operations at all.

The latter (the weak_ptr move members) are not actually C++11, but are being handled by LWG 2315. However I would expect it to already be implemented by most implementations (I know it is already implemented in libc++).

These move members will be used when scooting smart pointers around in containers, e.g. under vector<shared_ptr<T>>::insert/erase, and can have a measurable positive impact compared to the use of the smart pointer copy members.

I point it out so that you will know that if you have the opportunity to move instead of copy a shared_ptr/weak_ptr, it is worth the trouble to type the few extra characters to do so.

这篇关于C ++ weak_ptr创建性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆