digitalmars.D - Automatically using stack allocations instead of GC
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (11/11) Oct 23 2017 Are there any plans (or is it already happening) to make
- Daniel Kozak (2/13) Oct 23 2017 AFAIK does not work right now.
- Dmitry Olshansky (2/13) Oct 23 2017 LDC does something like that IIRC.
- Nicholas Wilson (5/21) Oct 23 2017 It does.
- Johan Engelen (7/20) Oct 23 2017 It does not.
- Walter Bright (4/15) Oct 23 2017 There are no plans at the moment, but it's a good idea that `scope` can ...
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (8/13) Oct 23 2017 I'm glad your open for such automatic optimizations, Walter.
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 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.
Oct 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.
Oct 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: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).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 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:It does not. https://godbolt.org/g/i21t8GOn Monday, 23 October 2017 at 09:06:21 UTC, Per Nordlöw wrote:It does.int foo() { auto x = [1, 2]; // should be allocated on the stack return y = x[0] + x[1]; }LDC does something like that IIRC.https://github.com/ldc-developers/ldc/blob/master/gen/passes/GarbageCollect2Stack.cppThis 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 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.
Oct 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.Great. I believe good diagnostics (for, in this case, mismatches between allocations and qualifiers) will play a key role in this regard.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 2017