November 28, 2019 Re: Leak-detection of references to scoped class instances | ||||
---|---|---|---|---|
| ||||
Posted in reply to mipri | 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.
|
November 29, 2019 Re: Leak-detection of references to scoped class instances | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | On Thursday, 28 November 2019 at 21:49:09 UTC, Per
>
> Also, what happens if `C` doesn't fit on the stack?
This is OS specific I think. For example on Linux at the end of the stack there is a guard page and when you hit it the process will segfault.
|
Copyright © 1999-2021 by the D Language Foundation