Boost Weak_Ptr:销毁比预期更昂贵

Jak*_*zer 6 c++ optimization performance boost

无论出于何种原因,我们都会从破坏弱指针中看到相当多的成本.这是罪魁祸首代码:

~weak_count() // nothrow  
{  
    if(pi_ != 0) pi_->weak_release();  // Consumes a huge chunk of our time.
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)  
    id_ = 0;  
#endif  
} 
Run Code Online (Sandbox Code Playgroud)

我们没有处于调试模式,并且调试挂钩未被使用.弱释放正在消耗真正惊人的时间.这是一个已知的问题?我们做错了吗?

Boost版本:1.36
编译器:VS2008编译器套件.

不幸的是,由于各种原因,我们被锁定在这个Boost版本中,所以我更想知道这些奇怪的支出是否可以在新版本上重复,或者代表已知不良做法的结果.我们只破坏了500k弱指针的顺序,它们不会因破坏相似数量的原始指针而在性能上产生明显的差异.当然不会增加2.5-4倍的成本.请注意,我们不会删除所述指针所针对的对象.这笔费用完全来自指针本身的破坏.

这里发生了什么?

Bil*_*eal 4

weak_ptr needs something like a shared_ptr to implement itself -- because it needs to be able to determine if the pointer still exists, it needs to have a reference counted structure somewhere that maintains it's own refcounts.

I.e., how does the weak_ptr determine if the object still exists unless the reference count is kept available somehow for it to get to? :)

如果您实际上不需要weak_ptr使用weak_ptr.

  • @Jake:谁告诉你的?他们错了。`Weak_ptr` 并不像原始指针那样高效,也从来没有打算这样做。 (3认同)