C++0x r-value references and move semantics


Uploaded by LucasFranchesco on 31.05.2011

Transcript:
Hi, my name is Lukáš Doležal and I'm going to talk about r-value references and related
move semantics, which are new principles in the new C++ standard C++0x.
Did you know that although C++ is considered as the fastest programming language today,
performance of programs can be increased by up to 70% only by adding r-value references
to the language?
Yes, up to 70% increase in performance.
And how is it possible? That is the subject of my presentation.
So, first of all, I'm going to look at motivation why have r-value references been created.
Then, I want to briefly show you what it is. After that, I'll be talking about how to use
it in an appropriate way. And finally, I'd like to analyse how it can
increase performance of programs.
Now, I'd like to show you why  have r-value references been added into the new C++ standard.
Let's have a look at this. This code just takes two strings returned from some function
and concatenate them into the variable "a". It is sample of very common practice.
So what is interesting here?
Let me show you this diagram:
This diagram describes what is actually done by this code, when C++ compiles it.
You can see there 6 arrows.
Each arrow represents one copying of data.
It means, that for concatenating 2 strings and putting the result into a variable, Your
program need to perform 6 copies of these strings.
I believe You can imagine that copying 1GB string 6 times can last very very long.
Is it possible to reduce the number of copying the data?
I'd like you to look at these 3 temporary objects.
They are created by copying result of a function and they are used only to be copied into another
object.
So, what will happen when they will not be there?
The answer is simple. Nothing will happen.
So then You can ask, why the temporary object are there.
Let's have a look at this code sample. It is just a sample of assigning values to variables.
Using this code, I'm going to show you, why there were temporary object in the last slide.
Please focus on the last two lines.
On the first line, the content of the variable "a" is copied into the "b". This is assumed
behavior, because we want to use the variable "a" with the same content even after changing
the content of the variable "b". 
Now, I'd like you to look at the second line.
There is exactly the same situation as in the first line, excepts that on the right
side of the expression is a function instead of variable.
And here's the problem. The "equal sign" cannot distinguish between different types of the
right side, so it always needs a variable on the right side and it always do a copy
of a data even when it is unnecessary as in this case.
This indistinguishability is why C++ has to create a temporary object from a result of
a function.
And this indistinguishability is also the main motivation for adding new grammar to
the C++ language which are - r-value references.
This brings me to my next point which is description of what r-value references are.
Briefly, the r-value references are the way how to deal with the problem I have showed
you. How to distinguish between a variable and a function on the right side of an expression.
The idea is to introduce a new symbol to the language - a double ampersand - which represents
a r-value reference. And with it, to change behavior of how functions return their results.
With r-value references, every function returning a non-reference type is automatically retyped
to the r-value reference type.
This concept enables you to write different behaviors of a function depending on whether
it is processing the variable or  r-value reference.
As you can see on this slide, there are two similar declarations of assignment operator
for the string type.
In the first one, the operator knows that assigned content is from a variable, so it
has to copy that.
In the second, it knows that assigned value is from a result of a function, due to the
already mentioned concept of r-value references. 
So the data can be moved instead of copied.
This principle is called move semantics and is particular for using r-value references.
This brings me seamlessly to my last point, which is how to use r-value references.
Using of r-value references can be described by 3 simple rules.
First rule, you should follow, is don't write double ampersand to return type of a function.
As I mentioned before, it is done automatically by C++.
Second rule is that when you have a function in which you have copied content of an argument,
write also an overloaded version of the function with r-value reference. Then you can move
the content of a variable instead of copying it.
As I said before, this is called move semantics and it is why r-value references have been
created.
The last, third, rule is similar to the previous one. But I separated it to make it clear how
to use it when working with classes.
When you are writing a class, which can be copied through copy constructor and assignment
operator, write also the constructor and assignment operator with move semantics.
This is very important to achieve benefits of using r-value references.
Let's now move on to description of performance increase by using r-value references.
Let me go back to my first diagram. As I explained before, there is done too much unnecessary
copying of a data.
And now have a look at this diagram.
Almost all occurrences of doing a copy disappeared only by using r-value references!
Just 2 of 6 are still there, but they are necessary for making the concatenation.
And this brings me to the end of my presentation.
To recapitulate, we have discussed where is the problem with indistinguishability of the
right side of expressions. Then I have showed you how r-value references solved that problem
and how to use them.
And finally we have briefly discussed why r-value references can increase performance
of your programs.
Thank you very much for your attention.
If you have any question, feel free to write it in the comment  section below.