public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: On inlining in C++
@ 2003-08-05 18:13 Robert Dewar
  2003-08-05 18:17 ` Gabriel Dos Reis
  0 siblings, 1 reply; 92+ messages in thread
From: Robert Dewar @ 2003-08-05 18:13 UTC (permalink / raw)
  To: dewar, gdr; +Cc: aph, coyote, gcc

> The behaviour I'm arguing for is that one documented for over two
> decades.  That is not passion.

No, but it is goal-directed interpretation. Not everyone agrees with
your interpretation of the documentation! (I hope that you can see
that this statement at least is correct :-)

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-06 21:37 Robert Dewar
  2003-08-06 22:14 ` Joe Buck
  0 siblings, 1 reply; 92+ messages in thread
From: Robert Dewar @ 2003-08-06 21:37 UTC (permalink / raw)
  To: dewar, jbuck; +Cc: gcc, gdr, mrs

> Given that we can't inline mutually recursive functions, it is always
> possible to fix the order so that things work, though it can be a pain
> in the ass.

and that can be a bad distortion of the source code for bad reasons!

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-06  4:31 Robert Dewar
  2003-08-06  5:13 ` Andreas Jaeger
                   ` (2 more replies)
  0 siblings, 3 replies; 92+ messages in thread
From: Robert Dewar @ 2003-08-06  4:31 UTC (permalink / raw)
  To: jbuck, mrs; +Cc: gcc, gdr

> excessive lookahead (call comes before the
> definition)

