On 4/23/2011 9:48 AM, Andrei Alexandrescu wrote:
On 4/22/11 5:21 PM, Iain Buclaw wrote:
== Quote from bearophile (bearophileh...@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

Right. This is exactly the kind of thing TempAlloc was meant to solve. (Actually, to give credit, the initial rough idea for TempAlloc was proposed by Andrei, though I fleshed out the details and implemented it.) With TempAlloc, you have a segmented stack (i.e. one that allocates a new segment instead of crashing the program if it's full) that is independent of the call stack (so you can return TempAlloc-allocated memory from functions).

BTW, since when does the ternary operator work with functions, as opposed to variables?

Reply via email to