Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 17, 2014 Encouraging memory efficiency | ||||
---|---|---|---|---|
| ||||
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. |
July 17, 2014 Re: Encouraging memory efficiency | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tero | 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). |
July 17, 2014 Re: Encouraging memory efficiency | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tero | 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 |
July 17, 2014 Re: Encouraging memory efficiency | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kapps | 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 |
July 17, 2014 Re: Encouraging memory efficiency | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kapps | 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 |
July 17, 2014 Re: Encouraging memory efficiency | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | 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.
|
July 18, 2014 Re: Encouraging memory efficiency | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On 17/07/14 22:18, Dicebot wrote: > "Discouraged and unmaintained" is probably a better description. How does it need maintaining? -- /Jacob Carlborg |
July 18, 2014 Re: Encouraging memory efficiency | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tero | 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.
|
July 18, 2014 Re: Encouraging memory efficiency | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | 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.
|
July 19, 2014 Re: Encouraging memory efficiency | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tero | 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); } |
Copyright © 1999-2021 by the D Language Foundation