Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
RVO and NRVO

RVO and NRVO

Sep 17 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


Comments (0)

Next comments next comments
close comment form