Re: Lock-free reference counting
- From: Jon Harrop <jon@xxxxxxxxxxxxxxxxx>
- Date: Tue, 02 Dec 2008 00:18:26 +0000
Chris M. Thomasson wrote:
"Jon Harrop" <jon@xxxxxxxxxxxxxxxxx> wrote in message
news:8radna-lq4jsgqnUnZ2dnUVZ8tDinZ2d@xxxxxxxxxxxxxxxxx
Stephen Howe <sjhoweATdialDOTpipexDOTcom> wrote:
You said, "Which are scattered across memory.". I am pointing out that
the counters are _NOT_ scattered in memory...
That is incorrect.
if the reference counters are instrusive, the reference counters are
adjacent in memory to the object they count.
The objects themselves are scattered across memory => the references in
them
are also scattered.
Why do you "seem" to assume that the objects are always scattered across
memory?
That is the null hypothesis.
As to your last statement, that is only true if the object is passed
by value, it is not true if passed by reference or as a pointer, the
reference counts are _NEVER_ bumped.
Reference counts only exist to get bumped and they are bumped under a
variety of different circumstances including many where the data in the
object itself is not touched.
Passing objects to functions and procedures are one case where the counts
do not have to be mutated. In fact, the only time I ever need to
explicitly mutate counters is right before I hand a pointer to an object
off to another thread, and when a thread is done with a "shared" object.
Counter bumps can theoretically be optimized away in a variety of specific
circumstances (e.g. pass by reference to a function on the current thread).
My point is that real compilers do not automate these optimizations. So
either ordinary reference counted smart pointers are slow or you optimize
by hand, in which case you cannot claim to be using automatic memory
management and would be better off adopting a more efficient solution
anyway.
Even swapping a pair of reference counted smart pointers in an array
incurs several memory accesses to locations that would otherwise have
been untouched.
You can swap two reference counted objects without mutating their counts.
In theory, yes. In practice, GCC compiles the following C++ into a program
that is ~2x slower than the equivalent OCaml and which spends 60% of its
time bumping counters unnecessarily:
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/shared_ptr.hpp>
using namespace std;
using namespace boost;
int cmp(shared_ptr<int> a, shared_ptr<int> b)
{
return *a < *b;
}
int main(int argc, char *argv[])
{
vector<shared_ptr<int> > a;
const int n = (argc > 1 ? atoi(argv[1]) : 1000000);
cout << n << endl;
for (int i=0; i<n; ++i)
{
shared_ptr<int> k(new int);
*k = n - i;
a.push_back(k);
}
sort(a.begin(), a.end(), cmp);
return 0;
}
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?u
.
- Follow-Ups:
- Re: Lock-free reference counting
- From: Chris M. Thomasson
- Re: Lock-free reference counting
- References:
- Re: Lock-free reference counting
- From: Stephen Howe
- Re: Lock-free reference counting
- From: Jon Harrop
- Re: Lock-free reference counting
- From: Chris M. Thomasson
- Re: Lock-free reference counting
- Prev by Date: Re: Lock-free reference counting
- Next by Date: Re: Lock-free reference counting
- Previous by thread: Re: Lock-free reference counting
- Next by thread: Re: Lock-free reference counting
- Index(es):
Relevant Pages
|