2

I have a question regarding inline functions in C/C++ and parameters copying. Let's assume I want to run this function without inline specifier:

void my_func(int param)
{
    my_inner_func(param);
}

param will be first copied to my_func and then again to my_inner_func. If the function my_func were inline, would the compiler copy param only once for the my_inner_func parameter or twice anyway?

Edit

I would like to ask for explanation for both C and C++.

halfer
  • 19,824
  • 17
  • 99
  • 186
Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74

6 Answers6

7

Both C and C++ specify behavior of your program in terms of an abstract machine.

my_func's int param exists in this abstract machine, and it is distinct from the my_inner_func's int param. They have separate identities. If you take the address of one and the address of the other, they are guaranteed to compare not-equal.

But if you don't take the address of either, neither need have an address.

If all you do to an int is assign or initialize it, then use it to assign or initialize another int, and the compiler can prove there is no defined way to reach the intermediate int through indirection (like a pointer), then the intermediate int need not exist on the actual target machine.

Some compilers have problems with doing this at link-time. Others do not.

Some operations will block the existence of that int from being elided. Others will not.

I see nothing in your example that would require that intermediate int to exist on the target machine.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
4

The compiler can do what the heck it wants, as long as it follows the as if rule.

The rule states that at any point you try and measure the state of the program, it gives the correct answer.

Where you are not measuring it, it can re-order operations.

When the compiler is optimizing, it would be able to remove the second copy of parameters for both inline and non- inline code, unless there was code which tried to spot this. Then it would have to produce code which met the as-if rule, and possibly increase the number of copies.

mksteve
  • 12,614
  • 3
  • 28
  • 50
2

inline it is only a "suggestion" to the compiler, that can or cannot inline your function. Obviously can also choose to inline a function you didn't explicitly declare inline.

In other words, based on your compiler and your optimization settings, you should look at the generated assembler to have an answer.

LPs
  • 16,045
  • 8
  • 30
  • 61
  • Looks like I remembered something spectacularly wrong with the definitive "`inline` is ignored"... My bad. It is quite a murky subject to be fair, since it reaches over both standard wording and implementation details. – Quentin May 24 '17 at 12:16
  • 1
    I don't think *"suggestion"* is very descriptive of what `inline` does. It relaxes ODR by allowing definition in multiple translation units, and in fact adds new requirement for definition in all translation units that use the function. This new requirement has the *intended* side-effect of allowing inline expansion across translation units. Which on the other hand is not a strict requirement, if LTO is used. It's a suggestion for inlining in the same way as granting a drivers license is a suggestion to drive a car (bad analogy, doesn't work on all levels). – eerorika May 24 '17 at 12:20
  • @user2079303 ..bad as my worst english... ;) – LPs May 24 '17 at 13:14
2

It depends. An inlined function (btw, it's best to let the compiler decide whether to do this or not) must expose the same observable behavior as the normal, not inlined version. So the easiest approach would be to copy the parameter twice. The compiler could be smart enough to optimize out the second copy if the inner function doesn't write to it.

  • Since the outer function no longer uses the value after the call to the inner function, it would be safe for the compiler to use the same object even if the inner function does write to it. – eerorika May 24 '17 at 12:05
  • I consider the OP snipped pseudo-code; of course, what you describe would be another corner case that can be optimized. –  May 24 '17 at 12:14
  • Well, if you cannot assume that OP doesn't read from `param` after the call, I don't think you can assume that OP doesn't take the address of either param at any point, which is another requirement for the optimization to take place. – eerorika May 24 '17 at 12:16
0

If the functions are not inlined, on most architectures the cost of the function call itself will be significantly higher than copying an int.

On systems where parameters are passed in registers, the register for passing single int parameters will already hold the value when entering the first function and it can just pass it on to the next function without creating a new copy.

Don't sweat the small stuff!

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

Well, in inline functions, the compiler has access to all the code involved to inline, so it can detect if you are actually modifying the parameters, so it can avoid copying if your function does not modify the parameters. Said this, what's the problem you are facing to?

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31