Linus with some good observations on garbage collection

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Apr 23 06:48:50 PDT 2011


On 4/22/11 5:21 PM, Iain Buclaw wrote:
> == Quote from bearophile (bearophileHUGS at lycos.com)'s article
>> Timon Gehr:
>>> But, as pointed out by Linus, the prime performance problem is _not_ the GC, but
>>> the mindset that comes with it. Most programmers that "grew up" in a managed
>>> environment tend to use very many "new" keywords all over their code, instead of
>>> allocating large chunks of memory at once. (Java/C#/etc encourage you to do this.)
>> In C99 (and Ada) you avoid the allocation of some dynamic arrays with new thanks
> to variable length arrays.
>
> Variable length arrays are just sugary syntax for a call to alloca.

Well in fairness they can be a fair amount more elaborate. The trouble 
with alloca is that it's impossible to compose with. So in general you 
need to write something like:

bool onStack = smallEnough(length * sizeof(T));
auto a = (cast(T*) (onStack ? alloca : malloc)(length * sizeof(T)));
scope(exit) if (!onStack) free(a.ptr);
initialize(enforce(a));
scope(exit) clear(a);

This block is difficult to factor out because of alloca.


Andrei


More information about the Digitalmars-d mailing list