Re: [OFFish] Performance difference between passing smallish structs by reference or value
Re: [OFFish] Performance difference between passing smallish structs by reference or value
- Subject: Re: [OFFish] Performance difference between passing smallish structs by reference or value
- From: Robert Goldsmith <email@hidden>
- Date: Sun, 23 May 2004 15:48:21 +0100
>
Essentially, I'm trying to get a feel for the trade off's between
>
passing by const reference (C++'s "&") and by value, for smallish
>
structs (<50 bytes).
Well, you just need to think about what 'By Reference' and 'By Value'
actually mean. Although modern cpus have many registers, it is still
best to talk worst-case and assume that any parameter passed in a
method call is passed on the stack. When you pass by reference, a 4
byte value (the address in memory of the item being passed) is pushed
onto the stack and then pulled off again inside the method. If you pass
by value, the entire parameter is copied out of memory, pushed onto the
stack then pulled back off the stack into the space created for it
inside the called method. Therefore, passing by reference only requires
a push, pull and (possibly) a store of the 4 byte address. Passing by
value requires a push, pull and store of however many bytes are in the
parameter. Once the parameter has been stored, it's address is then
loaded into a register and, from then on pass by value and pass by
reference are pretty much treated the same - except for possibly a
little bit of tidy-up code at the method return.
>
At what point does gcc start passing parameters in registers, if -O3
>
doesn't do it?
gcc will always try to pass as many by-reference parameters in
registers as possible. It uses 'colouring' to keep track of what is in
what register and which of the (more than directly accessible) shadow
registers needs to be loaded and activated when. It's pretty efficient
really - esp. with the powerpc capability to pre-fetch and pre-load
shadow copies of registers well before a method call.
>
Anyway, I thought I'd ask because even given my observations, nearly
>
every performance-critical sample of code I've seen still passes by
>
value (e.g. OpenGL, as a prime example).
Ah, now here you have to be careful. By Reference only works when the
calling and called methods are both in the same virtual address space.
This works fine for normal calls within one application but does not
work when you are talking about more advanced stuff. Open GL code
usually ends up handing the parameters over to the graphics card -
which has it's own memory address space and which can't 'see' the
application address space, so they have to be passed by value. Same
with Distributed Objects - esp. across multiple machines. Generally,
the rule is to pass by value only when the value is less than 4 bytes
(although the gain here is very debatable) or when you are passing data
across a memory space boundary (between two apps or to the graphics
card or another distinct memory block).
Hope this helps :)
Robert
---
GnuPG public key:
http://www.Far-Blue.co.uk
[demime 0.98b removed an attachment of type application/pgp-signature which had a name of PGP.sig]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.