Presentation is loading. Please wait.

Presentation is loading. Please wait.

Belgian C++ User Group Impact of C++11 Move Semantics on Performance Francisco Almeida.

Similar presentations


Presentation on theme: "Belgian C++ User Group Impact of C++11 Move Semantics on Performance Francisco Almeida."— Presentation transcript:

1 Belgian C++ User Group Impact of C++11 Move Semantics on Performance Francisco Almeida

2 Move semantics and performance A little bit of background. – What are R-value references? – What is it good for? What about compiler optimizations? – Copy elision/return value optimization. Execution time comparisons (STL). – Using GCC rd of April, 2012Belgian C++ User Group2

3 L-Value References What do we mean by L-value reference? 3rd of April, 2012Belgian C++ User Group3 std::string becpp = “BeCppUG”; L-value (named object) std::string&

4 R-Value References 3rd of April, 2012Belgian C++ User Group4 std::string GetGroupName() { return std::string(“BECppUG”); } R-value (unnamed object) What do we mean by R-value reference? std::string&&

5 Construct by moving Default constructor Parameterized constructor Copy constructor Move constructor 3rd of April, 2012Belgian C++ User Group5 MyClass::MyClass(MyClass&& other) { data = other.data; }

6 std::move 3rd of April, 2012Belgian C++ User Group6 MyClass::MyClass(MyClass&& other) { data = other.data; } MyClass::MyClass(MyClass&& other) { data = std::move(other.data); } L-value R-value

7 std::move 3rd of April, 2012Belgian C++ User Group7 MyClass& MyClass::operator=(MyClass&& other) { data = other.data; return *this; } MyClass& MyClass::operator=(MyClass&& other) { data = std::move(other.data); return *this; } L-value R-value

8 std::move definition 3rd of April, 2012Belgian C++ User Group8 template inline typename std::remove_reference ::type&& move(T&& t) { return static_cast ::type&&>(t); }

9 By the way… Even if you don’t use it, the STL will! 3rd of April, 2012Belgian C++ User Group9 template inline void swap(T& a, T& b) { T tmp = a; a = b; b = tmp; } template inline void swap(T& a, T& b) { T tmp = std::move(a); a = std::move(b); b = std::move(tmp); }

10 Default move constructor The compiler will provide your class an implicit move constructor if: – No user-defined copy constructor or assignment. – No user-defined destructor. – No user-defined move assignment. Your class will also get an implicit move assignment if: – No user-defined copy constructor or assignment. – No user-defined destructor. – No user-define move constructor. Strong guarantee required: Copy constructor and destructor have no side effects. Constructors do not throw (tell the compiler noexcept ). 3rd of April, 2012Belgian C++ User Group10

11 Imperfect forwarding How to ensure that a reference type is always correctly forwarded? 3rd of April, 2012Belgian C++ User Group11 template shared_ptr create_shared(U& arg) { return shared_ptr (new T(arg)); } template shared_ptr create_shared(const U& arg) { return shared_ptr (new T(arg)); } template shared_ptr create_shared(U arg) { return shared_ptr (new T(arg)); }

12 C++11 Reference Collapsing Rules When passing…… it becomes… A& &A& A& &&A& A&& &A& A&& &&A&& 3rd of April, 2012Belgian C++ User Group12 template inline T&& forward(typename std::remove_reference ::type& t) noexcept { return static_cast (t); }

13 Perfect forwarding “One overload to forward them all” 3rd of April, 2012Belgian C++ User Group13 template shared_ptr create_shared(U&& arg) { return shared_ptr (new T(std::forward(arg))); }

14 Doesn’t the compiler do all this, anyway? 3rd of April, 2012Belgian C++ User Group14

15 Return Value Optimization 3rd of April, 2012Belgian C++ User Group15 std::string GetGroupName() { return std::string(“BECppUG”); } //... std::string name = GetGroupName(); No copying!

16 Return Value Optimization Compiler skips object copying (elides copy) – Stack frame optimization technique. – First introduced by Walter Bright, in the Zortech C++ Compiler. Compiler dependent, and not always guaranteed. 3rd of April, 2012Belgian C++ User Group16

17 I don’t need “move semantics” to move! 3rd of April, 2012Belgian C++ User Group17

18 3rd of April, 2012Belgian C++ User Group18 void MyClass::Swap(MyClass& other) { std::swap(data, other.data); } MyClass obj1; obj1.Swap(temp); Explicit swaps do get most of the job done…

19 …and return value optimization does the rest… 3rd of April, 2012Belgian C++ User Group19 MyClass& MyClass::operator=(MyClass other) { Swap(other); return *this; } …but it is not portable, nor guaranteed to always work. Copy-and-Swap idiom:

20 Optimal use of copy and move semantics 3rd of April, 2012Belgian C++ User Group20

21 But does it really make any difference? 3rd of April, 2012Belgian C++ User Group21

22 A simple example “Benchmark” for comparing move-enabled STL to move-disabled STL performance. Tested using GCC – Without -std=c++0x (C++98 rules, no move) – With -std=c++0x (C++11, use move in STL) Refer to: 3rd of April, 2012Belgian C++ User Group22

23 Howard’s STL move semantics “benchmark” Fill a std::vector with N std::set s of N randomly generated values. – We will use N = 5001 here. Sort the std::vector. Rotate the std::vector by half its size. 3rd of April, 2012Belgian C++ User Group23

24 Execution times comparison (containers passed by value) 3rd of April, 2012Belgian C++ User Group24

25 Execution times comparison (containers passed by reference) 3rd of April, 2012Belgian C++ User Group25

26 Conclusions Move semantics are another optimization tool in the C++ arsenal. Profile your code and compare approaches. – Use noexcept wherever possible. – Do not overuse move semantics, you may actually lose performance. STL is move-enabled “out of the box” – Optimizations for free 3rd of April, 2012Belgian C++ User Group26


Download ppt "Belgian C++ User Group Impact of C++11 Move Semantics on Performance Francisco Almeida."

Similar presentations


Ads by Google