Boost logo

Glas :

Re: [glas] Reference counting?

From: Neal Becker (ndbecker2_at_[hidden])
Date: 2007-10-12 09:53:16

On Friday 12 October 2007, Karl Meerbergen wrote:
> Hi Bert,
> This is the idea of a boost::shared_ptr.
> It is clear that this might help in some cases for improving reliability
> of code.
> If I understand Neal well, the situation could be the following:
> you create a matrix.
> Later, you create an object that is a proxy to a row of this matrix.
> If you have reference counting, the programmer can destroy the matrix
> before the row proxy. Without the reference count, the proxy should be
> destroyed before the matrix.
> At this stage, this mechanism is indeed not provided. In practice, it
> would imply adding a boost::shared_ptr in all containers. This is
> possible, but could it not create undesired overhead?
> One of the great things of generic programming is that we can provide
> containers and proxies that support reference counting in addition to
> containers and proxies that do not use reference counting :-)

Ref counting has a lot of benefits. For example, you can return an array by
value, without a huge performance impact. Also, a view of an array can be
treated just the same as an array. This simplifies code.

Ref counting can be bolted-on using shared_ptr, but that has greater cost
(shared_ptr has to allocate on heap a count). You want the ref count to be
included in the memory object to reduce the cost. Also, without ref
counting, I believe you need to distinguish between destroying an array
(which owns the memory) and a view (which does not). This code becomes
redundant if you have ref counting built in.

I find the separation of the memory component and the 'view of memory' (array)
that blitz++ uses to be useful. I haven't looked at the glas design yet.