Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V813. Decreased performance. The...
menu mobile close menu
Analyzing projects
Additional information
toggle menu Contents

V813. Decreased performance. The argument should probably be rendered as a constant pointer/reference.

Apr 29 2013

An argument that is a struct or class is passed to the function by value. After checking the function body, the analyzer has determined that the argument isn't modified. To optimize the code, this argument can be passed as a const reference instead.

This can speed up the program because the function call copies only the address, not the entire class object. The optimization is especially noticeable when the class contains a large amount of data.

The example:

void foo(Point p)
{
  float x = p.x;
  float y = p.y;
  float z = p.z;
  float k = p.k;
  float l = p.l;

  // the p argument is not used further on
}

To fix the code, just change the function signature:

void foo(const Point &p)
{
  ....
}

Note N1. For the opposite situations, where passing by value is more efficient, the V835 diagnostic rule exists.

Note N2. Users can specify the size of the structures that are most effectively passed by copying.

For example, to have the analyzer issue warnings only for structures larger than 32 bytes, add the following comment to the source code or to the configuration file (.pvsconfig):

//V_CHEAP_TO_COPY_THRESHOLD=32

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. The analyzer cannot always reliably determine how an argument is modified within a function. To suppress such warnings, use one of the options described in the documentation.