This actually seems like a pretty serious limitation to me ...

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-05 17:28 Robert Dewar
  2003-08-05 17:49 ` Gabriel Dos Reis
  0 siblings, 1 reply; 92+ messages in thread
From: Robert Dewar @ 2003-08-05 17:28 UTC (permalink / raw)
  To: dewar, gdr; +Cc: aph, coyote, gcc

> So?
> 
> -- Gaby

So, the notion of trusting the programmer is not an absolute one. C++ is
fairly far from what Tuck would call a WYSIWYG language, where you have a
reasonable knowledge of the code being generated at all times. Dispatching,
elaboration, destructor handling, exceptions, are among many cases where
the implementation model is entirely undefined by the language, and varies
between implementations, so if you expect your trusted programmer to know
what is going on, that's not always achievable.

To some extent, the current discussion is on whether inlining should or 
should not be WYSIWYG in Tuck's sense (I find this quite a useful
notion in language design and implemenation). Gaby and others argue (without
much data, but with much passion) that inlining should never be up to the
compiler. Others argue (without much data, but with much passion) that of
course the implementation model for inlining should be something the
compiler controls.

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-05 17:06 Robert Dewar
  2003-08-05 17:26 ` Gabriel Dos Reis
  2003-08-05 17:57 ` Scott Robert Ladd
  0 siblings, 2 replies; 92+ messages in thread
From: Robert Dewar @ 2003-08-05 17:06 UTC (permalink / raw)
  To: coyote, dewar; +Cc: aph, gcc

> 
> I use C++ because it implicitly trusts the programmer; that is part of 
> the core C++ philosophy.

Even the mechanism of dispatchinhg calls is completely undefined (and there
are indeed two radically different mechanisms, one of which has no overhead
from multiple inheritance, but costs quite a bit if you use MI). There are
many things in C++ where the programmer has very little control (elaboration
is another example).

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-05 16:23 Robert Dewar
  0 siblings, 0 replies; 92+ messages in thread
From: Robert Dewar @ 2003-08-05 16:23 UTC (permalink / raw)
  To: rsandifo, s.bosscher; +Cc: dewar, gcc, gdr, jbuck

Steven said

> So far, no-one has actually tried it and produced numbers.

Sounds like a relatively easy way to get some numbers, and definitely
data would help clear the air of guesses on all sides. These guesses
may be better or worse informed, but they are still guesses :-)

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-05 16:22 Robert Dewar
  0 siblings, 0 replies; 92+ messages in thread
From: Robert Dewar @ 2003-08-05 16:22 UTC (permalink / raw)
  To: dewar, rsandifo; +Cc: gcc, gdr, jbuck

> Even if we change gcc in the way that Gaby suggests, we could still
> provide the existing behaviour as a flag.  A sort of restricted
> -finline-functions.  It's not like we have to lose the existing
> behaviour entirely.

But the flag already exists, ultimately this is a discussion about where
to set the thresshold by default.

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-05 16:15 Robert Dewar
  0 siblings, 0 replies; 92+ messages in thread
From: Robert Dewar @ 2003-08-05 16:15 UTC (permalink / raw)
  To: kunert, mrs; +Cc: gcc, gdr, jbuck

> - How much total time does the code spent in the function. If this time is
>  only a few micro seconds, there is absolutely no point in inlining.

This is not nearly so simple as you think. Because of the phenomenon of
cache thrashing, you can get a procedure which looks very tiny, and the
timings of your chip may imply that the call is almost free (e.g. on the
power architecture), but because of cache thrashing you get horrible
behavior in a loop.

> The programmer has the complete picture. He has an idea about the actual
> parameters passed to the function. He can actually measure the time.
> He knows if some function is critical or not.

Well for sure that's not true, to have the complete picture requires an
exact simulation of cache behavior and in any case with hundreds of
instructions in flight, analyzing what is going on can be very very hard.

> Maybe someday in the distant future compilers are smart enough to find out
> these things. But currently I strongly prefer to control inlining myself,
> because in most cases I know better.

Possibly, but it is likely to be a case of the blind leading the blind, since
often neither the programmer nor the compiler really know enough.

Fortunately, as someone pointed out, in exewcution time, it is fairly unusual
that inlining can make a difference (we certainly have some customers with
very large critical applications, e.g. in air traffic control,, where it
does make a difference, but this is not the norm). One effect you do want
to avoid is some huge blowup in code size, which in the worst case blows
up the compiler as well.

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-05 13:22 Richard Guenther
  2003-08-05 13:25 ` Gabriel Dos Reis
  2003-08-05 18:39 ` Matthias Benkmann
  0 siblings, 2 replies; 92+ messages in thread
From: Richard Guenther @ 2003-08-05 13:22 UTC (permalink / raw)
  To: Matthias Benkmann; +Cc: gcc

Matthias Benkmann wrote:

> One thing that is often overlooked is that most programs written today
> (for desktop platforms, not embedded systems!) do not need inlining
> optimization AT ALL. All of the programs I've written so far perform
> adequately regardless of whether I compile them with -Os, -O1, -O2 or -O3,
> -finline-functions, -whatever. The importance of optimization is vastly
> overrated. There are very few applications for which optimization makes a
> major difference (e.g. software MPEG-decoders). And these applications are
> usually carefully examined and maintained by capable people. These people
> should have a portable way of making their own inlining decisions. These
> are the people who complain if they can't do so. These are the people who
> complain if a new GCC version has a regression in the automatic inliner.
> These are the people GCC should be optimized for.
>
> The vast majority of developers won't notice the difference either way.
> But even they will be more happy if they look at the assembly of their
> compiled program and see that the compiler has inlined the functions they
> declared inline. It gives them the reassuring feeling that they are in
> charge.

The point is, all these "people will optimize" suggestions go against
portability to different compilers.  Optimizing inlining in a portable
way ist just not possible - neither the C++ standard, nor compilers
practice of interpreting it helps you at this task.

So to say it in other words: The only way to get good performance with
regard to inlining out of "portable" C++ programs is to improve the
automatic inlining heuristics of the _compiler_. You just cannot apply the
same heuristics wrt placing the "inline" keyword for gcc on ia32 and f.i.
the C++ compiler on a Hitachi SR8000 pseudo-vector computer. This is
the world of HPC and not about micro-optimizing your mpeg player.

Richard.

--
Richard Guenther <richard dot guenther at uni-tuebingen dot de>
WWW: http://www.tat.physik.uni-tuebingen.de/~rguenth/

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-05  8:55 Richard Guenther
  2003-08-05  9:36 ` Thomas Kunert
  0 siblings, 1 reply; 92+ messages in thread
