Performance improvements for D / DMD compiler.

Bill Baxter dnewsgroup at billbaxter.com
Fri Jan 19 21:34:59 PST 2007


Andrei Alexandrescu (See Website For Email) wrote:
> Christian Kamm wrote:
>>> What's on your wish-list (let's leave the GC and other library 
>>> functionality out of this for now <g>)?
>>
>> I'd like to see unneccessary copies being optimized away, especially 
>> of large structs passed to functions. The workaround
>>
>> // use inout to pass s by reference, it is not actually modified
>> void foo(inout LargeStruct s)
>>
>> is just abusing inout. In my opinion it should be an in parameter and 
>> D should detect whether a copy is neccessary or not.
>>
>> It's not done already, is it?
> 
> No, and for a good reason:
> 
> foo(LargeStruct s1, LargeStruct s2)
> {
>   ...
> }
> ...
> LargeStruct s;
> foo(s, s);
> 
> Nobody would enjoy hidden aliasing of s1 and s2.
> 
> 
> Andrei

I'm not really sure why that would be a problem, as long as the compiler 
has really determined that s1 and s2 aren't being modified by the 
function.  Maybe you can elaborate?  If it's effectively the same as a 
const reference, how can the aliasing make a big difference?

But anyway, I don't think it's possible to change way arguments are 
passed on what the function body does with them.  Say all you have is a 
.di file with this signature in it:
   void foo(LargeStruct s1, LargeStruct s2);

How is the compiler going to know how to push the parameters on the 
stack to call foo?

I think it may be possible, however, to establish a rule like "all 
struct parameters larger than a 'real' are actually passed by 
reference".  Then it would be up to the compiler to make copies of any 
modified arguments within the body of the function.

So the code generated for something like

void foo(LargeStruct s1) {
    s1.x = 10;
    ...use s1...
}

would look more like:

void foo(inout LargeStruct s1) {
    LargeStruct s1tmp = s1;
    s1tmp.x = 10;
    ...use s1tmp...
}

I guess you could call that "parameter copy-on-write".

--bb




More information about the Digitalmars-d mailing list