The purpose of D (GC rant, long)

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Thu Oct 26 17:11:34 PDT 2006


BCS wrote:
> How much does the runtime, generated code and Phobos depend on the GC?
> Does it try to optimize out allocations or delete things it can?
> For example, does this preform one allocation or several?
> 
> char[] foo = "abc", bar = "baz";
> char[] ret = foo ~ bar ~ foo;

Only one IIRC. There's a function called _d_arraycatn in the runtime 
that accepts an element size and a variable number of arrays, allocates 
size * (total length) and starts copying. That should be the one invoked 
for a concatenation like the above.

> // ret.length = 9;
> // ret[0..3] = foo;
> // ret[3..6] = bar;
> // ret[6..9] = foo;
> 
> and would these temporaries get deleted (or maybe not even created)?
> 
> if((foo ~ bar)[4]=='g') go();
> 
> //auto __tmp = foo ~ bar
> //if(__tmp[4] == 'g') go();
> //delete __tmp;
> 
> or
> 
> //if( (foo.length>4 && foo[4]=='g') || bar[4-foo.length]=='g') go();

I don't think these get deleted[1], nor do I expect creating these is 
avoided. Could be wrong about this one though, you'd have to check the 
compiler output to be sure[2]. Well, that or wait until Walter or 
someone who has checked for you answers ;).

[1]: before the next GC cycle, that is.
[2]: Don't forget to turn on optimizations, might make a difference here.



More information about the Digitalmars-d mailing list