Reference counting with fat pointers

via Digitalmars-d digitalmars-d at puremagic.com
Sat Jul 26 03:49:24 PDT 2014


On Friday, 25 July 2014 at 21:01:00 UTC, Kagamin wrote:
> Looks like I got the core logic working.
>
> The scheme takes advantage of D type system and distinction 
> between shared and unshared data to decide on interlocked 
> reference counting at compile time. Unshared mutable data and 
> shallow shared data (like strings) are counted with the fastest 
> possible non-atomic arithmetic. Cycles should be handled by the 
> user, see an example at the end. Language interface is not 
> considered, assess if the approach itself is ok.

Interesting proposal. It seems that you want to change the 
default for pointers in D, but Walter will probably not like that 
;-). But I wonder how much language support this actually needs, 
much of it seems implementable with templates, which is probably 
much more acceptable.

Some random comments:

> Scoped: "The reference count is incremented when escaped or 
> copied to a fat pointer."

Could you explain that? A scoped pointer can by definition never 
be escaped; therefore there should be no need to pass the 
reference counter along.

> Function parameters: "Caller doesn't increment the counter when 
> passing a pointer as a function argument."

There are some corner cases here. For example when an r-value is 
moved into a function as an argument. The callee will not 
decrement the RC of its parameters, with the consequence that the 
RC never goes to 0. There might be other problems, in particular 
in the presence of optimizations.

It's probably safer just to keep the traditional behaviour of 
reference counting, without specific optimizations: Increment on 
copying, decrement on destruction. Of course this is inefficient, 
but `scope` can ameliorate that significantly. There needs to be 
language support for implicit conversion to scope for certain 
types (maybe as part of implicit conversion in general), and 
Phobos needs to use scope as much as possible. See also my 
writeup on borrowing, will add more examples there:
http://wiki.dlang.org/User:Schuetzm/scope


More information about the Digitalmars-d mailing list