Re: memory alignment vs passing by value

Dan Thompson <[email protected]> Wed, 01 Dec 2004 15:33:43 -0800
Newsgroups gmane.games.devel.windows
Message-ID <[email protected]>
I'm not an expert by any stretch of the imagination, but here's my 
thoughts. (*actual* experts encouraged to pipe up, so I know where I'm 
being stupid)

>I don't know about other platforms/compilers, but with MSVC, passing a 4
>component floating point vector by value is far more efficient than passing
>it as a reference (could anyone explain why this is the case btw?).
>  
>
Huh. That doesn't seem to make sense. Passing by value means you'll be 
calling whatever copy constructor you have, and dumping that onto the 
stack (if I remember this stuff right. Who knows if I am). However a 4 
component float vector should be something like 16 bytes - and depending 
on your system the reference is 4 or 8 bytes. So the difference isn't a 
whole lot. My money is on the gains from the smaller parameter is 
getting lost in chasing the pointer down however many times you use it - 
or you are doing something like.

void MyFunc(vector4* pMyVec)
{
    vector4 holder = *pMyVec;
    // avoid copying onto the stack in the call, only to copy onto the 
stack in the body.
    // for a grand total of (sizeof(vector4*) + sizeof(vector4)) copied 
so far, less optimizations.
}

Of course, the best way to find out is to take a look at the generated 
asm for each, and see what is really going on.
If this is being called a lot, make sure you aren't busting cache 
lines... If you are calling by reference and the data is in the cache, 
then there really shouldn't be any problem with the pointer version as I 
understand it (provided you don't ask for local copies like in the 
example). Post your function if you can... its kinda hard to figure it 
out with what you say.

Not sure what you are going for with the aligning stuff. It was my 
understanding the compiler will align anything you need for speed (had 
this bite me with a struct a couple times).

-Dan

>One more thing that can help performance is correct alignment in memory.
>Problem is, when I my vectors are aligned, then I cannot pass by value
>anymore, I'm forced to use reference.
>
>These requirements knock each other out, so what can I do? :)
>
>The interesting thing is that I can still create local variables on the
>stack, and they will be correctly aligned, but it cannot align the passed
>parameters? Why?
>
>
>Thanks,
>
>
>Andras
>
>
>
>
>-------------------------------------------------------
>SF email is sponsored by - The IT Product Guide
>Read honest & candid reviews on hundreds of IT Products from real users.
>Discover which products truly live up to the hype. Start reading now. 
>http://productguide.itmanagersjournal.com/
>_______________________________________________
>Gamedevlists-windows mailing list
>[email protected]
>https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows
>Archives:
>http://sourceforge.net/mailarchive/forum.php?forum_id=555
>
>
>  
>


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/
_______________________________________________
Gamedevlists-windows mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_id=555