From: Richard Guenther @ 2003-08-05  8:55 UTC (permalink / raw)
  To: Thomas Kunert; +Cc: gcc

> As a C++ programmer I would like to ask you to retain a mechanism to tell
> the compiler "Inline at least everything I marked as inline." Currently,
> this mechanism is -finline-limit=1000000.
>
> Correct decisions about efficient inlining depend on many parameters the
> compiler does not know, e.g.

In general the compiler could know, at least if including profiling
feedback.

> - How much time takes one call of the function. This time in general depends
> on the actual parameters and cannot be guessed by the compiler.

For constants it knows, for others we'd have value range profiling.

>- How much total time does the code spent in the function. If this time is
> only a few micro seconds, there is absolutely no point in inlining.

No, its the other way around - if the time spent in the function is very
short, its a good candidate for inlining (because function call overhead
may be comparable to actual runtime).

>- From how many places the function is being called throughout the program.
>
> - How often the function is called from a certain position.
>
> The programmer has the complete picture. He has an idea about the actual
> parameters passed to the function. He can actually measure the time.
> He knows if some function is critical or not.

For the last two points you made the problem is, that there is no
(portable) way to tell the compiler to inline into certain positions or in
certain cases. It is only the compiler that can do this sort of context
dependend inlining decision.

> Maybe someday in the distant future compilers are smart enough to find out
> these things. But currently I strongly prefer to control inlining myself,
> because in most cases I know better. The compiler should be allowed to
> overrule my decision only if it can prove me wrong.

The C++ standard does not specify a way to tell (or hint) the compiler to
inline a certain function call. So for a large amount of cases you
absolutely _have_ to rely on the compiler to decide. Or you'll pay for at
different places.

Richard.

--
Richard Guenther <richard dot guenther at uni-tuebingen dot de>
WWW: http://www.tat.physik.uni-tuebingen.de/~rguenth/

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-04 17:50 Robert Dewar
  2003-08-04 17:52 ` Gabriel Dos Reis
  0 siblings, 1 reply; 92+ messages in thread
From: Robert Dewar @ 2003-08-04 17:50 UTC (permalink / raw)
  To: dewar, gdr; +Cc: aph, gcc

> 
> You should refrain from making such hash assertions about C++
> programmers.  "inline" like any other C++ features is documented and
> here is not the right place to transmute a meaning of an ISO
> standardized language.

They are not hash (rash?) assertions, they are simply obsrevations. I have
dealt with a LOT of C++ code written by a lot of people.

If you refer to the ISO standardized language, you are on thin ground, since
it has little to say that is meaningful in this area. 

This is not about standard conformance at all, this is about what is
pragmatically best from a code generation point of view, something
the standard is not concerned with. As far as the standard is concerned
you can do multiplication by repeated addition :-)

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-04 17:41 Robert Dewar
  0 siblings, 0 replies; 92+ messages in thread
From: Robert Dewar @ 2003-08-04 17:41 UTC (permalink / raw)
  To: dewar, jbuck; +Cc: gcc, gdr

> 
> Option 1: a version of GNU C++ that
> 
> * honors the "inline" keyword in all situations where the compiler is
>   capable of inlining the call
> * does the same for functions defined in the class body
> * when -Winline is given, issues a warning and explanation about why
>   a call is not inlined
> * avoids computing any metrics related to inlining decisions (unless -O3)
> 
> Option 2: a version of GNU C++ that uses the smartest heuristics that the
> "ignore inline" advocates can come up with.

Indeed, this is exactly the experiment that is required, and I suspect the
best resolution is to have this controllable by a switch. The issue of
what the best setting for the switch is is another discussion, and for
example depends on how well gdb handles inlining.

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-04 17:40 Robert Dewar
  2003-08-04 17:52 ` Gabriel Dos Reis
  2003-08-08 21:21 ` Marc Espie
  0 siblings, 2 replies; 92+ messages in thread
From: Robert Dewar @ 2003-08-04 17:40 UTC (permalink / raw)
  To: dewar, gdr; +Cc: aph, gcc, jbuck

> Inline as I have described is close to what has been documented for
> two decades in official C++ manuals.

Well if you really believe that C++ programmers read "official C++
manuals" then you are talking about a different population of programmers
entirely.

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-04 17:39 Robert Dewar
  2003-08-04 17:51 ` Gabriel Dos Reis
  0 siblings, 1 reply; 92+ messages in thread
