[Dlang-study] [lifetime] Initial thoughts on lifetime management

Andrei Alexandrescu andrei at erdani.com
Wed Oct 28 10:21:46 PDT 2015


On 10/28/2015 12:24 PM, Marc Schütz wrote:
> Globals are just a special case, though. Rewriting your example without
> globals:
>
> void bar() {
>      C c = new C();
>      foo(c, c);
> }
>
> int foo(ref C c, scope C d) {
>      c = new C();    // c's old instance gets deleted
>      return d.i;        // oops! d is invalid
> }

Yah, the thing is direct passing is easily under the control of the 
compiler. With some made-up notation, the compiler could do something 
like this:

void bar() {
     C c = new C();
     [[auto __t = c; __t.refs += 2;]]
     foo(c, c);
     [[__t.refs -= 2;]]
}

> RC elision may only be done if the call doesn't create new mutable
> aliases to the owner.

Agreed. There are two complementary techniques that we want to look into:

a) RC elision, as we're already discussing
b) RC fusing, i.e. coalescing several incs/decs into one (a la ARC 
autorelease pools)

One major outcome I hope from this study group is to leverage the talent 
and knowledge of this group to devise a robust analysis that achieves 
(a) and (b).


Andrei


More information about the Dlang-study mailing list