2

Recently I received a feedback on a issue with the following warning from a colleague, who uses Coverity, a static analysis tool.

Passing the value of a large parameter (PASS_BY_VALUE)
pass_by_value: Passing parameter parameter_name of type class_name (size 184 bytes) by value, which exceeds the low threshold of 128 Passing parameter

This led me to wonder about how the size of the parameter will affect the quality of the application, and when will it be a major concern if the size of the parameter get out of hand?

And what is a good rule of thumb to keep it in check?

Terry Kim
  • 23
  • 2
  • 1
    There can be good reason to pass by value, but for larger objects using a const reference is generally a better idea. There's the cost of copying the object as well as space on the stack for it. – Retired Ninja Dec 17 '21 at 03:04
  • 4
    From the warning message, it appears Coverity considers 128 bytes to be the rule-of-thumb threshold for pass-by-value. – Jeremy Friesner Dec 17 '21 at 03:09
  • Depends on the platform where the tipping point is for size of parameters. Not sure if Coverity is configurable, but on my platform it's 256-512 bytes. 128 bytes would be squawking sooner than I'd be happy with -- but even those (and larger) are worth scrutinizing. (I use Coverity on my project, but I'm not the admin for it.) – Eljay Dec 17 '21 at 03:30
  • No general rule of thumb, because the only consistently true answer is "it depends". This link may be relevant https://stackoverflow.com/questions/40185665/performance-cost-of-passing-by-value-vs-by-reference-or-by-pointer/40185996 – Peter Dec 17 '21 at 03:59
  • 99% of the time, you can and should pass objects by const reference instead of by value. Avoids the overhead of the copy and accomplishes the same thing. That is prefer `void SomeFunction(const Foo& f)` over `void SomeFunction(Foo f)` – selbie Dec 17 '21 at 04:00
  • Thanks, guys! I would think I'm still pretty noob with C++. These comments help! – Terry Kim Dec 17 '21 at 10:58

1 Answers1

4
Pass by value Pass by reference Advantage
cost of passing the argument copy object copy pointer by value, if sizeof(T) <= sizeof(T*),
by ref, if sizeof(T) > sizeof(T*)
object access direct indirect by value
can alias no
the optimizer can do more access optimizations
yes
optimizer is restricted in access optimization
by value

For object smaller or equal in size to the size of a pointer it's a no brainer: pass by value has no disadvantages compared to pass by ref, only advantages.

As you consider bigger objects the cost of copying will eventually outweigh the disadvantages of pass by ref. Where that line is it's difficult to tell. And that line differs on different architectures and calling conventions. For your environment Coverity draws the line at 128 bytes, which I am sure is arrived at by smart people with experience and from doing relevant tests. You can use that limit, you can use another one if you want or you can profile your application to see what applies to your case.

bolov
  • 72,283
  • 15
  • 145
  • 224