From: Robert Dewar @ 2003-08-04 17:39 UTC (permalink / raw)
  To: dewar, gdr; +Cc: aph, gcc

> Again, this is an issue about C++ meaning of inline.  Not Ada's.

That's just plain wrong. mY comment:

> | Well I prefer to rely on my experience (which is that I have seen a lot
> | of cases in which users are surprised to find that inlining slows things
> | down), than your belief :-)

This comment is about inlining in general. It is not about Ada or about C++.
A lot of programmers have a pretty naive view of machine language, if they
have any view at all. For many programmers, the view of inlining is that
it must save time because it saves the time of the call and return. This
is of course entirely naive wrt super scalar machines with small icaches.

So you are misinterpreting the scope of my comment completely.

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-04 17:35 Robert Dewar
  2003-08-04 17:44 ` Gabriel Dos Reis
  2003-08-05 16:58 ` Scott Robert Ladd
  0 siblings, 2 replies; 92+ messages in thread
From: Robert Dewar @ 2003-08-04 17:35 UTC (permalink / raw)
  To: aph, dewar; +Cc: gcc

> People are looking at this problem from two different angles:
> 
> a.  What will, on average, give the best performance?
> b.  What will, on average, cause the least trouble?
> 
> Andrew.

Right, those two angles always have to be considered. Note that the decision
in gcc to default to -O0 clearly is more concerned with consideration b than
a. This is probably reasonable, although it often costs gcc in benchmarks
against other compilers where optimization is the default (we have seen
behcnmarking protocols which require the default switches to be used, yes
that's silly, but it happens!)

> 
> "I do not believe that would be a surprise to anyone who thought about
> it for a minute."

You are assuming a model of someone who understands the possible effects
of inlining and knows what an instruction cache is and how it works. The
readership and writership of this list is hardly a representative cross-
section of "in the trenches" C++ programmers, who in my experience have
very little awarness of what is going on at the machine level.

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-04 17:33 Robert Dewar
  2003-08-04 17:34 ` Gabriel Dos Reis
  0 siblings, 1 reply; 92+ messages in thread
From: Robert Dewar @ 2003-08-04 17:33 UTC (permalink / raw)
  To: dewar, gdr; +Cc: aph, gcc, jbuck

> C++ is not Ada and Ada is not C++.  
> If you want to argue about C++ inline, please do consider C++
> semantics, practice. 

My comment was on C++ practice as I see it in the real world.

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-04 17:15 Robert Dewar
  2003-08-04 17:26 ` Andrew Haley
  2003-08-04 17:28 ` Gabriel Dos Reis
  0 siblings, 2 replies; 92+ messages in thread
From: Robert Dewar @ 2003-08-04 17:15 UTC (permalink / raw)
  To: aph, dewar; +Cc: gcc

> I do not believe that would be a surprise to anyone who thought about
> it for a minute.


Well I prefer to rely on my experience (which is that I have seen a lot
of cases in which users are surprised to find that inlining slows things
down), than your belief :-)

Again, what is really needed is data. We have lots of data on this issue
in the Ada world, surely there are useful examples in the C++ world that
could argue the case more effectively than people stating what they
believe to be the case?

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-04 17:11 Robert Dewar
  2003-08-04 17:22 ` Gabriel Dos Reis
  0 siblings, 1 reply; 92+ messages in thread
From: Robert Dewar @ 2003-08-04 17:11 UTC (permalink / raw)
  To: dewar, jbuck; +Cc: aph, gcc

> For most targets, the only difference between -O2 and -O3 is that, with
> the latter, we find more functions to inline, beyond those explicitly
> marked as inline.  I read Gaby's argument as describing the behavior of
> -O2, not of -O3 (for which we will still need heuristics to drive inlining
> decisions).

Well of course the term "explicitly marked" is a bit contentious in this 
thread :-)

