8

So far as I understand you should not pass simple types by reference in c++ because it does not improve perfomance, it's even bad for performance(?). At least thats what I managed to gather from the net.

But I can't find out the reason why it's bad for performance, is it because it's quicker for c++ to just create a new simple type than it it is too look up variable or what is it?

Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
  • 1
    Even without any "performance" considerations, reference parameters change the calling semantics... –  May 26 '12 at 23:07
  • I would expect this to be a compiler optimization if passing by constant reference (the compiler would convert this to a pass by value). – Thomas Matthews May 27 '12 at 02:27

4 Answers4

8

If you create a reference, it's:

pointer to memory location -> memory location

If you use a value, it's:

memory location

Since a value has to be copied either way (the reference or the value), passing by reference does not improve performance; one extra lookup has to be made. So it theoretically "worsens" performance, but not by any amount you'll ever notice.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • That is a misleading answer. _"Since a value has to be copied either way"_. Passing by ref does not result copying the value, instead it uses pointer operations on the same object. So passing a `double` means creating a copy of the `double` (if it is an lvalue) and using that copy in the function. Passing a reference means you pass the pointer to the function and perform dereferencing, whenever the value is used, but not putting the var onto the stack. If you have a func with a ref arg that calls another one with a value arg, then only the second one will alloc the var on the stack. – Adam Hunyadi Jun 20 '17 at 08:00
  • @AdamHunyadi: Passing by reference results in a pointer being copied (down to optimizations). That’s what I meant by “a value”. (You can tell that by reading the parenthetical right after the part you quoted.) – Ry- Jun 20 '17 at 09:25
4

It's a good question and the fact that you're asking it shows that you're paying attention to your code. However, the good news is that in this particular case, there's an easy way out.

This excellent article by Dave Abrahms answers all your questions and then some: Want Speed? Pass by Value.

Honestly, it does not do the link justice to summarize it, it's a real must-read. However, to make a long story short, your compiler is smart enough to do it correctly and if you try doing it manually, you may prevent your compiler from doing certain optimizations.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • 1
    The link is dead in the meanwhile: From archive: https://web.archive.org/web/20140205194657/http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/ – darkmattercoder Dec 03 '18 at 12:43
  • 1
    However, there is a referencing post, that points out some potential downsides: https://juanchopanzacpp.wordpress.com/2014/05/11/want-speed-dont-always-pass-by-value/ – darkmattercoder Dec 03 '18 at 12:44
0

Internally references are treated at pointers, which need to be dereferenced each time you access them. That obviously requires additional operations.

Wormbo
  • 4,978
  • 2
  • 21
  • 41
  • References are not pointers. Nor should you think of references as pointers (it just makes thinking about them harder). – Martin York May 27 '12 at 02:00
  • I can agree for references created in a local scope, but what about passing stuff to methods as reference? If you don't pass by value (read: put a copy on the stack), your only other choice it to pass a pointer to the referenced object. You have to pass something, via stack or registers, so it must be either the entire value or a pointer to it. And since references are clearly not the entire values, they must be pointers. I prefer to think of them in the "it simply works" way of C# and Java, but internally they work like auto-dereferenced pointers. – Wormbo May 27 '12 at 08:06
  • You are mapping language constructs (pointers/references) onto implementation details now. This is even less useful. – Martin York May 27 '12 at 16:30
  • 2
    The question was why simply types should not be passed by reference. Implementation details aside there is no reason not to pass stuff by reference. The reason not to do it (apart from not allowing a function to modify the original variable passed in) is slowdown due to implementation detail. – Wormbo May 27 '12 at 16:36
  • Then write you r answer in terms of implementations details. Not your incorrect view of how references work. – Martin York May 27 '12 at 16:38
0

For example, in 32 bit mode, int size is 4 bytes and int * pointer size is 4 bytes. Passing 4 bytes int directly is quicker, than passing 4 byte pointer and then loading int by that pointer.

Ruben
  • 2,488
  • 1
  • 18
  • 22
  • @Loki Astari: Parameter in some configurations always is loaded from memory (should it be pointer or value), but if it is pointer then there will be another load from memory, which is not in case where value itself is passed as parameter. – Ruben May 27 '12 at 14:05