    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: A Deeper Look at Inline Functions</title>
        <link>https://members.accu.org/index.php/journals/449</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #42 - Apr 2001 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c162/">42</a>
                    (9)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c162-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c162+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;A Deeper Look at Inline Functions</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 April 2001 17:46:05 +01:00 or Thu, 26 April 2001 17:46:05 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>I think it's safe to say that all Overload readers know what C++
inline functions are. When we declare a function or member function
as inline we are trying to avoid the overhead of a function call by
getting the compiler to embed the code for the function at the
point of the call. I'd like to take a few minutes and delve a bit
deeper.</p>
<p>Inlines are always talked about in relation to performance. This
isn't an article on performance but it is an article that talks
about performance issues.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e24" id=
"d0e24"></a>Optimisations</h2>
</div>
<p>C++ inline functions present a potential optimisation to code:
make a function inline and you save the overhead of a function
call, that is: loading parameters onto the stack, making a jump,
popping parameters off the stack, and then it all in reverse when
you return.</p>
<p>Modern CPUs have pipelines and attempt to optimise code
execution on the fly. A function call can disrupt this pipeline
when it is encountered, and again, when it returns. So not only are
we avoiding the function call, but also we potentially allow the
CPU to further improve execution.</p>
<p>Not only this, but a compiler may be able to optimise code which
has been inlined in a way it cannot if there is a function call
simply because the optimisation becomes visible, e.g. optimising
register allocations. In the case of empty functions - fairly
typical of constructors - making the function inline may allow the
compiler to remove the call altogether.</p>
<p>Sometimes an inline function may save time and space. Making a
function call requires instruction codes to make the call both on
the part of the caller and callee. On occasions the number of bytes
required for this may actually be greater than the number of bytes
in the function body itself.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e35" id="d0e35"></a>Inline as a
better macro</h2>
</div>
<p>C programmers used to simulate inline functions with macros. C++
inline functions where in part, an attempt to encode this as a
language feature rather than yet another use of the macro
pre-processor. However, they are not an exact replacement.</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>If you use a macro the compiler has no desecration, the function
will be inlined.</p>
</li>
<li>
<p>Scope rules are different.</p>
</li>
<li>
<p>Macros, particularly multi-line macros are much more difficult
to understand and maintain.</p>
</li>
</ul>
</div>
<p>In short, inlines are an improvement on macros but they are not
a direct replacement.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e52" id="d0e52"></a>Catches</h2>
</div>
<p>With these kinds of optimisations available we might wonder why
we don't just make all functions inline. Simply, there are
downsides, and in my opinion they out weigh the advantages.</p>
<p>For starters inline is only a hint to the compiler, it is free
to ignore it. It will typically ignore it if:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>The member is a virtual: it's not really possible to have an
inline virtual function as virtual implies an indirect function
call - although compiler can sometimes do it if it has type
information.</p>
</li>
<li>
<p>The function is recursive or mutually recursive with other
functions.</p>
</li>
<li>
<p>The compiler will also ignore the inline if it considers the
body of the function &quot;too big.&quot; How big is too big depends on your
compiler.</p>
<div class="sidebar">
<p class="title c2">The Other Inline</p>
<p>C++ inline functions are not the only &quot;inlines&quot; supported by
most compilers. It is also possible to place assembler code within
C++ code and this is commonly referred to as &quot;inline assembler&quot;.
It's normally clear which type is being talked about, but it's more
difficult when doing a keyword search in MSDN or a search engine to
pin down what you want.</p>
<p>The days when most systems contained some inline assembler to
improve speed are fast receding. Traditionally used to speed up
performance critical sections of a program it's now doubtful that
even this is possible. Increasingly, to get the best performance
out of a CPU requires you to know a lot of things about memory
layout, cache architecture, pipelining, out of order execution and
so on. While these rules can be encoded in a good compiler it is
hard for an individual to know and apply all these rules.</p>
<p>There are a few occasions where inline assembler is still useful
though. Microsoft use a little bit in the ATL to &quot;thunk&quot; a function
call, and sometimes hard coding a break point in your code (asm {
int 03h; } in Intel speak) has it's uses.</p>
</div>
</li>
<li>
<p>Inline code will not be generated when using a variable number
of arguments to a function call (e.g. <tt class=
"function">printf</tt> would not be inlined) or a variable sized
data type, e.g.</p>
<pre class="programlisting">
void foo(int n) {
char str[n];
...
}
</pre></li>
<li>
<p>Compilers have a limit to the depth they will inline functions,
e.g. if inline A calls inline B, which calls inline C and so on.
This is often configurable through a pragma or command line
switch.</p>
</li>
</ul>
</div>
<p>Of course, every compiler will differ slightly and you or I
can't guarantee that there isn't a compiler out that can inline
some of these things. Conversely, there are more reasons why your
compiler will not inline a function that those given here. These
are clear-cut cases but there are several other reasons why I avoid
using inline functions. The main culprit is: code bloat, inline
functions usually make your code bigger because you have duplicated
code for each invocation of the function. Apart from needing a
bigger hard disc code bloat has several implications:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Increased number of CPU cache misses: a CPU may keep a
frequently executed function entirely in it's cache, if you
increase the function size with lots of inline functions it may be
too big for the cache, and because your functions are bigger fewer
may be held in cache at once.</p>
</li>
<li>
<p>Disc cache: if every function is inlined the OS may not load the
entire program into memory and your disc will start to thrash.</p>
</li>
</ul>
</div>
<p>So, don't be surprised if making methods inline actually makes
your program slower!</p>
<p>In addition embedded systems are very sensitive to code bloat: a
PC may have a big CPU cache, oodles of fast RAM and a big fast hard
disc but an embedded system probably won't. In the extreme this may
mean you need to fit your device with a bigger ROM, which means it
costs more, which in a competitive market may make the difference
between success and failure.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e102" id="d0e102"></a>And some more
catches...</h2>
</div>
<p>There are catches which have to do less with an individual piece
of code and more with how a system hangs together. These design
issues mean you should think carefully before making a method
inline:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Coupling between headers and implementation increases because a
change is more likely to occur in the header file, which causes a
bigger rebuild because other files depend on your header files. If
the code is in the implementation file by contrast only that file
needs to be recompiled. On large systems the difference can be
hours.</p>
</li>
<li>
<p>Putting the implementation in the header file reduces the
opportunity to forward declare classes, so you have to include
another header file, increasing coupling; making the code less
flexible and more prone to ripple effects - again increasing build
times.</p>
</li>
</ul>
</div>
<p>Perhaps a more significant point is what a lot of inline
functions says about your design. Inlining is typically used for
get and set functions. Classes with lots of such functions aren't
really hiding anything, they are just containers for values which
begs the question, just how object oriented is your design?</p>
<p>Some other points that are worth noting about inlined functions
are:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Finding code in source files can be time consuming, if you have
lots of inline functions you need to look in the .h files as well
as the .cpp files. This may seem trivial but on a large system with
several thousand files the time adds up.</p>
</li>
<li>
<p>If some of your code is compiled with inlining enabled and some
with them disabled you will encounter link errors.</p>
</li>
<li>
<p>Some compilers have an <span class=
"emphasis"><em>auto-inline</em></span> function that lets the
compiler decide which functions to inline and which to leave as is.
Of course, how aggressive this is, and hence how much difference it
makes, is dependent on your compiler.</p>
</li>
<li>
<p>Inlining is usually only enabled when optimised code is being
generated, so your debug builds won't have any inlined
functions.</p>
</li>
<li>
<p>Although inline is commonly thought of as a C++ optimisation it
is also available in C, and other languages, although these may
need vendor specific extensions.</p>
</li>
<li>
<p>Theoretically each inline function should have one, and only one
implementation body. However, it would be possible to provide
different bodies in different files. Stroustrup points out in the
<i class="citetitle">Design &amp; Evolution of C++</i> that most
compilers don't check for this, we can hope that in the 7 years
since he made his comments the situation has improved but I'm not
sure. Before anyone think of a use for this &quot;feature&quot; consider the
maintenance nightmare and please don't do it.</p>
<div class="sidebar">
<p class="title c2">Seeing inline at work</p>
<p>The most reliable way to see if a function is being inlined or
not is to look at the output from the compiler. Most compilers have
a switch to output assembler code for your inspection. In Visual
C++ there are several /FA which control this - the &quot;listing file
type&quot; in the project C++ settings box.</p>
<p>The other switches worth playing with are the /Ob which control
the aggressiveness of inline expansion - the &quot;Inline function
expansion&quot; option in C++ settings.</p>
<p>It's interesting to experiment with these switches and the
following example:</p>
<pre class="programlisting">
struct Widget {
  int number_;
  Widget();
  int t() const;
};
Widget::Widget() : number_(99) { }
int Widget::t() const
  {return number_; }