The point I was making is that indeed excessive inlining can be harmful.

In C++ the practice of writing simple functions in the header is not just
a matter of the programmer "requesting inline explicitly", but also it
is just the more convenient way of doing things, and doing otherwise may
be undesirable for structural and documentation purposes.

So to think that a C++ programmer is always conciously expecting and 
requesting inline when the keyword is not explicitly used seems a bit of
a dubious claim to me.

Certainly the most convincing evidence would be specific examples of
real applications compiled and run both ways. This thread seems short on
such examples.

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-04 16:50 Robert Dewar
  2003-08-04 17:02 ` Joe Buck
                   ` (3 more replies)
  0 siblings, 4 replies; 92+ messages in thread
From: Robert Dewar @ 2003-08-04 16:50 UTC (permalink / raw)
  To: aph, dewar; +Cc: gcc

> Perhaps there is, but the Principle of Least Surprise favours a
> fairly literal interpretation of "inline".

I don't agree, a huge blow up in size, resulting in slower execution because
of icache overload, can also qualify as a surprise.

Indeed we find that customers are quite often surprised to find that -O3
is slower than -O2 (as well as generating lots of code).

^ permalink raw reply	[flat|nested] 92+ messages in thread
* Re: On inlining in C++
@ 2003-08-04 16:38 Robert Dewar
  2003-08-04 16:46 ` Andrew Haley
                   ` (2 more replies)
  0 siblings, 3 replies; 92+ messages in thread
From: Robert Dewar @ 2003-08-04 16:38 UTC (permalink / raw)
  To: gdr, jbuck; +Cc: gcc

> Excellent article.  I think that anyone who wishes to oppose the argument
> should be asked to demonstrate that we can get better quality of results
> by deviating from the simple approach Gaby describes; philosophical
> arguments should not suffice.

Well there is a burden of demonstration on both sides I would say, in terms
of showing behavior on actual code.

^ permalink raw reply	[flat|nested] 92+ messages in thread
* On inlining in C++
@ 2003-08-04 13:51 Gabriel Dos Reis
  2003-08-04 16:36 ` Joe Buck
  2003-08-05 19:10 ` Matthias Benkmann
  0 siblings, 2 replies; 92+ messages in thread
From: Gabriel Dos Reis @ 2003-08-04 13:51 UTC (permalink / raw)
  To: gcc


This text I should have sent a while ago.  


	  Give C++ "inline" its original and obvious meaning

	     Gabriel Dos Reis <gdr@integrable-solutions>
				=====

          "Inlining was considered important for the utility of classes"
                    -- B. Stroustrup in "The Design an Evolution of C++"


  This note is about handling of "inline" by C++ implementations.
It has grown up after various misfortunate experience with
different C++ implementations, reading reports and complaints from C++
users; it is also a reaction to the more or less recent semantics
shift implementors are trying to put in the "inline" keyword.  While
this paper uses examples based on GCC/g++ behaviour to illustrate some
points, I believe that most of the issues are not unique to GCC. 

A cost-free functional abstraction
----------------------------------

One of the principal uses of inline functions is to offer safe,
cost-free functional access to small, simple expressions.  Consider:

   inline int min(int a, int b)
   { 
      return a > b ? b : a; 
   }

Call to abstractions like min() should not incur function call overhead.
A function like min() is just a convenient notation for the simple
expression it contains.  

Is there an alternative way to express that notation?   Often, people
in fear, doubt and uncertainty of their compilers not substituting an
inline function body at call sites resort to macros:

   #define MIN(A, B)   ((A) > (B) ? (B) : (A))

However, that is only a very rough approximation: The macro MIN does
suffer from several serious problems:
  * It does break scope rules and it cannot be passed around. 
  * It does break function call semantics - it evaluates its arguments
    more than once.  
In other words, that macro looses both semantics and syntax of a function.
That loss is a show-stopper as far as programming in C++ goes.  

A compiler targetting a given language should implement the semantics
and support usage patterns of that language.   For example, a C++
compiler should inline a call like min(2003-20, 49 * 36) yielding something
like 
   
       (2003-20) > (49 * 36)? (49 * 36) : (2003-20)

