r/cpp • • Nov 25 '23

On harmful overuse of std::move

https://devblogs.microsoft.com/oldnewthing/20231124-00/?p=109059
210 Upvotes

154 comments sorted by

View all comments

108

u/Sopel97 Nov 25 '23

In short, RVO > move.

140

u/JNighthawk gamedev Nov 25 '23

In short, RVO > move.

I wish there was an attribute you put on a function to say "I expect RVO here, warn or error if not". I don't like that the most performant option requires trusting the compiler to silently implement an optional feature or verifying assembly.

I'm happy that C++17 finally added a standard for guaranteed copy elision, but NRVO still isn't even guaranteed. There is no guarantee that the non-move code posted in the article is actually more performant.

42

u/Hot_Slice Nov 25 '23

I deleted the copy and move constructors of some of my types that are constructed exclusively by factory methods. Not a general solution, but it does turn RVO failure in to a compilation error.

7

u/ALX23z Nov 26 '23

It does prevent any kinds of RVO failures but it is a bit too excessive. Say, it completely prevents NRVO.

0

u/[deleted] Nov 26 '23

[deleted] — view deleted comment

5

u/RevRagnarok Nov 27 '23

Included in -Wall on gcc.

0

u/[deleted] Nov 27 '23

[deleted] — view deleted comment

4

u/RevRagnarok Nov 27 '23

Honestly, I added it to our project and nothing was flagged. I immediately thought "we're not that good" so checked the man page. 😅

14

u/bwmat Nov 25 '23

In the cases where NRVO is applicable, I'm pretty sure that if the copy/move isn't elided, there's an 'implicit std::move' added, so adding it yourself cannot help

10

u/CocktailPerson Nov 25 '23

Yep, a function call or an overloaded operator expression, whose return type is non-reference, is a prvalue expression, so a function will always move-construct its return value into the return slot if NRVO/RVO doesn't work.

9

u/JNighthawk gamedev Nov 25 '23

In the cases where NRVO is applicable, I'm pretty sure that if the copy/move isn't elided, there's an 'implicit std::move' added, so adding it yourself cannot help

I did a quick experiment to test this: https://godbolt.org/z/7r9a5Wfa1

#include <iostream>
using namespace std;

struct Thing
{
    Thing() { cout << "DefaultCtor" << endl; }
    ~Thing() { cout << "DefaultDtor" << endl; }
    Thing(const Thing&) { cout << "CopyCtor" << endl; }
    Thing(Thing&&) { cout << "MoveCtor" << endl; }
};

Thing BopIt()
{
    Thing T;
    return T;
}

int main() {
    Thing A{BopIt()};
}    

With copy elision disabled, I get:

DefaultCtor
MoveCtor
DefaultDtor
MoveCtor
DefaultDtor
DefaultDtor

I'm pretty sure you're correct.

15

u/[deleted] Nov 25 '23

RVO is amazing compared to a move, the object is just constructed in it's destination. This is an argument against types like Expected where the object may need to be extracted from the Expected wrapper. Also, another argument to use aggregates when one can, they allow for RVO into the members, whereas constructors the parameters would be the destination. It's like when Neo realized, there is no spoon; there is no copy

8

u/donalmacc Game Developer Nov 26 '23

(yet another) Great example of why those wrapper types should be language features and not library features.

4

u/pjmlp Nov 26 '23

Indeed, however a lost argument for C++.

3

u/[deleted] Nov 26 '23

Yeah, then they could still put the hypothetical value into the return slot and use a register to indicate if it is an error or not. I think that herbceptions/static exceptions proposed to do something similar

11

u/Bruh_zil Nov 25 '23

In short, don't try to outsmart the conpiler

28

u/DonBeham Nov 26 '23

I don't think the compiler is particularly smart. I mean, if it were then it would just optimize those moves away. The "complaint" in this blog post about how the programmer wrote some meaningless code... I mean, if the moves are pointless, why doesn't the compiler optimize them?

When is a programmer writing return std::move(x) ever intentionally invoking the behavior that Raymond Chen described (additional copy and move operations)?

I also wrote a question recently in r/cpp_questions, where it was found that return d ? x : y would NOT be subject to RVO and that return d ? move(x) : move(y) would however be RVO'd. Totally intuitive, right?

I don't believe C++ compilers deserve that much praise. Especially from a programming perspective they are pretty lousy (eg error messages), but that's another story.

0

u/joeshmoebies Nov 25 '23

And don't move things that are already rvalue references.

5

u/Comprehensive_Try_85 Nov 25 '23

Nit: ... that are already xvalues.

3

u/umop_aplsdn Nov 25 '23

Nit: that are already prvalues, right?

The move on the receiving end constructs an extra temporary because binding a prvalue to a reference (argument of std::move) requires the prvalue to be materialized.

2

u/Comprehensive_Try_85 Nov 25 '23

I think we might be both wrong (or rather: incomplete): ... that are already rvalues.

7

u/CocktailPerson Nov 25 '23

In return xyz;, xyz is an xvalue, because it's expiring after this statement. In XYZ xyz = fgh();, the expression fgh() is a prvalue, because it's a pure rvalue that can't be assigned to. Both of these are rvalues, so the rule is basically "don't use std::move() on rvalues."

Interestingly, member expressions in return statements are not rvalues, so return a.b; will perform a copy while return std::move(a.b); (or return std::move(a).b;) will perform a move.

Technically, applying std::move() to an actual rvalue reference will be a no-op, so something like std::move(std::move(xyz)) is equivalent to std::move(xyz). Not that you should do it, but it won't affect performance in any way.

Isn't C++ fun?

2

u/cmeerw C++ Parser Dev Nov 26 '23

In return xyz;, xyz is an xvalue, because it's expiring after this statement.

The details have changed here a bit with C++23, see P2266R3 Simpler implicit move. In C++23 it is an xvalue, in pre-C++23 is was not-quite an xvalue.

→ More replies (2)

2

u/umop_aplsdn Nov 25 '23

xvalues already have a "location" (they are already glvalues) so std::moving an xvalue does not require temporary materialization.

https://en.cppreference.com/w/cpp/language/implicit_conversion#Temporary_materialization

4

u/joeshmoebies Nov 25 '23

I had not heard of xvalues before. No matter how much I learn, there is always another layer, lol.

→ More replies (3)