性能大杀器:c++中的copyelision

程序员咋不秃头 2024-05-29 01:35:50

在上一篇文章性能大杀器:std::move 和 std::forward中,我们简单的介绍了下移动语义,今天聊聊编译器的一个常见优化拷贝消除(copy elision)。

move和copy elision是一种常见的编译器优化技术,旨在避免不必要的临时对象的复制和拷贝,对于那种占用资源比较多的对象来说,这种优化无疑会很大程度上提升性能。

且看一个例子,如下:

#include <iostream>struct Obj { Obj() { std::cout << "Default ctor" << std::endl; } Obj(const Obj& r) { std::cout << "Copy ctor" << std::endl; } int x_ = 0;};Obj CreateObj1() { return Obj();}Obj CreateObj2() { Obj temp; temp.x_ = 42; return temp;}int main() { Obj o1(CreateObj1()); Obj o2(CreateObj2()); return 0;}

编译并运行上述代码,输出:

Default ctorDefault ctor

PS:本文中所使用的编译器及版本为gcc 11.4.0,如果未做显式说明,在编译过程中都加上-std=c++11选项。

好了,仍然是上面的代码,如果编译选项变成-std=c++11 -fno-elide-constructors,输出试试,看看会是什么结果~~

emm,在本地尝试编译并运行了下:

Default ctorCopy ctorCopy ctorDefault ctorCopy ctorCopy ctor

与最开始的输出相比,多了很多,现在我们着手分析下原因,以Obj o1(CreateObj1());为例:

•调用CreateObj1()函数,创建一个临时对象并返回,此时会输出Default ctor•将上述的需要返回的临时对象以拷贝方式赋值给函数返回值,此时会输出Copy ctor•函数返回值作为obj1的拷贝对象,此时会输出Copy ctor

接着分析下Obj o2(CreateObj2());:

•CreateObj2()创建一个临时变量temp,此时会输出Default ctor•修改临时变量temp的成员变量x_的值为2•temp以拷贝方式赋值给函数返回值,此时会输出Copy ctor•函数返回值作为obj2的拷贝对象,此时会输出Copy ctor

对前面的输出做个简单总结,如下:

Default ctor // 在CreateObj1中以Obj()方式创建临时变量T1Copy ctor // T1以复制拷贝的方式赋值给CreateObj1()函数返回值,此处假设为T2Copy ctor // 通过调用拷贝构造函数,将T2值赋值给o1Default ctor // 创建临时变量tempCopy ctor // temp以复制拷贝的方式赋值给CreateObj1()函数返回值,此处假设为temp2Copy ctor // 通过调用拷贝构造函数,将temp2值赋值给o2

在上一节中,我们提到过,可以通过使用移动构造的方式来避免拷贝,为了测试该功能,尝试在Obj类中新增一个移动构造函数:

#include <iostream>struct Obj { Obj() { std::cout << "Default ctor" << std::endl; } Obj(const Obj& r) { std::cout << "Copy ctor" << std::endl; } Obj(const Obj&& r) { // 移动构造函数 std::cout << "Move ctor" << std::endl; } int x_ = 0;};Obj CreateObj1() { return Obj();}Obj CreateObj2() { Obj temp; temp.x_ = 42; return temp;}int main() { Obj o1(CreateObj1()); Obj o2(CreateObj2()); return 0;}

输出如下(编译选项为-std=c++11 -fno-elide-constructors):

Default ctorMove ctorMove ctorDefault ctorMove ctorMove ctor

看了上述输出,不禁奇怪,为什么在CreateObj2()函数中,创建的temp明明是一个左值,此处却调用的是移动构造即当做右值使用呢?,我们不妨看看标准对此处的解释:

in a return statement in a function with a return type, when the expression is the name of a non-volatile automatic object (other than a function parameter or a variable introduced by the exception-declaration of a handler (14.4)) with the same type (ignoring cv-qualification) as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function call’s return object

意思是当返回语句中的表达式是一个非volatile的命名对象,其类型与函数的返回类型相同时,编译器可以优化掉拷贝或移动操作,直接将自动对象构造到函数调用的返回对象中。

这意味着,当函数返回一个自动对象时,编译器可以优化掉不必要的拷贝或移动操作,直接将自动对象构造到函数调用的返回对象中,以提高效率。这种优化在 C++ 标准中被明确规定,以支持更高效的代码生成。

标准的这一规定,使得原本不支持拷贝的对象,作为函数返回值时,也成了可能。

众所周知,std::unique_ptr<>不支持拷贝操作,即:

std::unique_ptr<int> p1 = std::make_unique<int>(1);std::unique_ptr<int> p2 = p1;

上述代码将编译失败,错误提示如下:

error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]' std::unique_ptr<int> p2 = p1;note: declared here unique_ptr(const unique_ptr&) = delete;

那么,如果将其作为函数返回值呢?

std::unique_ptr<int> CreateUnique() { auto ptr = std::make_unique<int>(0); return ptr;}int main() { CreateUnique(); return 0;}

编译正确,进一步证明了我们前面的说法。

好了,如果我们在编译选项中去掉-fno-elide-constructors,那么输出如下:

Default ctorDefault ctor

通过这个输出,可以看出,编译器忽略了拷贝构造函数的调用,而是直接构造o1和o2对象,这种方式在性能上有了很大的提升,编译器对o1和o2的这种优化方式称为RVO和NRVO。

现在,我们仔细回想下前面的示例代码,在编译的时候,都加上了-std=c++11这个选项,这是因为笔者的gcc11.4默认情况下是用的c++17,而c++17是能够保证RVO优化的,单独对NRVO则不能保证。

如果使用g++ test.cc -o test编译并运行代码,输出:

Default ctorDefault ctor

以上~~

1 阅读:186

程序员咋不秃头

简介:感谢大家的关注