which a front-end or back-end optimization should reduce to plain
1764. Even the first C++ compiler, Cfront, could do that in 1985.
Interestingly, the GCC manual[1] (as of this writing) describes inline
functions as follows: 

	       An Inline Function is As Fast as a Macro

    By declaring a function inline, you can direct GCC to integrate
    that function's code into the code for its callers. This makes
    execution faster by eliminating the function-call overhead; in
    addition, if any of the actual argument values are constant, their
    known values may permit simplifications at compile time so that not
    all of the inline function's code needs to be included. 

The interesting point about that documentation is that it is close to
the documented meaning of "inline" found in any official C++ manual
since 1981[2]. 

Crossing protection barrier ought to be free
--------------------------------------------

Another important use of inline function is for providing controled
access to protected data. In fact, such uses were the decisive factors
for introducing inline function in C++:

   In particular, [Stroustrup, 1982b] observes that people had made
   data members public to avoid the function call overhead incurred by
   a constructor for simple classes where only one or two assignments
   are needed for initialization.  The immediate cause for the
   inclusion of inline functions into C with Classes was a project
   that couldn't afford function call overhead for some classes
   involved in real-time processing.  For classes to be useful in that
   applications, crossing the protection barrier had to be free.
                 -- B. Stroustrup in "The Design and Evolution of C++" 

Those words were written more than 20 years ago, but they do sound so
familiar, so contemporary.  Access control is a compile-time notion.
A controled access to protected data ought to be free.  

It is crucial to realize that "inline" in C++ does not mean "leave
this to the back-end".  It has a language specific meaning. Inline is
in C++ exactly because the programmer cannot rely on a compiler
reliably inline predictably and reasonably according to the
programmer's needs for a particular program. Even if one compiler does
it right, reliance on a compiler's inlining heuristics 	will not lead
to portable  behavior vis a vis the performance.  Inlining was
introduced as a language feature into C++ exactly to give the
programmer control, a lever to state his preference for an alternate
function call mechanism, namely <it>substitution</it>. An advantage of
substitution is that once inlined, the function body can expose many
optimization opportunities that otherwise would necessitate more
advanced infrastructures. 

More than twenty years after its introduction in C++, do major and
common C++ compilers get "inline" right?  I fear the answer is
<strong>no</strong> for many of them.  Failures to implement the
meaning of "inline" and to support usage patterns has been incitative
for otherwise talented  programmers to deploy all sorts of
workarounds, going against good software practice, defeating the 
intended semantics of common abstractions[3].  Simple abstractions like

  template<class T>
    inline const T& min(const T& a, const T& b)
    {
       return a > b ? b : a;
    }

or std::string::end() should be efficiently handled by C++ compilers.
Simple inline functions should be cost-free.  Functions that provide
controled access to data should be as efficient as accessing the data
directly.  Whatever strategy C++ implementors use to implement "inline",
the delivered behaviour should meet the documented obvious and
original meaning of "inline". 


Is "inline" just like "register"?
---------------------------------

It is not uncommon to see the argument that "inline" is just a
<em>hint</em>, like "register", and as such the compiler should treat
them equally, i.e. ignore the C++ programmer preference because the
compiler knows best.  

Such an argument is confusing.  There is no dispute that "inline" is a
hint.  But it is a hint only because, quoting again [4], p. 34:

   This is a logical necessity because one can write inline functions
   that cannot at compile time be proven not to cause infinite
   recursions;  trying to inline one of those would lead to infinite
   compilations.  Leaving "inline" a hint is also a practical
   advantage because it allows the compiler writer to handle
   "pathological" by simply not inlining. 

Another way Stroustrup used to put the logical necessity is: "we don't
want to require the compiler to solve the halting problem".  Of course,
"pathological" is not clearly defined, but it is matter of fact that no
simple, short expression is pathogolical.  In the very early days,
a loop in a function called in a way that required use of its return
value to be used was considered pathological, but most compilers can
do better today and this case can be very important because inlining
in this case opens possibilities for optimization. 

