On Monday, 25 November 2019 at 13:09:44 UTC, mipri wrote:
class C {
    int x;
    this(int n) { x = n; }
}

int heap() {
    auto x = new C(2); // vgc: `new` causes a GC allocation
    return x.x;
}
/+
int example.heap():
        sub     rsp, 8
        mov     edi, OFFSET FLAT:example.C.__Class
        call    _d_newclass
        mov     DWORD PTR [rax+16], 2
        mov     eax, 2
        add     rsp, 8
        ret
+/
gdc assembly.

What about adding a new optimization pass to the compiler that infers `scope` of each class instance `x` if it (and all it's aliasings) are

- only passed to scope parameters of functions and
- doesn't escape into a global variable or via a return value

? I recall such as mechanism already exists for GC-allocated dynamic arrays which, in release builds of ldc at least, are replaced with stack-allocated arrays (if they are small enough).

Also, what happens if `C` doesn't fit on the stack? In that case the compiler should allocate `C` using a deterministic allocator via `malloc` or `mmap` and `free` or `munmap` the instance at the end of `x`'s lifetime.

Reply via email to