13 Replies - 412 Views - Last Post: 28 August 2013 - 10:06 PM Rate Topic: -----

#1 xconwing  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-May 13

concept of inline function?

Posted 28 August 2013 - 05:27 PM

Hi all,

From what I gathered, inline function is like the physical written where it's called. But isn't that what the compiler do anyway if "inline" keyword wasn't there. So what actually happen when a function call? And what does "inline" mean in term of class?

Thanks,
X
Is This A Good Question/Topic? 0
  • +

Replies To: concept of inline function?

#2 Secretmapper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 20-July 12

Re: concept of inline function?

Posted 28 August 2013 - 05:55 PM

Inline means that the function is so-to-speak copy pasted into the code. If you did not have inline there it would call the function.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3070
  • View blog
  • Posts: 10,765
  • Joined: 08-August 08

Re: concept of inline function?

Posted 28 August 2013 - 06:04 PM

Note that the compiler is not required to respect your request to inline a function, and it may inline a function you haven't requested it inline, so there's not much point in worrying about it.
Was This Post Helpful? 1
  • +
  • -

#4 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: concept of inline function?

Posted 28 August 2013 - 06:06 PM

When you call a function, the compiler goes to where the function definition is defined. The call usually has some overhead associated with it. What inline does is it tells the compiler to just replace every function call with the definition. So instead of having one copy of the function, you would have one for each call.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: concept of inline function?

Posted 28 August 2013 - 06:23 PM

On a tangent. If you had the recursive version of the factorial function marked as inline, will a compiler ignore the inline request?

Sent from my T-Mobile G2 using Tapatalk 2
Was This Post Helpful? 0
  • +
  • -

#6 xconwing  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-May 13

Re: concept of inline function?

Posted 28 August 2013 - 07:02 PM

View Postsalazar, on 28 August 2013 - 06:06 PM, said:

When you call a function, the compiler goes to where the function definition is defined. The call usually has some overhead associated with it. What inline does is it tells the compiler to just replace every function call with the definition. So instead of having one copy of the function, you would have one for each call.


What is this "overhead" you speak of?
Was This Post Helpful? 0
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,357
  • Joined: 31-December 10

Re: concept of inline function?

Posted 28 August 2013 - 07:21 PM

View Postxconwing, on 28 August 2013 - 10:02 PM, said:

View Postsalazar, on 28 August 2013 - 06:06 PM, said:

When you call a function, the compiler goes to where the function definition is defined. The call usually has some overhead associated with it. What inline does is it tells the compiler to just replace every function call with the definition. So instead of having one copy of the function, you would have one for each call.


What is this "overhead" you speak of?

The point of the inline function was to remove the function call overhead, which if the function is inlined, there is no function call overhead. Here's a quote from the C++ Annotated Reference Manual from the part on inlining section 7.1.2 page 100:

Quote

Inlining does not affect the meaning of a function call. In this, inlining differs from macro expansion. An inline function has the usual function definition syntax and obeys all the usual scope and type checking rules. Inlining is simply a different implementation technique for function calls. Instead of generating code transferring control and passing arguments to a single copy of the function's code, a suitably modified copy of the function's code replaces the call. Inlining saves the time needed for the transfer of control (both there and back again) including the time needed to save and restore registers, arguments, and so on. Once inlined, the code of a function becomes available to optimizers that would otherwise not optimize across a function call boundary.

This post has been edited by vividexstance: 28 August 2013 - 07:22 PM

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3070
  • View blog
  • Posts: 10,765
  • Joined: 08-August 08

Re: concept of inline function?

Posted 28 August 2013 - 07:23 PM

At a minimum the machine code needs to jump from one part of the code to another and then back again. If you're calling this function from within a series of nested loops the extra cycles this takes would be multiplied by the total number of iterations. If possible it would be good to inline that function, but like I said earlier, the compiler can do it or not, regardless of what you've requested.
Was This Post Helpful? 2
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 834
  • View blog
  • Posts: 2,457
  • Joined: 20-March 10

Re: concept of inline function?

Posted 28 August 2013 - 07:59 PM

As CTphpnweb has said,

It is generally up to the compiler to decide which functions to inline and it may or may not inline a function you have requested to inline, this happens automatically.

But to understand what inline means consider this small example.

inline int Add(int a, int B)/>/>
{
  return a+b;
}


when you use the Add function such as