Furthermore, it is not true that in real code the compiler often knows
better than the programmer what to inline.  Compiler heuristics don't
serve all people well[5] and "inline" was introduced into C++ exactly to
give the programmer control.  The current state of compiler technology
and development is not at the same level of sophistication as that of
automatic register allocation.  Given the control offered by a simple
and straightforward implementation of "inline", good programmers
consistently produce well performing code.  Furthermore, compiler
technology is still far from the maturity level where a language
independent meaning of "inline" could be productively substituted for
the language specific meaning it has in C++.  When compilers are as
good at inlining as they are at register allocation, "inline" can go
the way of "register".  However, we are nowhere near that level of
sophistication yet, so we should - in the best C and C++ tradition -
trust the programmer. 

Failure to meet behaviour that have been documented for more than two
decades will only increase dialect proliferations, because the
fundamental need will still be there, and people will continue to
reinvent "inline" with vendor lock-in syntaxes.  Compiler middle ends
and back ends are not the right places to change an ISO standardized
language. 


Should an Inline Function returning a structure make a difference?
------------------------------------------------------------------

No, an inline function returning a structure or class should not make
any difference.  For that, it is crucial to understand the language
specific meaning of inlining in C++: It is substitution of the
function body for the usual call.  In other words, it is an alternate
implementation of function call mechanism.  Compiler back-ends that
insist on  using a language-neutral meaning for C++ inline fail to
understand that feature.  Dare I say, they fail to support C++.

Even more so, it is essential that an inline function returning a
structure that contains a scalar be as efficiently supported as an
inline function returning just that scalar.  The reason is that
liberal use of types is central for programming in C++, and most of
the types involved in most C++ programs are user-defined types,
i.e. structures or classes.  


Acknowledgments
---------------

The writing of this paper benefited from support, inputs and
improvements from friends I would like to thank.  Bjarne Stroustrup
patiently corrected errors and confusing formulations contained
in the drafts of this note.  He also provided assistance for history.
Paolo Carlini made careful reading, asking for better clarifications
and explanations.  Benjamin Kosnik provided data through his continual
exercising, improvement and testing for a "-Winline"-clean GNU
implementation of the C++ standard library.  Any remaining error, 
inaccuracy are my responsability.  


[Stroustrup, 1982b] Bjarne Stroustrup: "Adding Classes to C: An
Exercise in Language Evolution".  Bell Laboratories Computer Science
internal document.  April 1982.  Software: Practice & Experience,
Vol 13, 1983.

Footnotes: 
[1] GCC 3.3 Manual 
    http://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Inline.html#Inline

[2] Bjarne Stroustrup, private communication.

[3] See patches included in the message
    http://gcc.gnu.org/ml/libstdc++/2003-02/msg00102.html 

[4] Bjarne Stroustrup: "The Design and Evolution of C++", 1994 Addison-Wesly.
 
[5] http://gcc.gnu.org/ml/gcc/2001-08/msg01206.html

^ permalink raw reply	[flat|nested] 92+ messages in thread

end of thread, other threads:[~2003-12-15 18:29 UTC | newest]

Thread overview: 92+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-08-05 18:13 On inlining in C++ Robert Dewar
2003-08-05 18:17 ` Gabriel Dos Reis
2003-08-05 19:11   ` Geoff Keating
2003-08-06  2:15     ` Joe Buck
2003-08-06  2:31       ` Andrew Pinski
2003-08-06  2:47         ` Joe Buck
2003-12-15 18:49       ` Diego Novillo
2003-08-06 10:10     ` Richard Earnshaw
2003-08-06 11:51       ` Gabriel Dos Reis
2003-08-06 11:51         ` Richard Earnshaw
2003-08-06 18:06           ` Joe Buck
  -- strict thread matches above, loose matches on Subject: below --
