Topic: Implementation for Policy Based smart pointers


Author: "Axter" <google@axter.com>
Date: Fri, 7 Oct 2005 21:12:44 CST
Raw View
I'm looking for implementation of policy based smart pointers like that
described in the following link:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1681.pdf

Does any one have any good links?
And does anyone know what is the status of adding a policy based smart
pointer to the C++ standard?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: cppljevans@cox-internet.com (Larry Evans)
Date: Sat, 8 Oct 2005 04:24:46 GMT
Raw View
On 10/07/2005 10:12 PM, Axter wrote:
> I'm looking for implementation of policy based smart pointers like that
> described in the following link:
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1681.pdf
>
> Does any one have any good links?
> And does anyone know what is the status of adding a policy based smart
> pointer to the C++ standard?

See:

http://cvs.sourceforge.net/viewcvs.py/boost-sandbox/boost-sandbox/boost/policy_ptr/

and the corresponding test files under .../libs/policy_ptr/test.

Scant documentation exists.  I've had trouble writing the docs for the
cycle collecting code because of trouble understanding xml, xsl, boost
build and boostbook :(  I should have something for boost/fields_visitor
( which is used by the cycle collector to enumerate the smart ptrs in
an object) pretty soon.

I've also been in touch with scheng@innaworks.com who also seems
interested and is making some sort of comparison of gc methods
for c++.  Hopefully he'll share that with boost.

HTH.

-regards,
Larry

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: John Nagle <nagle@animats.com>
Date: 8 Oct 2005 05:40:01 GMT
Raw View
Axter wrote:
> I'm looking for implementation of policy based smart pointers like that
> described in the following link:
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1681.pdf
>
> Does any one have any good links?
> And does anyone know what is the status of adding a policy based smart
> pointer to the C++ standard?

    This is an attempt at premature optimization.  It
attemtps to solve an implementation performance problem at the
template level, rather than at the optimizer level.  It would
be more productive to work on implementations which optimize
the checking, rather than adding mechanisms for turning it off.

    What we really need are smart pointers which can have
checking and reference counting optimized by compilers.
Much checking and count updating can be hoisted out of loops.

    John Nagle
    Animats

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: gi2nospam@mariani.ws (Gianni Mariani)
Date: Sun, 9 Oct 2005 07:41:31 GMT
Raw View
John Nagle wrote:
..
>
>    This is an attempt at premature optimization.  It
> attemtps to solve an implementation performance problem at the
> template level, rather than at the optimizer level.  It would
> be more productive to work on implementations which optimize
> the checking, rather than adding mechanisms for turning it off.
>
>    What we really need are smart pointers which can have
> checking and reference counting optimized by compilers.
> Much checking and count updating can be hoisted out of loops.

That's true.  The Austria C++ smart pointer library allows to explicitly
control how reference counts are handled.  In my experience using it, a
significant number of reference counting operations can be eliminated
however I don't really know how a compiler would be able to do it
automatically.

A classic one is return values.  Using the Austria C++ at::PtrDelegate
is almost always the right thing since there is no need to increment the
reference count, however, if the returned pointer is not placed in
another smart pointer, then the reference count needs to be decremented.

Consider this example:

smart_pointer is a regular boost::shared_ptr like smart pointer.

class T;
smart_pointer<T> x;

void T::z()
{
     x = new T;
}

smart_pointer<T> f()
{
     return x; // rc gets incremented
}

void foo()
{
 // rc gets icremented and decremented
     smart_pointer<T> x = f();
}

void boo()
{
     // T::z() side effect is to decrement the rc on x
     // so temporary must hold the reference count.
     f()->z(); // z() will decrement the reference count
}


// using Austria C++ pointers

class T;
Ptr<T*> x;

void T::z()
{
     x = new T;
}

PtrDelegate<T*> f()
{
     return x; // rc gets incremented
}

void foo()
{
 // rc remains the same
         // no increment or decrements ops needed
     Ptr<T*> x = f();
}

void boo()
{
     // the temporary PtrDelegate holds a reference count.
     // so this still works
     f()->z();
}

Anyhow, the point is, I don't know how you could possibly make a
compiler smart enough to work out where it can and cannot eliminate
reference counting without giving it some help (like you can with
PtrDelegate()).

I have a new version of austria C++ coming out soon that has some
interesting features.  It will include a thread safe Ptr<> policy that
allows multiple threads to be able to read and write the pointer
simultaneously (with any unsafe accesses to the pointer trapped by
compile errors).

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: John Nagle <nagle@animats.com>
Date: 9 Oct 2005 22:00:01 GMT
Raw View
Gianni Mariani wrote:
> John Nagle wrote:
> ..
>
>>
>>    This is an attempt at premature optimization.  It
>> attemtps to solve an implementation performance problem at the
>> template level, rather than at the optimizer level.  It would
>> be more productive to work on implementations which optimize
>> the checking, rather than adding mechanisms for turning it off.
>>
>>    What we really need are smart pointers which can have
>> checking and reference counting optimized by compilers.
>> Much checking and count updating can be hoisted out of loops.
>
>
> That's true.  The Austria C++ smart pointer library allows to explicitly
> control how reference counts are handled.  In my experience using it, a
> significant number of reference counting operations can be eliminated
> however I don't really know how a compiler would be able to do it
> automatically.

     Here's how to do it.

     First, make it work for references.  You need "local references"
with an attribute, either supplied by the programmer ("auto" has been
suggested) or tagged automatically by the compiler, indicating
that the reference will not be be copied to a reference of
longer life, or converted to a pointer.

     For such references, do the reference count update when the
local reference is created.  The local reference can then be
used without reference counting.

     This is an optimization which a compiler could perform
during loop optimization, provided that the compiler can see
the potential side effects of everything in the loop.  In
inner loops, this is often the case.  The compiler could
create a temporary local reference on its own and hoist
the reference creation out of the loop.  This is just like
putting a pointer in a register for the duration of a loop.
The compiler needs some hints to tell it that it's OK to
do this, but those hints are part of the reference-counting
system, not the code that uses it.

     One can generalize this further, including application to
inter-function optimization.

    John Nagle
    Animats

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]