CppCoreGuidelines says that it is faster to pass small objects (up to two or three words) by value than by reference because of some overhead required for accessing from function. Can you explain me, what exactly is the overhead in this case?
-
2Highly related: http://stackoverflow.com/questions/26387569/rule-of-thumb-for-when-passing-by-value-is-faster-than-passing-by-const-referenc – NathanOliver Jan 06 '17 at 13:34
-
2I'm voting to close this question because it's unclear what you are asking, seeing as how the exact link you are giving contains the exact answer to the question: *"What is "cheap to copy" depends on the machine architecture, but two or three words (doubles, pointers, references) are usually best passed by value.*" If you don't understand, for example, what words are, or why the machine architecture affects this rule, then you should rephrase the question. – Christian Hackl Jan 06 '17 at 13:39
2 Answers
The document also gives you the explanation - you introduce additional indirection. At the very least, you need to dereference a pointer you wouldn't need otherwise. It also explains which approaches are fast and which are slow (and why).
This doesn't mean you necessarily care - unless you're in the hot part of your code, the difference doesn't really matter.

- 62,244
- 7
- 97
- 116
-
2Note that as long as your code is const-correct, and as long as you have some amount of optimization enabled (which you should if you care at all about performance,) the compiler will almost certainly optimize away things like this. In practice, the real overhead is the time wasted trying to "optimize" the little things that the compiler is already optimizing. – 0x5453 Jan 06 '17 at 14:14
The overhead is that such references involve indirection, typically implemented behind the scenes using a pointer. As such, you have a dereference operation, the weight of which does not stack up against any noticeable benefit for such a small amount of data.
The page you link to literally explains this:
When copying is cheap, nothing beats the simplicity and safety of copying, and for small objects (up to two or three words) it is also faster than passing by reference because it does not require an extra indirection to access from the function.

- 378,754
- 76
- 643
- 1,055
-
I have seen that, but expected to see more argumentative answer (assembler instructions, counting bytes, etc). – logumanov Jan 06 '17 at 13:43
-
2@logumanov: I don't really understand what more argumentation you need, and we don't need to go into such low-level implementation details to know that indirection has a cost, and you don't take on a cost when you gain nothing in return. – Lightness Races in Orbit Jan 06 '17 at 13:44
-
1@logumanov: As I said elsewhere, the question was just too vague. You literally posted a link to the explanation itself, and your question came down to: "I don't understand this." Question about technical details are of course welcome on Stack Overflow, but you need to actually *ask* them with the same attention to detail! :) – Christian Hackl Jan 06 '17 at 13:45
-
@ChristianHackl: I've thought that word 'exactly' reflects my intentions. – logumanov Jan 06 '17 at 13:50
-
1@logumanov "Exactly" isn't nearly specific enough. If you want an explanation of the entire memory model of x86 and C, and the way computers work, you need to get a book, not ask on Stack Overflow. SO is for *specific* questions. If you have an idea what "exactly" means specifically, ask that. "Indirection" is the best answer we can give to your question other than a 500 page explanation of what "exactly" happens on various architectures and compilers and configurations. – Luaan Jan 06 '17 at 14:48