int main(){
  Widget w;
  int i = w.t();
  return i;
}
</pre>
<p>It's interesting to make the constructor and t function inline
and look at the assembler code, or use the /Ob2 switch to force
functions to be inline, at this point the compiler reduces the
program to:</p>
<pre class="programlisting">
push  99
pop  eax
</pre>
<p>The program still comes out at 19,968 bytes, which demonstrates
the overhead that even the simplest programs may carry.</p>
</div>
</li>
</ul>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e160" id="d0e160"></a>When to use
inlines</h2>
</div>
<p>Despite all this there are occasions when inlines make
sense.</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Templates based code is always inlined: this is one of the
reasons people believe templates lead to bigger executables.</p>
</li>
<li>
<p>Sometimes making a function inline will improve performance for
all the reasons given at the start.</p>
</li>
</ul>
</div>
<p>The orthodox approach to performance today is to design a system
for ease of maintenance, flexibility and extendibility, and only
consider performance second. Usually, this means producing a
working system and then performance profiling, improving the
slowest bit and repeating until the system is fast enough.</p>
<p>When you profile your code you may find a hot spot in a function
that could benefit from inlining. This is quite understandable, and
at this point in the cycle your can afford to sacrifice some
maintainability for speed if it is needed. However, after making
the change repeat the performance test to ensure it actually does
help.</p>
<p>One compromise is to avoid inlining other than for templates.
Then as part of the QA or performance cycle use your compilers
auto-inline feature. This compromise is not ducking the issue, or
delivering code with sub-optimal performance. In fact, it's taking
an engineering line, we build something, measure it, and if
necessary improve it. (Stroustrup questions if letting the compiler
decide is the best policy.)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e178" id=
"d0e178"></a>Conclusion</h2>
</div>
<p>While inline functions may improve performance seldom will you
be able to radically change a program's performance by making a
bunch of functions inline. Inlines are not free; they will usually
increase your program size and may actually impair performance.</p>
<p>They may also increase your development cycle because they
increase coupling between files and lead to longer build times and
more ripple effects.</p>
<p>Heavy reliance on inline functions may indicate flaws in your
design; either a lack of object-orientedness or failure to design a
system with the required performance characteristics.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e187" id=
"d0e187"></a>Bibliography</h2>
</div>
<p>Many books and web sites feature articles on optimisation
techniques. While researching this piece I looked at quite a few
specifically seeking out those that talked of inline functions.
This is a selection of those I find interesting enough to read:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Stroustrups views on inline functions are covered in the Design
&amp; Evolution of C++, Addison-Wesley 1994</p>
</li>
<li>
<p>Pete Isensee has an interesting article on optimisation at
<a href="http://www.tantalon.com/pete/cppopt/main.htm" target=
"_top">www.tantalon.com/pete/cppopt/main.htm</a>. In this he takes
a slightly different view on when to optimise. He notes that
Microsoft Visual C++ 6.0 only ever inlines the functions the
developer specifies. My own experience is slightly different (see
sidebar).</p>
</li>
<li>
<p>Wind River Systems have a good article focusing on embedded
systems at <a href=
"http://www.wrs.com/products/html/%20optimization_wp.html" target=
"_top">www.wrs.com/products/html/ optimization_wp.html</a>.</p>
</li>
<li>
<p>There is an interesting article on optimisation in general at
<a href="http://www2.ios.com/~jimwe19/program/optimize.htm" target=
"_top">www2.ios.com/~jimwe19/program/optimize.htm</a>,
unfortunately the e-mail address <tt class="email">&lt;<a href=
"mailto:ihfhq@instructor.net">ihfhq@instructor.net</a>&gt;</tt> is
the sole clue as to who wrote it!</p>
</li>
</ul>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
