Parameter might be expensive to copy
This defect occurs when you pass a parameter by value instead of by reference or pointer, but the parameter is unmodified and either:
The parameter is a non-trivially copyable type. For more on non-trivially copyable types, see is_trivially_copyable.
The parameter is a trivially copyable type that is above a certain size. For
example, an object greater than 2 * sizeof(void *) is more expensive
to pass by value than by reference or pointer.
Polyspace® flags unmodified parameters that meet the preceding conditions even if the
parameters are not declared const.
Polyspace raises no defect if:
The passed by value parameter is a move-only type. For instance,
std::unique_ptr can be moved-from but cannot be copied.
The passed by value parameter is modified.
For example, no defect is raised in this code where a large trivially copyable
Buffer and a move-only unique_ptr are passed by
value.
#include<memory>
typedef struct Buffer {
unsigned char bytes[ 20 ];
} Buffer;
void func1(Buffer modified_param)
{
++modified_param.bytes[ 0 ];
}
void func2(std::unique_ptr<Buffer> move_only_param);Passing a parameter by value creates a copy of the parameter which is inefficient if the
parameter is expensive to copy. Even if your intent is to pass by reference or pointer, you
might forget the const& or const* in your function
signature and inadvertently run an inefficient version of the function.
Convert the parameter to a const pointer (const*) for C code, or to a
const reference (const&) for C++ code.
Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
| Group: Performance |
| Language: C | C++ |
| Default: Off |
Command-Line Syntax:
EXPENSIVE_PASS_BY_VALUE |
| Impact: Medium |
Expensive copy in a range-based for
loop iteration | Expensive local variable
copy | Expensive return by
value | Find defects
(-checkers)