0

According to "Optimizing software in C++" by Agner Fog (2018-08-18), page 50, if any of these conditions is not met then it is usually faster to transfer a pointer or reference to the object.

  1. the object is so small that it fits into a single register
  2. the object has no copy constructor and no destructor
  3. the object has no virtual member
  4. the object does not use runtime type identification (RTTI)

The reasoning behind the first 2 conditions is fairly obvious. The 3rd condition is due the vptr that is added to the object, which would make it "too large" to pass-by-value efficiently. Is that correct?

Can anybody please explain the 4th condition?

Xlv
  • 53
  • 1
  • 6
  • No idea what author means, but I'd take their advice with a grain of salt - for example, with AMD ABI, even the object that doesn't fit into a single register could be effectively passed in registers - it would just use more than one. It is still better than indirection via pointer/reference. Example: https://gcc.godbolt.org/z/l2Zg8_ – SergeyA Jun 24 '19 at 18:36
  • RTTI means that type information is included, so sizeof(object) > sizeof(register). – Michael Chourdakis Jun 24 '19 at 18:40
  • You mean 4th condition? RTTI requires additional pointer – Severin Pappadeux Jun 24 '19 at 18:40
  • 3
    Passing an object with virtual methods by value causes it to be **sliced** into just the declared type of the argument, which is rarely what you want. It has nothing to do with size. Slicing also makes RTTI useless. Example: https://stackoverflow.com/a/25453490/9952196 – Shawn Jun 24 '19 at 18:51
  • @SergeyA, that is very interesting. Btw, when I change the code into pass-by-pointer, the compiler tries to copy that object into the frame of the callee. I am not sure why is that. -O0 doesn't change that behavior, either. https://gcc.godbolt.org/z/nvKJd9 – Xlv Jun 24 '19 at 18:56
  • 1
    I've not read Agner Fog's book, but for optimization I strongly urge profiling for compiler optimized code (at least for the parts of the code which are performance critical), rather than guessing and second-guessing what the compiler's optimizer provides and what one's changes will do for performance. – Eljay Jun 24 '19 at 18:57
  • @Shawn, but this advice is concerned with **performance**. I understand how pass-by-value can slice down an object. – Xlv Jun 24 '19 at 18:58
  • @Shawn not necessarily. Slicing will only occur if the object is passed by value and the target type doesn't match dynamic type of the object. If it matches, no slicing will happen. – SergeyA Jun 24 '19 at 19:01

0 Answers0