2

I'm wondering how compiler optimization handles the evaluation of parameters in case a function is an empty call. Have an example:

void function(const std::string& input) {}
std::string getInput()
{
// some magic here...
return "some_string";
}

int main()
{
function(getInput());

return 0;
}

In such case if function() is an empty call, will compiler optimize that in such way that the call to getInput() won't happen?

  • Why not simply look at the compiled code for yourself and see exactly what it does? – Remy Lebeau Sep 07 '22 at 06:28
  • the code will not be optimized out, `getInput` may have some side effect. You can try yourself with a display in your `getInput` – Martin Morterol Sep 07 '22 at 06:29
  • 2
    @MartinMorterol that really depends. It is going to be optimized if there are no side effects: https://godbolt.org/z/W4hGbf6n6 – RoQuOTriX Sep 07 '22 at 06:30
  • @RemyLebeau Reading compiled code may not be "simple" depending of the level of the user ;) – Martin Morterol Sep 07 '22 at 06:31
  • OP, are you concerned by the performance of your code (will the compiler make a `call` to the function) or the correctness of the code (will the code make what you think it will make) ? – Martin Morterol Sep 07 '22 at 06:38
  • 2
    Standard FAQ entry for all questions asking if a compiler can/will do an optimization: [What exactly is the "as-if" rule?](https://stackoverflow.com/questions/15718262/what-exactly-is-the-as-if-rule) If an optimization falls under this rule, and if the situation is easy to recognize, then there is probably a compiler that does it. – JaMiT Sep 07 '22 at 07:51
  • If you understand assembler then you could use the compiler explorer at https://godbolt.org/ to see what the code does under various compilers. Using the Microsoft C++ compiler it is optimized away as far as I can tell when optimizations are enabled and not when they aren't (no surprise there) – Code Gorilla Sep 07 '22 at 08:53
  • compiler maybe inlined some short and simple functions by itself when it optimize code, so your empty function maybe "disappeared" in compiled output. – Exlife Sep 07 '22 at 09:19
  • @MartinMorterol I'm concerned about the performance of my code. But as some of the guys suggested here, I will look into compiler explorer to see what's happening. – Arkadiusz M Sep 07 '22 at 13:05
  • Except for the sake of asking the question, is there a reason to write such a function ? –  Sep 08 '22 at 07:46
  • @YvesDaoust yeah, there is a reason. I wanted to have a class method that serializes class to a string that could be passed to the logger. – Arkadiusz M Sep 15 '22 at 12:13
  • You can make the string a static class member. –  Sep 15 '22 at 12:18

1 Answers1

0

Other than return-value optimization, C++ doesn't allow optimizations that change visible side effects of the program. (What are copy elision and return value optimization?). e.g. a cout << in a constructor might not get called, or in more recent C++ versions is guaranteed not to get called in some cases where you'd naively expect it (and where earlier C++ revisions would make that side effect happen).

This isn't that, so the call to getInput() definitely has to happen, even if its return value ends up not being used (because it inlines and optimizes away void function(const std::string& input) {}). The fact that getInput()'s return value is being passed to an empty function makes zero difference in the call to getInput() itself.

As a general rule, only visible side effects in constructors / destructors are ever in danger of being optimized away. It's normally a bad idea to put visible side effects in constructors, except for side effects on the object being constructed. Except for debug logging where you want it to not log depending on optimization.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847