Thread overview
Automatically using stack allocations instead of GC
Oct 23, 2017
Per Nordlöw
Oct 23, 2017
Daniel Kozak
Oct 23, 2017
Dmitry Olshansky
Oct 23, 2017
Nicholas Wilson
Oct 23, 2017
Johan Engelen
Oct 23, 2017
Walter Bright
Oct 23, 2017
Per Nordlöw
October 23, 2017
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`?
October 23, 2017
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.
October 23, 2017
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.


October 23, 2017
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.
October 23, 2017
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.
October 23, 2017
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).
October 23, 2017
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