DIP-74 Reference Cycles

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 9 14:59:19 PDT 2015


On Thursday, 9 July 2015 at 16:49:53 UTC, Brad Anderson wrote:
> On Thursday, 9 July 2015 at 08:58:22 UTC, deadalnix wrote:
>> The part where refcount is done can be done via library (and 
>> should IMO).
>
> I think the nice thing about the compiler aware approach is 
> that the compiler could forgo incs/decs when it knows the 
> reference doesn't escape. I'm not sure how much this would 
> actually aid performance in practice though.

There are one case where it is actually useful: when refcounting 
shared object.

For thread local object, the compiler already have the 
increment/decrement pair and can optimize accordingly. It can't 
when the refcount is shared (it always is in many languages like 
C++ or ObjC) because another thread could have done arbitrary 
thing to the refcount in the meantime. In that case it is useful, 
but generally, refcounting shared object have loosy perfs because 
of contention on the cache line containing the refcount.

The other case where refcounting is bad is when it interract with 
exceptions. In which case, the language improvement we are 
talking about here do not help much, and this is why exception 
are not recoverable in ObjC and absent in swift. A option to make 
them fast again would be to not decrement the refcount on 
unwinding, and leak, delegating the cleanup to the GC.


More information about the Digitalmars-d mailing list