ref arguments

Steven Schveighoffer schveiguy at yahoo.com
Mon Sep 21 14:54:32 PDT 2009


On Mon, 21 Sep 2009 16:29:11 -0400, Bill Baxter <wbaxter at gmail.com> wrote:

> On Mon, Sep 21, 2009 at 12:53 PM, Jeremie Pelletier <jeremiep at gmail.com>  
> wrote:
>>>> Right now the compiler makes a temporary copy of referenced  
>>>> parameters on
>>>> the stack, calls the function with a pointer to the stack copy, and  
>>>> once the
>>>> function returns copies the modified temporary back to its original
>>>> location. This is quite considerable overhead.
>>>
>>> Are you sure this is true?  I don't have a d2 compiler right now, but  
>>> that
>>> sounds like a *huge* step in the wrong direction.  D1 does not do this
>>> (tested dmd 1.046).
>>
>> Yeah I started a thread about that a few months ago in digitalmars.D,  
>> its
>> something that's on the bugzilla I believe.
>
> I think this is the only bugzilla bug about speed of reference  
> parameters:
> http://d.puremagic.com/issues/show_bug.cgi?id=2008
>
> It does not mention the copying issue in D2 you talk about, only lack
> of inlining in D1 and D2.  But I think the asm code posted there may
> be doing that copying.  Not a big ASM guru though.

I don't think it's doing that.  I think it's pushing the pointers onto the  
stack (as expected).

I'm also not an asm guru :)

Note that some of the assembler instructions at the beginning of the  
function are to initialize the variables a and b to 0.

Here is my test code, and relevant asm output (dmd 1.046, no inline or  
release), note the void initialization to prevent the initializing of the  
structure before passing:


struct S
{
     int x;
     int y;
     int z;
}

void foo(ref S s)
{
     s.x = 5;
     s.y = 6;
     s.z = 7;
}

void main()
{
     S s = void;
     foo(s);
}

_D6testme3fooFKS6testme1SZv:
		push	EBP
		mov	EBP,ESP
		mov	dword ptr [EAX],5
		mov	dword ptr 4[EAX],6
		mov	dword ptr 8[EAX],7
		pop	EBP
		ret
		nop
		nop
		nop
.text._D6testme3fooFKS6testme1SZv	ends

...

_Dmain:
		push	EBP
		mov	EBP,ESP
		sub	ESP,0Ch
		lea	EAX,-0Ch[EBP] ; I think this puts the pointer into the EAX register  
for the call
		call	near ptr _D6testme3fooFKS6testme1SZv at PC32
		xor	EAX,EAX
		leave
		ret
.text._Dmain	ends


-Steve


More information about the Digitalmars-d-learn mailing list