Bart Janssens
Bart Janssens

Reputation: 211

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

Upvotes: 13

Views: 3176

Answers (1)

SoapBox
SoapBox

Reputation: 20609

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

Upvotes: 10

Related Questions