Boost Weak_Ptr: Destruction is more expensive than expected

For some reason, we see quite a lot of the cost of destroying weak pointers. Here is the culprit code:

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

We are not in debug mode, and debug hooks are not involved. Weak release consumes a truly phenomenal amount of time. Is this a known issue? Are we doing something wrong?

Boost version: 1.36
Compiler: VS2008 Compiler Suite.

We are locked into this version of Boost for a number of reasons, unfortunately, so I am more wondering if these strange costs duplicate newer versions or are the result of known bad practice. We destroy only about 500 thousand. Weak pointers, which should not lead to a noticeable difference in performance from the destruction of a similar number of source pointers. Of course, this is not an increase in value by 2.5-4 times. Please note: we do not delete objects that the specified pointers are aimed at. This expense is solely due to the destruction of the pointers themselves.

What's going on here?

+6
c ++ performance optimization boost
source share
2 answers

weak_ptr requires something like shared_ptr to implement itself - because it must be able to determine if a pointer exists, it must have a reference-counting structure somewhere that supports its own refcounts.

Ie, how weak_ptr determine if the object still exists, if the reference counter is not available to access it? :)

You may be able to bypass raw pointers instead of weak_ptr s if you don't need to take ownership of a piece of code with weak_ptr .

+4
source share

Most likely, the cost comes from the atomic decrement of the link account, which is quite expensive compared, for example, with whole assignments, and can entail a cache (or be very extreme, page) skipping / error and be atomic op, it can also carry such things like cache line invalidation.

However, destroying a raw pointer is non-op, so I'm not quite sure what you expect from weak_ptr, but it is physically impossible for it to offer the same cost of destruction if it offers any kind of destruction semantics.

Equally, it is more than possible that you are not using them. Forced signs are not a silver mark - you still have to think about who owns the property. The widespread use of a weak pointer tells me that you really haven't thought through your property semantics.

Finally, there is an implementation of shared_ptr and weak_ptr in MSVC 2008 in the std :: tr1 namespace. You can try this.

+2
source share

All Articles