Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
RVO and NRVO

RVO and NRVO

17 Sep 2021

RVO (Return Value Optimization) is a compiler optimization. In some cases, it allows not to create a local object that is used as a return value.

Instead, the returned object is constructed in place of the function call. This eliminates the unnecessary move/copy constructor call.

Look at the example:

std::vector<int> GetVector()
{
  return std::vector<int>(1'000'000, 1);
}

void foo()
{
  auto vect = GetVector();
}

Here the value returned by the GetVector function is immediately created in memory allocated for the vect object. At the same time, the move/copy constructor is eliminated.

We may apply RVO if the object returned from a function is a prvalue expression. This expression must have the same type as the type of the function's return value by signature, without cv qualifiers. Besides, with C++17, RVO is no longer an optimization, but a rule that compilers must follow. This rule is applied even if a move/copy constructor has side effects.

The RVO rule is one of the compiler rules that implement the temporary materialization mechanics. It's a principle, according to which a prvalue expression isn't physically created in memory until it's assigned to a non-prvalue object. Such compiler behavior reduces the number of copies. As a result, it speeds up the program execution.

There's another optimization type — NRVO (Named Return Value Optimization). This compiler optimization type is like RVO. Instead of creating a local return object and then moving/copying it in place of the function call, this optimization instantly creates it in the right place. Its difference from RVO is that NRVO is applied to lvalue objects.

For example, in the following code fragment, instead of RVO, we apply NRVO to the returned result object:

std::vector<int> GetVector2()
{
  std::vector<int> result(1'000'000, 1);
  return result;
}

void foo()
{
  auto vect = GetVector();
  ....
}

NRVO occurs in the following way. The compiler pre-allocates and initializes the object which is supposed to receive the result of a function. Then the function gets a pointer to this object. This pointer is used in the function body instead of the actual object the function returns.

Effectively, NRVO transforms the code fragment above into the following:

void GetVector2(std::vector<int> *x)
{
  new (x) std::vector<int>(1'000'000, 0);
}

void foo()
{
  auto *x = static_cast<std::vector<int> *>(
              alloca(sizeof(std::vector<int>)));
  GetVector2(x);
  ....
  delete x;
}

However, we can apply NRVO only when the type of the actually returned object and the type of the object returned according to the function signature completely coincide.

There is also an anti-pattern that disables the use of NRVO:

typename <typename Res, typename ...T>
Res foo(T ...)
{
  Res result;
  // some calculations
  return std::move(result);
}

void bar()
{
  auto obj = foo<SomeObject>();
}

Here we should remove the std::move call. Code tries to 'tell' the compiler that the returned object must be moved with std::move and not copied. Despite that, the compiler is obliged to generate slower assembly code.

That's because the returned object is the result of calling the std::move function and its type is Res &&. The actually returned type and the type function return type according to its signature are different. Therefore, the compiler cannot apply NRVO for the foo function. We are dealing not with an optimization, but with a pessimization.

The C++11 standard says that if a compiler cannot apply an optional optimization, it must do the following. First, it must apply the move constructor. Then apply the copy constructor for local variables or formal function parameters.

Popular related articles

S'abonner

Comments (0)

close comment form
close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam