at what size should one call by reference..

Started by
14 comments, last by Lukas Boersma 18 years ago
It depends on how the parameter is used, for example:

You'd normally pass a container by reference - but if you need a local copy in the function anyway, passing by reference is just a waste.

It also depends on the copying cost - it's possible to have a class that holds an object by pointer, then deep copies the pointed to object when it's copied. Such an object would be the size of the machines registers, but would be expensive to copy.



Advertisement
For me, I rarely pass POD types by reference. I just pass those by value. If it's a struct or class, and it's not going to be changed inside the function, then I'll pass by const reference; however, if the class or struct is to be altered, then I'd pass by reference. Rarely do I pass by pointers anymore; but, that's due to the set of conventions I set for myself. Since I started using boost, if I ever need to pass a pointer around, I'd use one of the smart pointer classes you get with it. I also toss my long-term pointers (excluding dynamic arrays) into smart pointers to take care of the off chance that I may end up with invalid pointers floating around.
It also depends on platform. For the XB360 microsoft recommends passing their native vectors and such by values because their registers are huge and it's faster than passing a reference, cache-wise. But yea agree with everything said above about PC. _goat summed it up pretty well, with the addition of const to the reference unless you need to modify it in the function, and avoid passing by * in C++ unless theres some reason the argument should be allowed to be null for some special behavior.
Many compilers also have special-case calling conventions for arguments that are equal to 2*register size which makes passing the by value efficient. This question falls firmly into the realm of "it depends".

IMHO reasonable defaults for pass-by-value are "type" for primitives and "const type &" for non-primitives. In relatively rare cases where you want pass-by-value but are going to party on the parameter in the function anyway then you don't really lose anything by using "type" for non-primitives.

Oh, and calling by reference doesn't do anything to the heap. References are really just pointers in disguise and they will happily point to stack objects as well as heap objects.
-Mike
for a PC, simple vec3,vec2,vec4 and float3x3, float4x4, float2x2 very simple classes, it looks like the rule of tum for it is pass by const reference (which is what I usually do), but vec3 is just 3 floats... we are talking small stuff, and float4x4 is 16 floats (64 bytes) which for 64-bit CPU's fits into 8 registers... (maybe that is too many) oh well.... it is very easy to change one's .hpp files from pass by value to pass by const reference for these situations, but still leaves me with the question which one? [possibly it does not matter a great deal since I only do maybe 100-200 of these vec/vec, matrix/matrix, vec/matrix calls per frame of drawing anyways, but still... jsut curious from others experience.)


Close this Gamedev account, I have outgrown Gamedev.
Well, I'd just call those functions a few 1000 times and simply take the time they need... then you know exactly wich of them is the faster one

This topic is closed to new replies.

Advertisement