cppgraphqlgen icon indicating copy to clipboard operation
cppgraphqlgen copied to clipboard

Pass smart pointers by value

Open paussu opened this issue 2 years ago • 4 comments

We have gotten some code smells reported by SonarQube regarding these smart pointer rvalue references, so I thought I could maybe contribute and suggest passing these smart pointers by value instead.

Maybe there is a reason they should be rvalue references?

This is the kind of code smell we got: https://rules.sonarsource.com/cpp/tag/confusing/RSPEC-5954

paussu avatar Jul 22 '22 17:07 paussu

CLA assistant check
All CLA requirements met.

ghost avatar Jul 22 '22 17:07 ghost

The reason I favored r-value references is that we have another static analyzer rule internally which warns against passing values larger than 4 * (sizeof(void*)). The explanation is that this generates more code at the call sites to perform a copy, whereas passing by a reference puts all of the copy/move code inside the function/method you're calling.

wravery avatar Aug 02 '22 16:08 wravery

I think the threshold for that rule's trigger (4 * sizeof(void*)) is probably a heuristic. I've also heard a rule of thumb that you should pass by reference for anything greater than 2 * sizeof(void*) to maximize the number of parameters that will fit in registers instead of the stack.

wravery avatar Aug 02 '22 16:08 wravery

Right and passing by value and moving is not as optimal as passing rvalues references?

Just wondering as many example codes for passing smart pointers in constructors are using the pass by value and move method. Example

paussu avatar Aug 02 '22 21:08 paussu