www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Encouraging memory efficiency

reply "Tero" <a7710680 drdrb.net> writes:
Just watched Don's DConf 2014 talk where he said D has to be 
ruthless about
memory inefficiency. Here's one thing that I think could help 
avoid unnecessary garbage: built-in syntax for this:

import core.stdc.stdlib : alloca;
ubyte[] buffer = (cast(ubyte*) alloca(bufsize)) [0 .. bufsize];

Often bufsize is not known at compile-time but it won't change 
after the buffer
allocation. So there's no reason to create garbage other than the 
*inconvenience*
of using alloca. Allocating in the stack seems ideal so I'd 
encourage that by a
clean syntax. I keep missing this feature.
Jul 17 2014
next sibling parent reply "Kapps" <opantm2+spam gmail.com> writes:
On Thursday, 17 July 2014 at 16:28:00 UTC, Tero wrote:
 Just watched Don's DConf 2014 talk where he said D has to be 
 ruthless about
 memory inefficiency. Here's one thing that I think could help 
 avoid unnecessary garbage: built-in syntax for this:

 import core.stdc.stdlib : alloca;
 ubyte[] buffer = (cast(ubyte*) alloca(bufsize)) [0 .. bufsize];

 Often bufsize is not known at compile-time but it won't change 
 after the buffer
 allocation. So there's no reason to create garbage other than 
 the *inconvenience*
 of using alloca. Allocating in the stack seems ideal so I'd 
 encourage that by a
 clean syntax. I keep missing this feature.
In theory there would probably be an allocator that uses alloca when Andrei's std.allocator makes it in. Using alloca is actually rather problematic in D right now though (with DMD at least)... https://issues.dlang.org/show_bug.cgi?id=12820 Also, there used to be a built in syntax, 'scope foo = new Foo()' that would allocate on the stack, but that's deprecated now (and seemed to segfault when I tried it).
Jul 17 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Kapps:

 In theory there would probably be an allocator that uses alloca
 when Andrei's std.allocator makes it in.
How is it going to work?
 Also, there used to be a built in syntax, 'scope foo = new 
 Foo()' that would allocate on the stack, but that's deprecated 
 now
Perhaps once the "scope" (and memory lifetimes) is fleshed out, it will be un-deprecated :-) Bye, bearophile
Jul 17 2014
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2014-07-17 18:35, Kapps wrote:

 Also, there used to be a built in syntax, 'scope foo = new Foo()' that
 would allocate on the stack, but that's deprecated now (and seemed to
 segfault when I tried it).
It is still available and no sign of deprecation (except everyone saying that). -- /Jacob Carlborg
Jul 17 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Thursday, 17 July 2014 at 19:41:57 UTC, Jacob Carlborg wrote:
 On 2014-07-17 18:35, Kapps wrote:

 Also, there used to be a built in syntax, 'scope foo = new 
 Foo()' that
 would allocate on the stack, but that's deprecated now (and 
 seemed to
 segfault when I tried it).
It is still available and no sign of deprecation (except everyone saying that).
"Discouraged and unmaintained" is probably a better description.
Jul 17 2014
parent reply Jacob Carlborg <doob me.com> writes:
On 17/07/14 22:18, Dicebot wrote:

 "Discouraged and unmaintained" is probably a better description.
How does it need maintaining? -- /Jacob Carlborg
Jul 17 2014
parent "Dicebot" <public dicebot.lv> writes:
On Friday, 18 July 2014 at 06:46:16 UTC, Jacob Carlborg wrote:
 On 17/07/14 22:18, Dicebot wrote:

 "Discouraged and unmaintained" is probably a better 
 description.
How does it need maintaining?
I mean that if you create bug report or enhancement request for scope classes it is quite unlikely anyone will pay attention to it.
Jul 18 2014
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Tero:

 Allocating in the stack seems ideal so I'd encourage that by a 
 clean syntax. I keep missing
 this feature.
See: https://issues.dlang.org/show_bug.cgi?id=9832 http://en.cppreference.com/w/cpp/container/dynarray Bye, bearophile
Jul 17 2014
prev sibling next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 17 July 2014 at 16:28:00 UTC, Tero wrote:
 Just watched Don's DConf 2014 talk where he said D has to be 
 ruthless about
 memory inefficiency. Here's one thing that I think could help 
 avoid unnecessary garbage: built-in syntax for this:

 import core.stdc.stdlib : alloca;
 ubyte[] buffer = (cast(ubyte*) alloca(bufsize)) [0 .. bufsize];

 Often bufsize is not known at compile-time but it won't change 
 after the buffer
 allocation. So there's no reason to create garbage other than 
 the *inconvenience*
 of using alloca. Allocating in the stack seems ideal so I'd 
 encourage that by a
 clean syntax. I keep missing this feature.
When talking about allocations: The stack is just a fixed size chunk of pre-allocated memory with complicated rules and caveats for use. It's only advantage as a place for general purpose allocation is that it's "hot" memory, i.e. it's normally already in cache due to it's locality. The fact that it is 1) pre-allocated and 2) eagerly de-allocates on exiting a function can be easily implemented in any allocation scheme.
Jul 18 2014
prev sibling next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 07/17/2014 06:27 PM, Tero wrote:
 Just watched Don's DConf 2014 talk where he said D has to be ruthless about
 memory inefficiency. Here's one thing that I think could help avoid
 unnecessary garbage: built-in syntax for this:

 import core.stdc.stdlib : alloca;
 ubyte[] buffer = (cast(ubyte*) alloca(bufsize)) [0 .. bufsize];

 Often bufsize is not known at compile-time but it won't change after the
 buffer
 allocation. So there's no reason to create garbage other than the
 *inconvenience*
 of using alloca. Allocating in the stack seems ideal so I'd encourage
 that by a
 clean syntax. I keep missing this feature.
Well, there is always this hack: import core.stdc.stdlib : alloca; auto createBuffer(T,alias size)(T[] buf=(cast(T*)alloca(size))[0..size]){ return buf; } void main(){ auto bufsize=5; auto buffer=createBuffer!(ubyte,bufsize); import std.stdio; writeln(buffer); }
Jul 18 2014
parent Nick Treleaven <ntrel-public yahoo.co.uk> writes:
On 19/07/2014 01:31, Timon Gehr wrote:
 auto createBuffer(T,alias size)(T[]
 buf=(cast(T*)alloca(size))[0..size]){ return buf; }
alloca(T.sizeof * size)
Jul 21 2014
prev sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 7/17/2014 9:27 AM, Tero wrote:
 Just watched Don's DConf 2014 talk where he said D has to be ruthless about
 memory inefficiency. Here's one thing that I think could help avoid unnecessary
 garbage: built-in syntax for this:

 import core.stdc.stdlib : alloca;
 ubyte[] buffer = (cast(ubyte*) alloca(bufsize)) [0 .. bufsize];

 Often bufsize is not known at compile-time but it won't change after the buffer
 allocation. So there's no reason to create garbage other than the
*inconvenience*
 of using alloca. Allocating in the stack seems ideal so I'd encourage that by a
 clean syntax. I keep missing this feature.
You might be interested in std.internal.scopebuffer.
Jul 21 2014