Prev: How to reuse an existing stream with a different streambuf ?
Next: A quick question about Initializer-List in C++0x
From: Nick Hounsome on 13 Jun 2010 17:23 On 13 June, 21:43, Peter Olcott <NoS...(a)OCR4Screen.com> wrote: > On 6/13/2010 5:06 AM, Keith H Duggar wrote: > > > On Jun 12, 2:12 am, Peter Olcott<NoS...(a)OCR4Screen.com> wrote: > >> Inlining is always better for functions that are known > >> to only need to be called from a single place. > > > In those absolute terms that statement is flat wrong. Whether to > > inline depends intimately on the relative execution frequency and > > size of the code in question versus code in the calling chain. It > > has to do with optimizing instruction cache performance and other > > factors. See the "Problems" section in the wikipedia > > (1) The ONLY reason not to always inline everything (that can be > in-lined) all the time is code bloat. > (2) If a function is only called once then there can not possibly be any > code bloat at all. > (3) Therefore all functions that are only called once should always be > in-lined. A C++ compiler can never know this since it only ever sees one compilation unit. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 14 Jun 2010 17:59 Nick Hounsome wrote: > A C++ compiler can never know this [how often a function is used] since it > only ever sees one compilation unit. So-called "whole program optimization" has existed now for a long time in many compilers, so that statement is not actually accurate any longer. That said, the OP should just use that optimisation, since his compiler supports it, and stop trying to outsmart the compiler. Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Bo Persson on 14 Jun 2010 18:04 Nick Hounsome wrote: > On 13 June, 21:43, Peter Olcott <NoS...(a)OCR4Screen.com> wrote: >> On 6/13/2010 5:06 AM, Keith H Duggar wrote: >> >>> On Jun 12, 2:12 am, Peter Olcott<NoS...(a)OCR4Screen.com> wrote: >>>> Inlining is always better for functions that are known >>>> to only need to be called from a single place. >> >>> In those absolute terms that statement is flat wrong. Whether to >>> inline depends intimately on the relative execution frequency and >>> size of the code in question versus code in the calling chain. It >>> has to do with optimizing instruction cache performance and other >>> factors. See the "Problems" section in the wikipedia >> >> (1) The ONLY reason not to always inline everything (that can be >> in-lined) all the time is code bloat. >> (2) If a function is only called once then there can not possibly >> be any code bloat at all. >> (3) Therefore all functions that are only called once should >> always be in-lined. > > A C++ compiler can never know this since it only ever sees one > compilation unit. This is not true anymore, as several of the most popular compilers have implemented whole program (link-time) optimization. Among other things, this means that inlining is performed *after* (preliminary) linking of the object modules. Cross module inlining is happening. A reason for not inlining called-once functions might be an attempt to reduce dependencies between the modules, but hardly code size. Bo Persson -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Peter Olcott on 14 Jun 2010 18:05 On 6/14/2010 3:23 AM, Nick Hounsome wrote: > On 13 June, 21:43, Peter Olcott<NoS...(a)OCR4Screen.com> wrote: >> On 6/13/2010 5:06 AM, Keith H Duggar wrote: >> >>> On Jun 12, 2:12 am, Peter Olcott<NoS...(a)OCR4Screen.com> wrote: >>>> Inlining is always better for functions that are known >>>> to only need to be called from a single place. >> >>> In those absolute terms that statement is flat wrong. Whether to >>> inline depends intimately on the relative execution frequency and >>> size of the code in question versus code in the calling chain. It >>> has to do with optimizing instruction cache performance and other >>> factors. See the "Problems" section in the wikipedia >> >> (1) The ONLY reason not to always inline everything (that can be >> in-lined) all the time is code bloat. >> (2) If a function is only called once then there can not possibly be any >> code bloat at all. >> (3) Therefore all functions that are only called once should always be >> in-lined. > > A C++ compiler can never know this since it only ever sees one > compilation unit. > > Then it should default to always inline every function where in-lining is explicitly specified, and in-lining is possible. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 14 Jun 2010 18:00
Vaclav Haisman wrote: > Peter Olcott wrote, On 13.6.2010 22:43: >>[...] >>> In those absolute terms that statement is flat wrong. Whether to >>> inline depends intimately on the relative execution frequency and >>> size of the code in question versus code in the calling chain. It >>> has to do with optimizing instruction cache performance and other >>> factors. See the "Problems" section in the wikipedia >> >> (1) The ONLY reason not to always inline everything (that can be >> in-lined) all the time is code bloat. >> (2) If a function is only called once then there can not possibly be any >> code bloat at all. >> (3) Therefore all functions that are only called once should always be >> in-lined. > > You have completely ignored the Problems section of the Wikipedia page. > Your "reasoning" above is wrong. Could you elaborate on that? If a piece of code is only ever used in one place, not inlining it only adds the function call overhead without any benefits. There is a break even point[1] in the number of calls at which inlining a function stops making sense, but that one is not reached when the number of calls is just one. >>> http://en.wikipedia.org/wiki/Inline_expansion The second and third point in the problems section of that page are IMHO badly worded. Adding additional code in a function can cause cache misses, but adding a function call adds code overhead plus a fetch from possibly non-local memory, causing even further memory access overhead. Also, adding additional code to a function can cause register spill, yes, but calling a function that doesn't know which registers are actually used or free and which will just save them on the stack is at best the same but usually even worse for performance. Uli [1] Actually, there can be one. If you have a function that contains less code than the function call overhead itself, you should always inline that function. Typical candidates are wrapper/forwarding functions. -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |