主要内容

性能缺陷

影响 C/C++ 代码性能的缺陷

这些缺陷检测 C/C++ 代码中可能会导致出现性能瓶颈的问题。检测的问题包括:

  • 无意中导致系统执行复制而非原本的移动操作的问题

  • 低效或不必要的临时变量创建

  • 使用的函数可能有更高效的替代方案

Polyspace 结果

全部展开

移动运算可能抛出异常Throwing move operations might result in STL containers using the corresponding copy operations
常量参数值可能导致不必要的数据副本Const parameter values may prevent a move operation resulting in a more performance-intensive copy operation
常量返回值可能导致不必要的数据副本Const return values may prevent a move operation resulting in a more performance-intensive copy operation
常量 rvalue 引用参数可能导致不必要的数据副本The const-ness of an rvalue reference prevents intended move operation (自 R2021a 起)
常量 std::move 输入可能导致更高成本的对象副本Const std::move input cannot be moved and results in more expensive copy operation
空析构函数可能导致不必要的数据副本User-declared empty destructors prevent autogeneration of move constructors and move assignment operators
高成本返回 const 对象The return statement of a function copies an objects instead of moving it because the returned object is declared as a const (自 R2022a 起)
移动运算使用复制Non-const rvalue reference parameter of a function or operator is copied instead of moved (自 R2021b 起)
对不可移动类型调用 std::movestd::move used on a class type with no move constructor or move assignment operator
变量的最后一次使用成本高An expensive-to-copy local variable is copied in its final use instead of being moved (自 R2025a 起)
基于范围的 for 循环迭代中的高成本复制The loop variable of a range-based for loop is copied from the range elements instead of being referenced resulting in inefficient code
高成本局部变量复制Local variable is created by copy from a const reference and not modified later (自 R2021a 起)
成员初始化的成本很高You assign values to class members in the constructor body instead of constructing members using a member initializer list (自 R2023b 起)
高成本的按值传递Parameter might be expensive to copy
高成本的按值返回Functions return large output by value instead of by reference or pointer
使用 std::any_cast 的成本很高An object is cast by-value using std::any_cast when casting by-reference is more efficient (自 R2023b 起)
从常量字符串进行高成本的 std::string 或 std::regex 构造A constant std::string or std::regex object is constructed from constant data, resulting in inefficient code
缺失 constexpr 设定符constexpr specifier can be used on variable or function for compile-time evaluation
在重新分配前执行不必要的构造Instead of directly constructing objects with value, you construct objects and then immediately assign values to objects (自 R2023a 起)
高成本的未使用对象Expensive local object is constructed but not used (自 R2024a 起)
高成本 std::function 的类型定义Definition of std::function type uses pass-by-value semantics for arguments that are expensive to copy (自 R2024a 起)
高成本动态强制转换Expensive dynamic_cast is used instead of more efficient static_cast or const_cast (自 R2021b 起)
高成本使用标准算法而未采用已有更高效的方法Functions from the algorithm library are misused with inappropriate inputs, resulting in inefficient code (自 R2021b 起)
高成本使用非成员 std::string operator+() 而非简单的 append 方法The non-member std::string operator+() function is called when the append (or +=) method would have been more efficient
高成本使用 std::string 方法而非更高效的重载An std::string method is called with a string literal of known length, instead of a single quoted character (自 R2021a 起)
高成本使用 std::string 处理空字符串字面值Use of std::string with empty string literal can be replaced by less expensive calls to std::basic_string member functions (自 R2021a 起)
高成本使用 C 标准库中的字符串函数String functions from the C standard library are used inefficiently (自 R2022a 起)
高成本使用 substr() 缩短 std::stringThe method std::string::substr() is called to shorten an std::string object (自 R2022a 起)
低效使用 sprintfThe function sprintf, snprintf, or swprintf copies strings instead of the more efficient strcpy, strncopy, or wcsncpy (自 R2021b 起)
字符串长度计算效率低下String length calculated by using string length functions on return from std::basic_string::c_str() instead of using std::basic_string::length()
std::endl 可能导致不必要的刷新std::endl is used instead of the more efficient \n
使用 new 或 make_unique,而非更高效的 make_sharedUsing new or make_unique to initialize or reset shared_ptr results in additional memory allocation (自 R2021a 起)
不必要地使用 std::string::c_str() 或等效的字符串方法Instead of a std::string object, a string operation uses the C-string obtained from std::string functions including std::string::c_str, std::string::data(), std::string::at(), or std::string::operator[], resulting in inefficient code
高成本使用容器的 count 方法The function member count() of a container is used for checking if a key is present, leading to inefficient code (自 R2021b 起)
高成本使用容器的 insertion 方法One of the insertion methods of a container is used to insert a temporary object (自 R2022a 起)
高成本使用容器的 size 方法A container's size() method is used for checking emptiness instead of its empty() method, which is more efficient (自 R2022b 起)
高成本使用 map 的方括号运算符插入值或赋值The bracket operator of std::map or std::unordered_map is used for inserting or assigning a value in the container instead of the insert_or_assign() method, which is more efficient (自 R2022b 起)
缺失对容器保留方法的调用A fixed number of items are added to a container without calling the reserve() method of the container beforehand, resulting in inefficient code (自 R2022b 起)
高成本使用 map 而不是使用 setThe key for a map is member of the object being inserted, resulting in redundant map key (自 R2024b 起)
高成本逻辑运算A logical operation requires the evaluation of both operands because of their order, resulting in inefficient code (自 R2021a 起)
不必要 std::move 导致高成本返回An unnecessary call to std::move in the return statement hinders return value optimization, resulting in inefficient code (自 R2022b 起)
高成本循环分配Fixed sized memory is allocated or deallocated in a loop (自 R2022a 起)
高成本后增量运算Object is post-incremented when pre-incrementing is faster (自 R2021b 起)
低效使用 for 循环Range-based for loop can perform equivalent iteration more efficiently (自 R2022a 起)
不必要的填充Members of a struct are padded to fulfill alignment requirement when rearranging the members to fulfill this requirement saves memory (自 R2021b 起)
不必要的特殊成员函数实现Implementing special member functions hinders code optimization when implicit versions are equivalent (自 R2023a 起)
对参数的不必要引用Parameter is passed to function as const reference when passing by value is more efficient (自 R2024a 起)

主题