2003-08-06 21:37 Robert Dewar
2003-08-06 22:14 ` Joe Buck
2003-08-06  4:31 Robert Dewar
2003-08-06  5:13 ` Andreas Jaeger
2003-08-06  9:07 ` Steven Bosscher
2003-08-06 16:51 ` Joe Buck
2003-08-05 17:28 Robert Dewar
2003-08-05 17:49 ` Gabriel Dos Reis
2003-08-05 17:06 Robert Dewar
2003-08-05 17:26 ` Gabriel Dos Reis
2003-08-05 17:57 ` Scott Robert Ladd
2003-08-06 19:49   ` Alexandre Oliva
2003-08-05 16:23 Robert Dewar
2003-08-05 16:22 Robert Dewar
2003-08-05 16:15 Robert Dewar
2003-08-05 13:22 Richard Guenther
2003-08-05 13:25 ` Gabriel Dos Reis
2003-08-05 13:29   ` Richard Guenther
2003-08-05 13:39     ` Gabriel Dos Reis
2003-08-05 14:49       ` Richard Guenther
2003-08-05 14:51         ` Gabriel Dos Reis
2003-08-05 18:39 ` Matthias Benkmann
2003-08-05 19:42   ` Steven Bosscher
2003-08-05 19:53     ` Gabriel Dos Reis
2003-08-06  2:19   ` Joe Buck
2003-08-05  8:55 Richard Guenther
2003-08-05  9:36 ` Thomas Kunert
2003-08-05  9:57   ` Richard Guenther
2003-08-05 10:06     ` Gabriel Dos Reis
2003-08-05 11:45       ` Richard Guenther
2003-08-04 17:50 Robert Dewar
2003-08-04 17:52 ` Gabriel Dos Reis
2003-08-04 17:41 Robert Dewar
2003-08-04 17:40 Robert Dewar
2003-08-04 17:52 ` Gabriel Dos Reis
2003-08-04 17:59   ` Daniel Jacobowitz
2003-08-04 18:09     ` Gabriel Dos Reis
2003-08-08 21:21 ` Marc Espie
2003-08-08 23:12   ` Kevin Atkinson
2003-08-04 17:39 Robert Dewar
2003-08-04 17:51 ` Gabriel Dos Reis
2003-08-04 17:35 Robert Dewar
2003-08-04 17:44 ` Gabriel Dos Reis
2003-08-05 16:58 ` Scott Robert Ladd
2003-08-04 17:33 Robert Dewar
2003-08-04 17:34 ` Gabriel Dos Reis
2003-08-04 17:15 Robert Dewar
2003-08-04 17:26 ` Andrew Haley
2003-08-04 17:28 ` Gabriel Dos Reis
2003-08-04 17:11 Robert Dewar
2003-08-04 17:22 ` Gabriel Dos Reis
2003-08-04 16:50 Robert Dewar
2003-08-04 17:02 ` Joe Buck
2003-08-04 17:05 ` Andrew Haley
2003-08-04 18:30 ` Bernd Schmidt
2003-08-26 15:11 ` Nick Ing-Simmons
2003-08-04 16:38 Robert Dewar
2003-08-04 16:46 ` Andrew Haley
2003-08-04 17:35 ` Joe Buck
2003-08-04 17:54   ` Daniel Berlin
2003-08-04 19:01   ` Geoff Keating
2003-08-04 19:22     ` Joe Buck
2003-08-05  8:26 ` Richard Sandiford
2003-08-05  9:24   ` Steven Bosscher
2003-08-05 12:23   ` Matthias Benkmann
2003-08-04 13:51 Gabriel Dos Reis
2003-08-04 16:36 ` Joe Buck
2003-08-05  7:37   ` Mike Stump
2003-08-05  7:44     ` Thomas Kunert
2003-08-05  8:55     ` Gabriel Dos Reis
2003-08-05 19:33       ` Mike Stump
2003-08-05 19:46         ` Gabriel Dos Reis
2003-08-06  0:57           ` Mike Stump
2003-08-06  2:02             ` Joe Buck
2003-08-06  2:20               ` Scott Robert Ladd
2003-08-05 21:12         ` Scott Robert Ladd
2003-08-05 21:12         ` Gabriel Dos Reis
2003-08-06  2:00     ` Joe Buck
2003-08-06  9:30       ` Gerald Pfeifer
2003-08-06 13:57         ` Gabriel Dos Reis
2003-08-05 19:10 ` Matthias Benkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).