Should we pass a shared_ptr by reference or by value?
When a function takes a shared_ptr
(from boost or C++11 STL), are you passing it:
by const reference:
void foo(const shared_ptr<T>& p)
or by value:
void foo(shared_ptr<T> p)
?
I would prefer the first method because I suspect it would be faster. But is this really worth it or are there any additional issues?
Could you please give the reasons for your choice or if the case, why you think it does not matter.
This question has been discussed and answered by Scott, Andrei and Herb during Ask Us Anything session at C++ and Beyond 2011. Watch from 4:34 on shared_ptr
performance and correctness.
Shortly, there is no reason to pass by value, unless the goal is to share ownership of an object (eg. between different data structures, or between different threads).
Unless you can move-optimise it as explained by Scott Meyers in the talk video linked above, but that is related to actual version of C++ you can use.
A major update to this discussion has happened during GoingNative 2012 conference's Interactive Panel: Ask Us Anything! which is worth watching, especially from 22:50.
Here's Herb Sutter's take
Guideline: Don’t pass a smart pointer as a function parameter unless you want to use or manipulate the smart pointer itself, such as to share or transfer ownership.
Guideline: Express that a function will store and share ownership of a heap object using a by-value shared_ptr parameter.
Guideline: Use a non-const shared_ptr& parameter only to modify the shared_ptr. Use a const shared_ptr& as a parameter only if you’re not sure whether or not you’ll take a copy and share ownership; otherwise use widget* instead (or if not nullable, a widget&).
Personally I would use a const
reference. There is no need to increment the reference count just to decrement it again for the sake of a function call.
Pass by const
reference, it's faster. If you need to store it, say in some container, the ref. count will be auto-magically incremented by the copy operation.
I ran the code below, once with foo
taking the shared_ptr
by const&
and again with foo
taking the shared_ptr
by value.
void foo(const std::shared_ptr<int>& p)
{
static int x = 0;
*p = ++x;
}
int main()
{
auto p = std::make_shared<int>();
auto start = clock();
for (int i = 0; i < 10000000; ++i)
{
foo(p);
}
std::cout << "Took " << clock() - start << " ms" << std::endl;
}
Using VS2015, x86 release build, on my intel core 2 quad (2.4GHz) processor
const shared_ptr& - 10ms
shared_ptr - 281ms
The copy by value version was an order of magnitude slower.
If you are calling a function synchronously from the current thread, prefer the const&
version.