Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost shared_ptr dereference cost

I am trying to compare performance between raw pointers, boost shared_ptr and boost weak_ptr. On the dereferencing part, I expected shared_ptr and raw_ptr to be equal, but results show that shared_ptr is about twice as slow. For the test, I am creating an array with either pointers or shared pointers to ints, and then dereferencing in a loop like this:

int result;
for(int i = 0; i != 100; ++i)
{
  for(int i = 0; i != SIZE; ++i)
    result += *array[i];
}

The full code for the test can be found here: https://github.com/coolfluid/coolfluid3/blob/master/test/common/utest-ptr-benchmark.cpp

Test timings for an optimized build without assertions can be found here: http://coolfluidsrv.vki.ac.be/cdash/testDetails.php?test=145592&build=7777

The values of interest are "DerefShared time" and "DerefRaw time"

I guess the test may be flawed somehow, but I failed to figure out where the difference comes from. Profiling shows the operator* from shared_ptr gets inlined, it just seems to take more time. I double-checked that the boost assertion is off.

I would be very grateful if anyone can explain where the difference might come from.

Additional stand-alone test: https://gist.github.com/1335014

like image 252
Bart Janssens Avatar asked Nov 02 '11 21:11

Bart Janssens


People also ask

What happens when shared_ptr goes out of scope?

All the instances point to the same object, and share access to one "control block" that increments and decrements the reference count whenever a new shared_ptr is added, goes out of scope, or is reset. When the reference count reaches zero, the control block deletes the memory resource and itself.

Should you use shared_ptr?

Use shared_ptr if you want to share ownership of a resource. Many shared_ptr can point to a single resource. shared_ptr maintains reference count for this propose. when all shared_ptr's pointing to resource goes out of scope the resource is destroyed.

Are shared pointers expensive?

shared_ptr is more expensive than unique_ptr : shared_ptr is ~twice the size of a raw pointer, because it needs to store a pointer to the control block too. That control block has to be allocated, as well as the managed object.

Is shared_ptr slow?

When we create an object with new operator in shared_ptr there will be two dynamic memory allocations that happen, one for object from the new and the second is the manager object created by the shared_ptr constructor. Since memory allocations are slow, creating shared_ptr is slow when compared to raw pointer.


1 Answers

As Alan Stokes said in his comment, this is due to cache effects. Shared Pointers include a reference count, which means they are physically larger in memory than a raw pointer. When stored in a contiguous array, you get less pointers per cache line, which means the loop has to go out to main memory more often than it would for a raw pointer.

You can observe this behavior by, in your raw pointer test, allocating SIZE*2 ints, but also changing the de-reference loop to stride by i+=2 instead of ++i. Doing this yielded approximately the same results in my tests. My code for the raw test is below.

#include <iostream>
#include <boost/timer.hpp>

#define SIZE 1000000

typedef int* PtrT;

int do_deref(PtrT* array)
{
    int result = 0;
    for(int i = 0; i != 1000; ++i)
    {
        for(int i = 0; i != SIZE*2; i+=2)
            result += *array[i];
    }

    return result;
}

int main(void)
{
    PtrT* array = new PtrT[SIZE*2];
    for(int i = 0; i != SIZE*2; ++i)
        array[i] = new int(i);
    boost::timer timer;
    int result = do_deref(array);
    std::cout << "deref took " << timer.elapsed() << "s" << std::endl;
    return result;
}

Incidentally, using boost::make_shared<int>(i) instead of PtrT(new int(I))allocates the reference count and the object together in memory rather than in separate locations. In my tests this improved the performance of shared pointer dereference by about 10-20%. Code for that is below:

#include <iostream>
#include <boost/timer.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#define SIZE 1000000

typedef boost::shared_ptr<int> PtrT;

int do_deref(PtrT* array)
{
    int result = 0;
    for(int j = 0; j != 1000; ++j)
    {
        for(int i = 0; i != SIZE; ++i)
            result += *array[i];
    }

    return result;
}

int main(void)
{
    PtrT* array = new PtrT[SIZE];
    for(int i = 0; i != SIZE; ++i)
        array[i] = boost::make_shared<int>(i);
    boost::timer timer;
    int result = do_deref(array);
    std::cout << "deref took " << timer.elapsed() << "s" << std::endl;
    return result;
}

My Results (x86-64 Unbuntu 11 VM):

Original Raw: 6.93
New Raw: 12.9
Original Shared: 12.7
New Shared: 10.59
like image 176
SoapBox Avatar answered Oct 07 '22 12:10

SoapBox