Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function inlining—what are examples where it hurt performance?

Tags:

c++

c

inline

It's conventional wisdom that function inlining doesn't always benefit, and can even hurt performance:

  • The Linux kernel style guide warns against excessive inlining
  • Google also recommends programmers be careful with inlining
  • The C++ FAQ lite says more of the same

I understand why inlining is supposed to help—it eliminates function call overhead by including the called function in its caller.

I also understand why people claim it can hurt performance—inlining functions can in some cases increase code size, which can eventually increase cache misses or even trigger extra page faults. This all makes sense.

I'm having trouble, though, finding specific examples where inlining actually hurts performance. Surely if it's enough of a problem to be worth warning about it, someone somewhere must have come across an example where inlining is a problem. So, I ask…

What is a good, concrete example of code where performance is actually hurt by function inlining?

like image 872
LnxPrgr3 Avatar asked Apr 27 '11 18:04

LnxPrgr3


1 Answers

On some platforms, with large inlined functions, performance can be reduced by causing a "far" jump rather than a relative jump. Inlining may also cause a page fault where the OS needs to haul in more code into memory, rather than executing code with may already exist (as a subroutine).

Some platforms may have optimized jump instructions for "near code". This type of jump uses a signed offset from the current position. The signed offsets may be restricted, for example 127 bytes. A long jump would require a bigger instruction because the longer jump must include the absolute address. Longer instructions take more time to execute.

Long inlined functions may expand the length of the executable so that the OS needs to haul in a new "page" into memory, called a page swap. Page swapping slows down execution speed of an application.

These are "possible" reasons how inlined code could slow performance. The real truth is obtained by profiling.

like image 132
Thomas Matthews Avatar answered Sep 23 '22 01:09

Thomas Matthews