Boost logo

Boost :

From: Greg Colvin (gcolvin_at_[hidden])
Date: 2000-01-28 19:04:45


A very interesting implementation. Its existence may be a good reason to
deprecate the use_count() member of shared_ptr. I'm not too sure that
you would see a 4X improvement in typical applications though, since your
test does a lot of allocation and very little copying. I'd be interested
in seeing the results of more thorough tests.

----- Original Message -----
From: Borgerding, Mark A. <MarkAB_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, January 28, 2000 9:48 AM
Subject: [boost] new approach to smart ptrs -- no new()s is good news

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello boosters,
I am new on the mailing list but have followed boost for some time
now. I may have something to contribute.

I think I discovered a new approach for smart pointers (tell me if
this has been done). Instead of maintaining the reference count as
an integer created on the heap, the smart ptr objects internally
maintain a double-linked list in which they are members.

If a smart ptr object is the only node in the list, it is the last
reference and is .: responsible for cleaning up.

This approach has two distinct advantages over conventional ref.
counting techniques:
1) linked_ptrs need not allocate heap resources. This means no
allocators would be needed for generic use.
2) Fast! I compared linked_ptr with boost::shared_ptr using the
following code, and linked_ptr was more than 4 times faster!
   //typedef boost::shared_ptr<foo> smartptr;
   typedef linked_ptr<foo> smartptr;
   foo * dumbptr = new foo(42);// don't want to time foo's
constructor
   boost::timer t;
   smartptr sp(dumbptr);//c'tor from dumb *
   dumbptr = NULL;
   for (int i=0;i<1000;++i) {
      smartptr s2(sp); //copy c'tor
      smartptr s3; // default c'tor
      s3 = sp; // assign
   }
   cout << "Took " << t.elapsed() << "s using " <<
typeid(smartptr).name() << endl;

Attached is linked_ptr.hpp. The class has basically the same
interface as shared_ptr, except the function use_count() has been
omitted, since it would require linear complexity. Personally, I've
never needed to know anything about a reference count except whether
or not it was unique.

Also attached is a version of timer.cpp that I changed to allow more
precise measurement (I #ifdef'd some non-portable code into it). If
you try to run the above test code without these extensions, you'll
need to bump up the i<1000 number considerably to get any non-zero
timings.

I am interested in your comments & suggestions.

Incidentally, my dev. platforms are MSVC6 (NT4.0 on P2) at work and
gcc 2.95.2 (Mandrake 6.0 on Pentium)
Anybody know where to get a good, free <limits> header for gcc? The
Cygnus snapshots seem to lack this. I cannot fully test my timer.cpp
changes without it.

Mark Borgerding
markab (at) xetron . com
Software Engineer
Xetron Corporation


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk