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.