ColdHands
ColdHands

Reputation: 21

C++ std::move() performance wise with lvalues

I've just finished learning about lvalues, rvalues, move constructor and move operator. I can see the added value of using move constructor with rvalues performance and usability wise. But i can't see the added value of using move operator with move constructor with lvalues performance wise, sure there is a added value usability wise, but can't we achieve the same functionality for lvalues using some other technologies like pointers for example. so my question is: what is the added value of using move operator with move constructor with lvalues performance wise. Thanks for reading my question.

example:

class string{
public:
char* data;

string(){
    data = nullptr;
}

string(const char* p)
{
    size_t size = std::strlen(p) + 1;
    data = new char[size];
    std::memcpy(data, p, size);
}

 ~string()
{
    delete[] data;
}

string(const string& that)
{
    size_t size = std::strlen(that.data) + 1;
    data = new char[size];
    std::memcpy(data, that.data, size);
}

string(string&& that)
{
    data = that.data;
    that.data = nullptr;
}

string movee(string &that){
    data = that.data;
    that.data = nullptr;
}};

what is the difference performence wise:

string s1("test");
string s2(std::move(s1));

string s1("test");
string s2 = string();
s2.movee(s1);

In the case of rvalues, the compiler spares us the time and memory taken to reform a new object and assign the rvalue to it, and then using that new object to change the values in the moved-to object, thus increasing performance. But in the case of a lvalue object, the move operator is not increasing performance, it is increasing usability and readability of course, but it is not increasing the performance as if it was a rvalue, am I wrong?

Upvotes: 2

Views: 394

Answers (1)

Asteroids With Wings
Asteroids With Wings

Reputation: 17454

You could achieve the same functionality using indirection with pointers. Indeed, we used to, and even now that is exactly what is happening inside your move constructors/assignment operators!

Yes, most of the benefit when you std::move a variable is in the cleanliness of the code. But this is not pointless. Stealing resources with move constructors/assignment operators allows us to do so neatly, and quickly, without hacks or any performance penalties. Your code will be more maintainable and easier to rationalise about. You will be much less tempted to add an extra layer of indirection, dynamically allocating things that didn't need to be dynamically allocated, just to get code that's in any way understandable, because now there won't be any benefit in doing so.

But! Don't forget that your standard containers are doing this for you behind the scenes, e.g. when std::vector resizes. That's very valuable. If we could only move from temporaries, that would not be possible and we'd have a massive wasted opportunity on our hands.

Doing it with temporaries isn't really any different; it's just that it's done for you without having to type std::move.

Upvotes: 0

Related Questions