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

Slides:



Advertisements
Similar presentations
August 15, 2006Vladlen Timciuc, Caltech CMS Group1 ECAL H4 TB 2006 Status report.
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Endacott Society Computer Study Group1 By Jerry Niebaum.
Chapter 18 Vectors and Arrays
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Introduction to C++ Templates Speaker: Bill Chapman, C++ developer, financial company in Mid-Manhattan. Can be reached via: ' This.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Risk Management User Group Thursday, August 26, 2004.
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
New Market Structures: APEC COAL TRADE AND INVESTMENT LIBERALISATION AND FACILITATION (TILF) WORKSHOP (TILF) WORKSHOP Kuala Lumpur, Malaysia March 8, 2002.
Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
Brown Bag #3 Return of the C++. Topics  Common C++ “Gotchas”  Polymorphism  Best Practices  Useful Titbits.
© 2005, Dynamic Measurement Group1 IDEL™ Training: Administration and Scoring of “Fluidez en el Nombramiento de las Letras” (FNL) D ynamic M easurement.
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Writing Modern C++ Marc Grégoire Software Architect April 3 rd 2012.
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Writing “Exception Safe” C++ Alan Griffiths –Work: Experian Limited Senior Systems Consultant Mentoring & development OOA/OOD/C++
Procedures and Control Flow CS351 – Programming Paradigms.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
OOP Spring 2007 – Recitation 21 Object Oriented Programming Spring 2007 Recitation 2.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates CS-240 Dick Steflik & DJ. Foreman. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Intro to Generic Programming Templates and Vectors.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Unrestricted © Siemens AG 2015 All rights reserved.Smarter decisions, better products. Move semantics && rvalue references, part 2 Bert Rodiers, Software.
Concepts in C++. Templates in current C++ C++ template is typeless No language support for constrained generics Accidental errors found in instantiation.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Objects and Classes
Motivation for Generic Programming in C++
Programming with ANSI C ++
How to be generic Lecture 10
Templates.
Programming with ANSI C ++
Operator overloading Conversions friend inline
Operator overloading Conversions friend inline
Operator overloading Conversions friend inline
Class: Special Topics Copy Constructors Static members Friends this
Finally! Discussing a language!
Templates.
Chapter 2 Objects and Classes
Generic Lightweight Druntime
Constructors and Other Tools
JAVA Constructors.
Java Programming Language
Class: Special Topics 2 For classes using memory allocation
CMSC 341 C++ and OOP.
SPL – PS3 C++ Classes.
Presentation transcript:

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

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

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&

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&&

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; }

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

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

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); }

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); }

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

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)); }

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); }

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))); }

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

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

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

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

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…

…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:

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

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

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

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

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

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

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