bearophile wrote:
Walter Bright:

Java does do some escape analysis to try and allocate heap objects on the stack instead, but I don't know how effective this is, and even that won't help if you try to embed a value aggregate into a class:

struct S { int x, y, z; }

class C
{
     S v;   // in Java this would require a second heap allocation
}

I have discussed with LDC devs an improvement of that idea (it was discussed in 
this newsgroup too):

class C1 { int x, y, z; }

class C2 {
    scope C1 c;
}

This idea raises few problems, but I think they can be solved.
You can special-case the management of such objects scope-allocated inside 
other objects. Or you can just add an invisible field to that C2 class, the 
reference to c.

Bye,
bearophile

I would love to see such a feature added in D, would make object composition much more convenient and faster.

You'd just need the compiler to force a call into C1's constructor within C2's and generate a call to C1's destructor in the epilog of C2's. I can't see how implementing idea that could be any harder, other than the parser related support.

No need to store a reference within the object, that would still require another heap allocation killing the need for scope in the first place.

Reply via email to