More radical ideas about gc and reference counting
Michel Fortin via Digitalmars-d
digitalmars-d at puremagic.com
Wed Apr 30 18:16:29 PDT 2014
On 2014-04-30 21:51:18 +0000, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org> said:
> I'm thinking e.g. non-interlocked refcounts go like 1, 3, 5, ... and
> interlocked refcounts go like 2, 4, 6, ...
>
> Then you do an unprotected read of the refcount. If it's odd, then it's
> impossible to having originated as an interlocked one. So proceed with
> simple increment. If it's even, do an interlocked increment.
Nice idea, although I'd add a twist to support polymorphism in class
hierarchies: add a magic value (say zero) to mean "not
reference-counted". When you instantiate an object that has a
destructor, the reference counter is set to 1 or 2 depending on whether
it's shared or not. If however the object has no destructor and is not
reference counted, set the counter to the magic value.
Then have the compiler assume all objects are reference counted. At
runtime only those objects with a non-magic value as the reference
count will actually increment/decrement the counter.
Finally, let the the compiler understand some situations where objects
are guarantied to have no destructors a destructor so it can omit
automatic reference counting code in those cases. One such situation is
when you have a reference to a final class with no destructor. We could
also add a @nodestructor attribute to forbid a class and all its
descendants from having destructors.
--
Michel Fortin
michel.fortin at michelf.ca
http://michelf.ca
More information about the Digitalmars-d
mailing list