0

I've heard it said that passing by reference or pointer can be more efficient because it avoids a copy. I'm assuming this obviously doesn't apply for small arguments, for example doubles. Sometimes I deal with vec3 and vec4s which might be about 32 bytes big. I have a habit of passing by reference because I have this idea in mind, but I'm wondering if it's actually worth doing. IT makes sense that if copies are avoided it's faster, but how big would the argument generally need to be to gain anything? Are we talking about 8 bytes, 32 bytes? Or much bigger?

I'm not sure how passing by reference or pointer actually works under the hood anyway. For example is it possible it's actually slower with a reference because the memory address is used to retrieve the information when a simple copy would mean the object is available immediately.

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • Depends on the machine architecture I'd say. On a 64 bit architecture it's likely to be more efficient to use references over 64 bits of memory used as data. – πάντα ῥεῖ Sep 27 '20 at 19:53
  • An object with only a single byte in size could be prohibitively expensive to copy, while an object of thousands of bytes in size could be very cheap. It all depends on how the copying is done, and what the objects copy-constructor does. There's really no straight answer unless you specify an explicit object and shows us its type and possible copy-constructor (if not the default). – Some programmer dude Sep 27 '20 at 19:53
  • If you want to order a meal. Will you give the delivery guy your address or your House?. –  Sep 27 '20 at 19:55
  • If `uintmax_t` or smaller, pass by value. if pointer, pass by value, If numeric (up to complex long double), pass by value. If a `struct/union`, pass by ref. – chux - Reinstate Monica Sep 27 '20 at 19:57
  • I am surprised that this question have been such down-voted: this seems a legitimate question I asked myself several times already. I am pretty sure that the answer is "it all depends", but I tend to conclude as AKL that, as a rough estimate, the threshold is above 16 bytes on a 64-bit processor. BTW, the mentioned "duplicate" question is much more general and not about performance. – prapin Sep 27 '20 at 20:25
  • 1
    Profile, profile, profile. On my machine, it seems the break even point is between 256 bytes and 512 bytes. – Eljay Sep 27 '20 at 20:36

1 Answers1

-1

Assuming that accessing a pointer and copying an int value takes same amount of processors time, and if the following test is true:

std::is_trivially_copy_constructible<type>::value == true

, then the formula for passing by reference could be the following:

sizeof(type) > 2 * sizeof(type*)

where type is type of the argument to the function.

AKL
  • 1,367
  • 7
  • 20
  • 1
    Why are you making this assumption? – Eugene Sep 27 '20 at 19:58
  • But sizeof a type does not tell anything about how complex and time consuming algorithms its copy constructror runs. – Öö Tiib Sep 27 '20 at 19:58
  • @Eugene I am not assuming, I said if that is true then this – AKL Sep 27 '20 at 19:59
  • @ÖöTiib you are right , but I assumed that type is using a default copy constructor! I will update my answer – AKL Sep 27 '20 at 20:00
  • @AKL Even a default constructors RT complexity cannot be known in advance for every type. You need to decide case by case what's more efficient (by means of performance vs stack memory consumpiton). – πάντα ῥεῖ Sep 27 '20 at 20:06
  • @πάνταῥεῖ how about types which their members also have default copy constructors? – AKL Sep 27 '20 at 20:08
  • @AKL Sure, and these member types in the members also have default constructors aso. – πάντα ῥεῖ Sep 27 '20 at 20:10
  • @πάνταῥεῖ yes that is exactly what I mean, I think they are called trivial types or something like that – AKL Sep 27 '20 at 20:11
  • @πάνταῥεῖ don't you agree that at least in those case I am right? – AKL Sep 27 '20 at 20:12
  • @AKL Let's agree we disagree that what you provided in your answer is a good rule of thumb. – πάντα ῥεῖ Sep 27 '20 at 20:15
  • @πάνταῥεῖ I updated my answer, please take a look! – AKL Sep 27 '20 at 20:18
  • This is clearly a rule of thumb, but I ended up having the same, passing notably pairs of `double` (like a geographical coordinate) by value, but bigger values by reference (on 64-bit CPU). – prapin Sep 27 '20 at 20:28