My Reference Safety System (DIP???)
via Digitalmars-d
digitalmars-d at puremagic.com
Mon Mar 2 12:36:52 PST 2015
On Monday, 2 March 2015 at 20:04:49 UTC, deadalnix wrote:
> On Monday, 2 March 2015 at 13:30:39 UTC, Zach the Mystic wrote:
>> On Monday, 2 March 2015 at 08:59:11 UTC, deadalnix wrote:
>>> On Monday, 2 March 2015 at 00:37:05 UTC, Zach the Mystic
>>> wrote:
>>>>> I'm sure many inc/dec can still be removed.
>>>>
>>>> Do you agree or disagree with what I said? I can't tell.
>>>
>>> Yes, but I think this is overly conservative.
>>
>> I'm arguing a rather liberal position: that only in a very
>> exceptional case do you need to protect a variable for the
>> duration of a function. For the most part, it's not necessary.
>> What am I conserving?
>
> I let the night go over that one. Here is what I think is the
> best road forward :
> - triggering postblit and/or ref count bump/decrease is
> prohibited on borrowed.
> - Acquiring and releasing ownership does.
>
> Now that we have this, let's get back to the exemple :
> class C {
> C c;
>
> // Make ti refconted somehow, doesn't matter. Andrei's
> proposal for instance.
> }
>
> void boom() {
> C c = new C();
> c.c = new C();
>
> foo(c, c.c);
> }
>
> void foo(ref C c1, ref C c2) {
> // Here is where things get different. c1 is borrowed, so
> you can't
> // do c1.c = null before acquiring c1.c beforehand. That
> means the
> // compiler needs to get a local copy of c1.c, bump the
> refcount
> // to get ownership before executing c1.c = null and
> decrease
> // the refcount. The ownership expire when the function
> returns
> // so c2 is free when foo returns.
> c1.c = null;
> // c2 is dead.
> }
>
> The definition is a bit wonky ATM and most likely needs to be
> refined, but I think this is the way forward with that issue.
> It allow elision of a lot of ref increase/decrease by the
> compiler, which is very important to get refcounting works fast.
Interesting approach. I will have to think about that. But I
think it does not really work. Your example hides the fact that
there are actually two types involved (or can be): an RC wrapper,
and the actual class. foo() would need to take at least `c1` as
the wrapper type `RC!C`, not `C` itself, otherwise it couldn't
copy it. But that defeats the purpose of borrowing, that it
neutralizes the actual memory management strategy; foo() should
know whether `c1` is reference counted or not.
More information about the Digitalmars-d
mailing list