int sum = Add(x,y);


the compiler can replace the function call with

int sum = x+y;


but it may decide not to, it comes down to compiler design.

Best Wishes

Snoopy.

This post has been edited by snoopy11: 28 August 2013 - 08:02 PM
Reason for edit:: Spelling mistakes

Was This Post Helpful? 0
  • +
  • -

#10 xconwing  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-May 13

Re: concept of inline function?

Posted 28 August 2013 - 08:03 PM

View PostCTphpnwb, on 28 August 2013 - 07:23 PM, said:

At a minimum the machine code needs to jump from one part of the code to another and then back again. If you're calling this function from within a series of nested loops the extra cycles this takes would be multiplied by the total number of iterations. If possible it would be good to inline that function, but like I said earlier, the compiler can do it or not, regardless of what you've requested.


That makes sense. Thank you all for the replies.
Was This Post Helpful? 0
  • +
  • -

#11 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,357
  • Joined: 31-December 10

Re: concept of inline function?

Posted 28 August 2013 - 08:23 PM

View Postsnoopy11, on 28 August 2013 - 10:59 PM, said:

As CTphpnweb has said,

It is generally up to the compiler to decide which functions to inline and it may or may not inline a function you have requested to inline, this happens automatically.

But to understand what inline means consider this small example.

inline int Add(int a, int B)/>/>/>/>
{
  return a+b;
}


when you use the Add function such as

int sum = Add(x,y);


the compiler can replace the function call with

int sum = x+y;


but it may decide not to, it comes down to compiler design.

Best Wishes

Snoopy.

So the function arguments are not evaluated more than once, your example would more likely be:
int tmp1, tmp2;
int sum = ( (tmp1 = x), (tmp2 = y), (tmp1 + tmp2) );



*EDIT*:
Here's an example from the book I quoted above to show why the temporary variables are necessary:
double old_a;

#define DBL(a) ((old_a = a), ((a) + (a)))

inline int dbl(int a) { old_a = a; return a + a; }

// The function and macro can be used like:
void f(int* pi, char* pc)
{
    double old_a = 7;    // hides global 'old_a'
    // ...
    old_a = dbl(*pi++);
    old_a = dbl(pc);    // error: argument type mismatch
}

// The correct call above will expand to something like:
int tmp;
old_a = ( (tmp = *pi++), (::old_a = tmp), (tmp + tmp) );


This post has been edited by vividexstance: 28 August 2013 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: concept of inline function?

Posted 28 August 2013 - 08:40 PM

The other reason for the advantages of inlining beyond saving the overhead of pushing parameters on the stack and setting up a new stack frame is that with modern processors you always want to keep the CPU pipeline full. By inlining, the next set of instructions are ready for the CPU to execute. If you have to call or jump to another location, the pipeline may get stalled while the instructions at that new location are fetched and put into the CPU instruction pipeline.
Was This Post Helpful? 1
  • +
  • -

#13 xconwing  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-May 13

Re: concept of inline function?

Posted 28 August 2013 - 09:21 PM

View PostSkydiver, on 28 August 2013 - 08:40 PM, said:

The other reason for the advantages of inlining beyond saving the overhead of pushing parameters on the stack and setting up a new stack frame is that with modern processors you always want to keep the CPU pipeline full. By inlining, the next set of instructions are ready for the CPU to execute. If you have to call or jump to another location, the pipeline may get stalled while the instructions at that new location are fetched and put into the CPU instruction pipeline.


But wouldn't that call/jump be consider as an instruction and therefore be pipelining as well?
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: concept of inline function?

Posted 28 August 2013 - 10:06 PM

The jmp/call instruction will be in the pipeline itself, but the instructions at the location where the jmp/call goes to may not be.

Let's say the pipeline is only 64 bytes long, and the code was in memory like this:
0x10000000 push param3       ; pass parameters
0x10000004 push param2
0x10000008 push param1
0x1000000C call 0x20000000   ; call the function
0x10000010 cmp 0, eax
0x10000014 jnz 0x10000030
:
0x10000030 ret
:
0x20000000 push ebp          ; setup stack frame within function
:
0x20000020 ret               ; return from function



The pipeline will start off having addresses 0x10000000 through 0x10000040. When the call is executed, the pipeline needs to be flushed, and memory from 0x20000000 through 0x20000040 will have to be read in.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1