Unnecessary reference to parameter
Parameter is passed to function as const
reference when passing
by value is more efficient
Since R2024a
Description
This defect occurs when you pass a parameter to a function as a const
reference but the parameter is cheap to copy. Polyspace® considers a parameter to be cheap to copy if both of these conditions are true:
The parameter is a trivially copyable type. For more on trivially copyable types, see is_trivially_copyable.
The parameter size is less than or equal to
2 * sizeof(void *)
.
Polyspace reports this defect for non-const
reference parameters if
the parameters are not modified in the function. Taking the address of the passed reference
is not considered a modification of the passed reference.
Risk
When you pass a parameter by reference, reading the parameter within the function body requires a dereferencing operation. For cheap-to-copy objects, dereferencing the object in the function scope is more expensive than copying the object into the function scope. Passing parameters by reference can also result in two or more references to the same memory location. In such a case, because the interaction between these references can be difficult to track, the compiler optimizes the code using more conservative assumptions. Passing cheap-to-copy objects by reference can make your code inefficient by requiring expensive dereferencing operations and by hindering compiler optimization.
Fix
To fix this defect, pass cheap-to-copy parameters by value.
Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
Examples
Result Information
Group: Performance |
Language: C++ |
Default: Off |
Command-Line Syntax:
EXPENSIVE_PASS_BY_REFERENCE
|
Impact: Low |
Version History
Introduced in R2024a
See Also
Find defects
(-checkers)
| Expensive pass by
value
| Expensive return by
value
| Expensive copy in a range-based
for loop iteration
| Expensive local variable
copy
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)