Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

about inline function

6 views
Skip to first unread message

blackbiscuit

unread,
Nov 26, 2009, 1:08:31 AM11/26/09
to
Dear all,

May I ask how to see whether the compiler has really inlined my
functions?

We usually use "inline" keyword to define a inline function or just
define it in a class declaration. But whether it is inlined is
depended by the compiler. There should be some criterions which are
various from different compilers.

If I define a inline function in my source code, how can I know it is
really inlined by the compiler?

Thanks very much.

Best wishes,
Tony

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

Martin B.

unread,
Nov 26, 2009, 11:23:43 AM11/26/09
to
blackbiscuit wrote:
> Dear all,
>
> May I ask how to see whether the compiler has really inlined my
> functions?
>
> We usually use "inline" keyword to define a inline function or just
> define it in a class declaration. But whether it is inlined is
> depended by the compiler. There should be some criterions which are
> various from different compilers.
>
> If I define a inline function in my source code, how can I know it is
> really inlined by the compiler?
>
> Thanks very much.
>

The only way I know to make sure is to check the generated assembly.

On Visual Studio, there is the optional warning C4714 that will tell you
if a function with __forceinline has not been inlined and then there is
another warning C4710 that will tell you if a function has not been
inlined although it "was selected for inline expansion" - whatever that
means exactly. (see: http://msdn.microsoft.com/en-us/library/cfahxw6k.aspx)

So I can see two points:
1.) If you "just" want to know, check the assembly.
2.) If you have-to-make-sure that something non-trivial is inlined the
*only* way is to write a preprocessor macro. (Whether it is ever
warranted to force anything inline is of course another story.)

br,
Martin

Ulrich Eckhardt

unread,
Nov 26, 2009, 11:24:10 AM11/26/09
to
blackbiscuit wrote:
> May I ask how to see whether the compiler has really inlined my
> functions?

Look at the generated machine code.

> We usually use "inline" keyword to define a inline function or just
> define it in a class declaration. But whether it is inlined is
> depended by the compiler. There should be some criterions which are
> various from different compilers.
>
> If I define a inline function in my source code, how can I know it is
> really inlined by the compiler?

Two things:
1. The C++ Standard 'inline' keyword
'inline' only has one guaranteed meaning, that multiple definitions in
different translation units don't cause errors. If you put an inline
function into a header file used in two translation units and it links
without errors, the compiler did its job.
2. The machine code inlining
This basically means that the function call overhead was elided by replacing
it with the whole body of the called function. The compiler is free to do
this as it sees fit, as long as it doesn't change the program.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Goran

unread,
Nov 26, 2009, 11:29:28 AM11/26/09
to
On Nov 26, 7:08 am, blackbiscuit <infozyzh...@gmail.com> wrote:
> Dear all,
>
> May I ask how to see whether the compiler has really inlined my
> functions?
>
> We usually use "inline" keyword to define a inline function or just
> define it in a class declaration. But whether it is inlined is
> depended by the compiler. There should be some criterions which are
> various from different compilers.
>
> If I define a inline function in my source code, how can I know it is
> really inlined by the compiler?

The best shot by far: look at the disassembly. ;-)

In general, compiler has a final say at "inline". Inline is a mere
hint you can give to it because you think a particular function should
be inlined. But in my experience, compiler knows better in some cases,
and in others, it doesn't know how to inline (see a recent post on
that in this very group). On top of that, compiler will inline stuff
you didn't mark as inline if it sees fit, and will do a good job off
it.

Inline mattered more in the old days when compiler were less smart.

In a way, I don't want to even __know__ about inline, not until I
decide, through measurement, that I should, and could, do better.
Until then, I just leave it to the compiler. It really knows better
than I, especially given the time I have to invest into that.

Goran.

restor

unread,
Nov 26, 2009, 11:31:55 AM11/26/09
to
> If I define a inline function in my source code, how can I know it is
> really inlined by the compiler?

Once your program is compiled, you open the binary executable/library
and inspect the assembler instructions. How you do it is highly
dependent on your platform.

Regards,
&rzej

CornedBee

unread,
Nov 26, 2009, 11:31:37 AM11/26/09
to
On Nov 26, 7:08 am, blackbiscuit <infozyzh...@gmail.com> wrote:
>
> May I ask how to see whether the compiler has really inlined my
> functions?
>
> We usually use "inline" keyword to define a inline function or just
> define it in a class declaration. But whether it is inlined is
> depended by the compiler. There should be some criterions which are
> various from different compilers.
>
> If I define a inline function in my source code, how can I know it is
> really inlined by the compiler?

You can read your compiler's documentation to see if it has an option
that enables such informational output. For example, GCC has the
option -Winline that causes it to emit a warning whenever it decides
not to inline a function that is actually declared inline.

Last resort, you can set your compiler to emit assembly and inspect
it, but in that case you really have to consider whether you need to
know that badly.

Sebastian

Francis Glassborow

unread,
Nov 27, 2009, 8:37:16 AM11/27/09
to
Ulrich Eckhardt wrote:
> blackbiscuit wrote:
>> May I ask how to see whether the compiler has really inlined my
>> functions?
>
> Look at the generated machine code.
>
>> We usually use "inline" keyword to define a inline function or just
>> define it in a class declaration. But whether it is inlined is
>> depended by the compiler. There should be some criterions which are
>> various from different compilers.
>>
>> If I define a inline function in my source code, how can I know it is
>> really inlined by the compiler?
>
> Two things:
> 1. The C++ Standard 'inline' keyword
> 'inline' only has one guaranteed meaning, that multiple definitions in
> different translation units don't cause errors. If you put an inline
> function into a header file used in two translation units and it links
> without errors, the compiler did its job.
> 2. The machine code inlining
> This basically means that the function call overhead was elided by replacing
> it with the whole body of the called function. The compiler is free to do
> this as it sees fit, as long as it doesn't change the program.
>
> Uli
>

Including times when the programmer has not asked for it. Modern
compilers are becoming increasingly sophisticated at doing this using
whole program analysis.

The common cases for inlining are where a function is small and has been
declared as static. The compiler knows that such a function cannot be
referenced outside the current TU (unless its address has been taken and
passed to a function defined outside the TU.

--

0 new messages