The analyzer has detected a function that takes a parameter by reference to a constant object, even though passing it by value would be more efficient.
Here ate two examples for 64-bit systems.
In the first case, the function takes objects of the const std::string_view & type:
uint32_t foo_reference(const std::string_view &name) noexcept
{
return static_cast<uint32_t>(8 + name.size()) + name[0];
}
Assembly code:
foo_reference(std::basic_string_view<char, std::char_traits<char> > const&):
mov eax, dword ptr [rdi] // <= (1)
mov rcx, qword ptr [rdi + 8] // <= (2)
movsx ecx, byte ptr [rcx]
add eax, ecx
add eax, 8
ret
There, dereferencing occurs every time data is read from an object of the const std::string_view & type. These are the mov eax, dword ptr [rdi] (1) and mov rcx, qword ptr [rdi + 8] (2) instructions.
In the second case, the function takes objects of the std:string_view type:
uint32_t foo_value(std::string_view name) noexcept
{
return static_cast<uint32_t>(8 + name.size()) + name[0];
}
Assembly code:
foo_value(std::basic_string_view<char, std::char_traits<char> >):
movsx eax, byte ptr [rsi]
add eax, edi
add eax, 8
ret
The compiler generated less code for the second example. This happens because the entire object fits in the processor's registers, so the compiler does not need to use addressing to access it.
Note N1. For the opposite cases, when passing an object by reference to a constant is more efficient than passing it by value, the V813 diagnostic rule exists.
Note N2. Users can specify the size of the structures that are most effectively passed by copy.
For example, to have the analyzer issue warnings only for structs that are 8 bytes or less, add the following comment to the source code or to the configuration file (.pvsconfig):
//V_CHEAP_TO_COPY_THRESHOLD=8
The default value at which the analyzer determines it is more efficient to pass an object by copy is equal to the size of two pointers on the target platform. For example, on most 64-bit platforms, a pointer is 8 bytes, so the default value there is 16 bytes.
You can also specify this setting for a specific platform only. Use settings sections in the configuration file (.pvsconfig) to do this.
For example, we recommend setting the value to 8 bytes for Windows x64, as this matches the Microsoft x64 calling convention. To do this, add the following to the configuration file (.pvsconfig):
//V_SECTION_BEGIN
//V_WHEN_TARGET_OS: Windows
//V_WHEN_TARGET_ARCH: x64
//V_CHEAP_TO_COPY_THRESHOLD=8
//V_SECTION_END
Note N3. In some cases, passing objects by reference to a constant may be more efficient than passing them by value when a class has a non-trivial copy constructor.
For example, passing std::shared_ptr objects by value creates an additional resource owner and increments the atomic reference counter. Depending on the program's intended behavior, this additional ownership may be undesirable.
Real-world projects can also involve more complex cases. To suppress such warnings, use one of the options described in the documentation.
Parameter-passing rules depend on the target architecture and ABI. Many common 64-bit ABIs can efficiently pass small objects up to 16 bytes through registers. So, the V835 diagnostic rule considers objects up to 16 bytes cheap-to-copy by default.
However, this value is not universal. For example, under the Microsoft x64 calling convention, 16-byte objects are passed indirectly, whereas on Windows ARM64, objects of this size may be passed via registers.
Take a look at the "System V Application Binary Interface AMD64 Architecture Processor Supplement", which defines function calling conventions for Unix-like systems. Section 3.2.3 describes parameter passing. Each parameter gets its own class. If a parameter belongs to the MEMORY class, the calling convention passes it through the stack. Otherwise, the parameter goes through processor registers, as in the example above.
According to subsection 5 (C), an object larger than 16 bytes gets the MEMORY class. The exception is aggregate types up to 64 bytes whose first data member belongs to the SSE class and all remaining ones belong to SSEUP. So, larger objects go on the function call stack, and accessing them also requires addressing.
Here are two more examples for 64-bit systems.
In the third one, a 16-byte object is passed by copy:
struct SixteenBytes
{
int64_t firstHalf; // 8-byte
int64_t secondHalf; // 8-byte
}; // 16-bytes
uint32_t foo_16(SixteenBytes obj) noexcept
{
return obj.firstHalf + obj.secondHalf;
}
Assembly code:
foo_16(SixteenBytes): # @foo_16(SixteenBytes)
lea eax, [rsi + rdi]
ret
The compiler generated efficient code by placing the struct in two 64-bit registers.
In the fourth example, a 24-byte struct is passed by copy:
struct MoreThanSixteenBytes
{
int64_t firstHalf; // 8-byte
int64_t secondHalf; // 8-byte
int32_t yetAnotherStuff; // 4-byte
}; // 24-bytes
uint32_t foo_more_than_16(MoreThanSixteenBytes obj) noexcept
{
return obj.firstHalf + obj.secondHalf + obj.yetAnotherStuff;
}
Assembly code:
foo_more_than_16(MoreThanSixteenBytes):
mov eax, dword ptr [rsp + 16]
add eax, dword ptr [rsp + 8]
add eax, dword ptr [rsp + 24]
ret
According to the System V ABI, the compiler has to place the struct on the stack. As a result, the code accesses it indirectly via an address calculated using the rsp register. In this case, the analyzer issues the V813 warning.