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);
}

Reply via email to