pure static
TheFlyingFiddle
kurtyan at student.chalmers.se
Tue Jan 7 03:12:39 PST 2014
On Tuesday, 7 January 2014 at 00:54:10 UTC, bearophile wrote:
> Meta:
>
>> Why not just return arr.dup instead? You're returning a slice
>> of a stack-allocated array, so of course you shouldn't write
>> code like this.
>
> In certain critical code paths heap allocations are evil
> (perhaps even if your generational GC has a stack-like nursery,
> that currently the D GC doesn't have) :-) And you also want to
> minimize copies and initializations.
>
> Bye,
> bearophile
Wouldn't it be better to simply allocate on the stack directly in
the calling function? Or if that is not a possibility use a
region allocator or even a ScopeStack allocator? It's basically
just a pointer bump so it should be fast.
So it would translate to something like this:
int[] foo(Allocator)(ref Allocator allocator)
{
auto arr = allocator.allocate!(int[])(5);
foreach(i, ref elem; arr)
elem = i;
return arr;
}
More information about the Digitalmars-d
mailing list