After profiling my application I found out that there are far too many mallocs. I was surprised that shared_ptr and shared_array allocated memory for reference counts. Besides that an object which encapsulates reference counts contain two counts uses_count and weak_count as well as a pointer to the virtual table. To me this seems like overkill when I only need a simple reference counting class. Is there any way to tweak shared_ptr and shared_array to implement a simpler scheme? Without additional call to malloc and only with one counter: uses_count. Or maybe there's a simpler class somewhere in STL or Boost?
If you use boost::make_shared, the function will allocate the memory for both the ref counter and the object in one single call to new.
The additional counter for weak refereces should not be a big problem, since its only an additional 4 or 8 bytes that won't hurt.
If profiling shows that shared_ptr's implementation still is a bottleneck for your application, consider to use boost::intrusive_ptr. Also look for passing the shared_ptrs by reference rather than by value or pass them by moving them if a copy is needed. Of course, if you can use unique_ptr you should prefer those over shared_ptrs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With