Built-in functions

Possible duplicate:
Benefits of built-in functions in C ++?

I have a confusion regarding the inline function.

People say that built-in functions save processor time by replacing a function with source code, but increasing the size of the code compared to a regular function.

So, the real question is that I keep calling this inline function 10 times in a loop whether the code size will increase.

Suppose the size of an inline function is 2 bytes, will it increase by 20 bytes?

Can anyone explain this to me?

+4
source share
5 answers

The same code will be executed 10 times. But still in a loop, so the code is not copied 10 times in a row. Thus, the size will not increase with the number of executions.

+9
source

I don’t know why you think the number of iterations of the loop will matter. We will see. Suppose you write this:

inline int foo() { return 5 * gargle(); } /* later... */ for (size_t i = 0; i < 100; ++i) { const int x = i * foo(); baz(x + lookup[i]); } 

If foo becomes nested, then essentially the compiler processes your code as if you wrote:

 for (size_t i = 0; i < 100; ++i) { baz(i * (5 * gargle()) + lookup[i]); } 

Thus, the code is only replaced on the call site once.

(This is a separate question, regardless of whether the loop is reversing.)

+3
source

It is entirely up to you, your code, and your compiler. Imagine that you have:

 #include <vector> int frob (int a, int b) { return a + b; } int main () { std::vector<int> results(20), lhs(20), rhs(20); for (int i=0; i<20; ++i) { results[i] = frob(lhs[i], rhs[i]); } } 

Now, if your compiler is optimized for size, it can leave it as it is. But if it optimizes for performance, it may (or maybe not, some compilers use crude heuristic measures to determine this), convert this value to:

 int main () { std::vector<int> results(20), lhs(20), rhs(20); for (int i=0; i<20; ++i) { results[i] = lhs[i] + rhs[i]; } } 

If he optimizes even more, he can unroll

 int main () { std::vector<int> results(20), lhs(20), rhs(20); for (int i=0; i<20; i+=4) { results[i] = lhs[i] + rhs[i]; results[i+1] = lhs[i+1] + rhs[i+1]; results[i+2] = lhs[i+2] + rhs[i+2]; results[i+3] = lhs[i+3] + rhs[i+3]; } } 

Size increased. But if the compiler now also solves a bit of automatic vectorization, it can transform again into something not like this:

 int main () { std::vector<int> results(20), lhs(20), rhs(20); for (int i=0; i<20; i+=4) { vec4_add (&results[i], &lhs[i], &rhs[i]); } } 

Size reduced.

Next, the compiler, as always, smart, deploys again and completely kills the loop:

 int main () { std::vector<int> results(20), lhs(20), rhs(20); vec4_add (&results[i], &lhs[i], &rhs[i]); vec4_add (&results[i+4], &lhs[i+4], &rhs[i+4]); vec4_add (&results[i+8], &lhs[i+8], &rhs[i+8]); vec4_add (&results[i+12], &lhs[i+12], &rhs[i+12]); vec4_add (&results[i+16], &lhs[i+16], &rhs[i+16]); } 

G ++ optimization will be performed if it can be done enough to replace a vector with a regular array

 int main () { int results[20] = {0}, lhs[20] = {0}, rhs[20] = {0}; vec4_add (&results[i], &lhs[i], &rhs[i]); vec4_add (&results[i+4], &lhs[i+4], &rhs[i+4]); vec4_add (&results[i+8], &lhs[i+8], &rhs[i+8]); vec4_add (&results[i+12], &lhs[i+12], &rhs[i+12]); vec4_add (&results[i+16], &lhs[i+16], &rhs[i+16]); } 

He sees how everything is constant, and develops

 int main () { int results[20] = {0}; // because every lhs[0]+rhs[0] == 0 } 

He concludes that the results are not actually used and finally spit out:

 int main() { } 
+1
source

When you use inline, you tell the compiler to replace any calls with your inline method, with the code for that method. For instance:

 inline int min(int a, int b) { return (a < b) ? a : b; } void some_method() { int x = min(20, 30); } 

will be changed by the compiler to:

 void some_method() { int x = (20 < 30) ? 20 : 30; } 

If it were in a loop, it would still be the only replacement, so in this particular situation the code size will not increase.

However, there are problems with the built-in functions that should be considered. Often, letting the compiler decide what to do will be more efficient than doing it yourself.

0
source

Using the inline , give the compiler permission to inline function calls that it may or may not accept.

The reason this can speed up the program is because the CPU will not have to make a function call and it will not have to push parameters onto the stack, so in fact the compiler can generate much less code on the call site than when making the call functions.

In addition, the optimizer may be able to re-order / eliminate instructions that are now closer to each other to provide even better performance and even less code.

The only way to find out if this happens is through trial and error. You write it in one way and measure the performance and size of the code, and then write it in another way and check again.

0
source

All Articles