Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compiler optimization of return value in VS 2010

using VS 2010 with full optimization /Ox look at the following two function calls:

static string test1(const string& input)
{
    return input;
}

static void test2(const string& input, string& output)
{
    output = input;
}

If I use the latter test2 then the function is always optimized out and the code inlined. However test1 is not inlined unless I turn off exceptions. Does anyone know why this is?

Additionally I would expect the compiler to be able to do as an efficient a job in test1 as test2 if it uses return value optimization but it seems not to be doing that. This also is puzzling me.

The reason I want to use the first function signature would be that I have two compilable versions of the function. I want to have the calling code always call test1 and when a certain compile flag is set I want it to append the input to a copy and return it, when the compile flag is not set I want it to get as close to being a no-op as possible.

like image 767
skimon Avatar asked Apr 09 '12 01:04

skimon


People also ask

What is return function in optimization?

In the context of the C++ programming language, return value optimization (RVO) is a compiler optimization that involves eliminating the temporary object created to hold a function's return value. RVO is allowed to change the observable behaviour of the resulting program by the C++ standard.

Is return value optimization guaranteed?

Compilers often perform Named Return Value Optimization (NRVO) in such cases, but it is not guaranteed.

Does C have return value optimization?

> Note also that C doesn't have return-value-optimization, hence all your struct-returning functions will cause a call to memcpy (won't happen when compiled in C++ mode of course).


2 Answers

Visual Studio can't inline functions that return objects with non-trivial destructors:

In some cases, the compiler will not inline a particular function for mechanical reasons. For example, the compiler will not inline:
  • A function if it would result in mixing both SEH and C++ EH.
  • Some functions with copy constructed objects passed by value when -GX/EHs/EHa is on.
  • Functions returning an unwindable object by value when -GX/EHs/EHa is on.
  • Functions with inline assembly when compiling without -Og/Ox/O1/O2.
  • Functions with a variable argument list.
  • A function with a try (C++ exception handling) statement.

http://msdn.microsoft.com/en-us/library/a98sb923.aspx

like image 116
Timo Avatar answered Sep 29 '22 15:09

Timo


The standard explicitly forbids the compiler to use return value optimization when the returned value is a parameter to the function (12.8/31):

This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

— in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function's return value

— ...

like image 29
Bo Persson Avatar answered Sep 29 '22 16:09

Bo Persson