It's conventional wisdom that function inlining doesn't always benefit, and can even hurt performance:
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With