www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Automatically using stack allocations instead of GC

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Are there any plans (or is it already happening) to make 
D-compilers automatically use stack allocations when possible in 
cases like

int foo()
{
     auto x = [1, 2]; // should be allocated on the stack
     return y = x[0] + x[1];
}

where allocations are "small enough" and cannot escape the 
current scope?

And how does/should/will this interact with ` nogc`?
Oct 23
next sibling parent Daniel Kozak <kozzi11 gmail.com> writes:
On Monday, 23 October 2017 at 09:06:21 UTC, Per Nordlöw wrote:
 Are there any plans (or is it already happening) to make 
 D-compilers automatically use stack allocations when possible 
 in cases like

 int foo()
 {
     auto x = [1, 2]; // should be allocated on the stack
     return y = x[0] + x[1];
 }

 where allocations are "small enough" and cannot escape the 
 current scope?

 And how does/should/will this interact with ` nogc`?
AFAIK does not work right now.
Oct 23
prev sibling next sibling parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On Monday, 23 October 2017 at 09:06:21 UTC, Per Nordlöw wrote:
 Are there any plans (or is it already happening) to make 
 D-compilers automatically use stack allocations when possible 
 in cases like

 int foo()
 {
     auto x = [1, 2]; // should be allocated on the stack
     return y = x[0] + x[1];
 }

 where allocations are "small enough" and cannot escape the 
 current scope?

 And how does/should/will this interact with ` nogc`?
LDC does something like that IIRC.
Oct 23
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Monday, 23 October 2017 at 10:42:44 UTC, Dmitry Olshansky 
wrote:
 On Monday, 23 October 2017 at 09:06:21 UTC, Per Nordlöw wrote:
 Are there any plans (or is it already happening) to make 
 D-compilers automatically use stack allocations when possible 
 in cases like

 int foo()
 {
     auto x = [1, 2]; // should be allocated on the stack
     return y = x[0] + x[1];
 }

 where allocations are "small enough" and cannot escape the 
 current scope?

 And how does/should/will this interact with ` nogc`?
LDC does something like that IIRC.
It does. https://github.com/ldc-developers/ldc/blob/master/gen/passes/GarbageCollect2Stack.cpp It is enabled by default at -O2 or higher (but not -Os or -Oz).
Oct 23
parent Johan Engelen <j j.nl> writes:
On Monday, 23 October 2017 at 12:49:18 UTC, Nicholas Wilson wrote:
 On Monday, 23 October 2017 at 10:42:44 UTC, Dmitry Olshansky 
 wrote:
 On Monday, 23 October 2017 at 09:06:21 UTC, Per Nordlöw wrote:
 int foo()
 {
     auto x = [1, 2]; // should be allocated on the stack
     return y = x[0] + x[1];
 }
LDC does something like that IIRC.
It does.
It does not. https://godbolt.org/g/i21t8G
 https://github.com/ldc-developers/ldc/blob/master/gen/passes/GarbageCollect2Stack.cpp
This broke somehow quite a while ago :( I haven't found time to look at it. Would be great if someone did (most important: add tests for it!!!!!). -Johan
Oct 23
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 10/23/2017 2:06 AM, Per Nordlöw wrote:
 Are there any plans (or is it already happening) to make D-compilers 
 automatically use stack allocations when possible in cases like
 
 int foo()
 {
      auto x = [1, 2]; // should be allocated on the stack
      return y = x[0] + x[1];
 }
 
 where allocations are "small enough" and cannot escape the current scope?
There are no plans at the moment, but it's a good idea that `scope` can make possible.
 And how does/should/will this interact with ` nogc`?
If it gets allocated on the stack, then it should be compatible with nogc.
Oct 23
parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Monday, 23 October 2017 at 10:48:37 UTC, Walter Bright wrote:
 There are no plans at the moment, but it's a good idea that 
 `scope` can make possible.
I'm glad your open for such automatic optimizations, Walter. Making D compilers automate these things which are cumbersome manual labour in languages such as Rust is, IMHO, the competitive way forward for D.
 And how does/should/will this interact with ` nogc`?
If it gets allocated on the stack, then it should be compatible with nogc.
Great. I believe good diagnostics (for, in this case, mismatches between allocations and qualifiers) will play a key role in this regard.
Oct 23