From: Joshua Maurice on
On Jun 14, 1:23 am, Vaclav Haisman <v.hais...(a)sh.cvut.cz> 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.
>
> >> http://en.wikipedia.org/wiki/Inline_expansion
>
> >> for a brief summary and vocabulary starting point.

It appears as though the only "real" "problem" which he ignored is:

> The increase in code size may cause a small, critical section of code to no longer fit in the cache, causing cache misses and slowdown.

Specifically, let's consider a function f which calls a function g,
but not always. g is called from f in a conditional body. Let's
further suppose that this is the uncommon path. Expanding inline g
into f could greatly increase the size of f, causing greater
instruction cache misses for relatively small gains (under the
assumption that the g branch is an infrequent path). Thus Peter
Olcott, your premise (1) is false, and thus your argument is not
sound.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Asher Langton on
On Jun 13, 1:43 pm, Peter Olcott <NoS...(a)OCR4Screen.com> wrote:
> (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.

Consider this snippet:

while (...)
{
...
if (condition)
doSomething();
}

If doSomething() is inlined, then the body of the loop might no longer
fit in the instruction cache, which -- in the case where 'condition'
is unlikely to be true -- could cause a significant decrease in
performance. Contrary to your claim, doSomething() might only be
called once, yet it would not make sense to inline this function call.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Vaclav Haisman on
Nick Hounsome wrote, On 14.6.2010 10: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.
This is true not true anymore, for quite some time actually. There are plenty
of compilers that can do various forms of whole program optimization with
inter-translation unit optimizations. VS since 2005, GCC now and probably
others that I do not know about. They all can process all TUs at once, seeing
what is used when and how many times.

--
VH

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nick Hounsome on
On 15 June, 10:03, Francis Glassborow
<francis.glassbo...(a)btinternet.com> wrote:
> 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.
>
> Yes it can if the programmer remembers to declare the function static.

The OP was using class functions - I think you just mean file static.

> It can also usually know it if the function is declared in the unnamed
> namespace (though exported templates can sometimes break that.)

But I would expect a good compiler to "do the right thing" here anyway
since it can know what is best even without whole program
optimisation.

Such usage is also questionable since there is no logical reason for a
function that is known to be only called from one place except where a
it must be passed by pointer to another function. (I agree that
sometimes it can be a good idea for reasons of clarity).


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nick Hounsome on
On 15 June, 09:59, Ulrich Eckhardt <eckha...(a)satorlaser.com> wrote:
> 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.

I think that this is actually only "whole exe/dll optimization".
When an app consists of a small exe and many dlls (as most real apps
do) the optimization doesn't extend across these (unless you have a
virtual machine type system such as .NET or Java)

What I have read of VS 2010 suggests that it only does it in
restricted circumstances.

> That said, the OP should just use that optimisation, since his compiler
> supports it, and stop trying to outsmart the compiler.

Indeed - As I said the system is allowed to inline even if the inline
keyword is not present


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]