[Exceptional C++ Style] Exception C++ Style - Item 25: inline Redux.

Terje Slettebø tslettebo at broadpark.no
Fri Jan 28 03:52:48 EST 2005


>From: <Atul_Khot at i2.com>

>            Inlining is a request to the compiler. A compiler can rightly
> ignore this request sometimes
> and honour it other times. Why this is so? Because compilers typically
have
> more context to decide
> whether inlining will really help or will slow down things. Better
equipped
> with this knowledge, compilers
> will do the right thing. We programmers don't have this contextual
> knowledge at coding time ( for example
> I cannot know if some function, after applying optimizations would be too
> large to fit into cache ).

My experience is that compilers will typically honour the request to inline,
even if it may make the code slower/bigger (and even if the optimisation
settings are set to optimise for smaller/faster, respectively). However,
some compilers (such as MSVC), have a setting to allow it to "inline any
suitable function", and in those cases, it must perform some heuristics to
determine which function (call) will benefit.

Besides, since member functions defined in the class are implicit inline,
and since many libraries do this (like much of Boost), I guess the compiler
should use heuristics in this case, as well, to avoid spectacular bloat...

>                   So the lesson is the later inlining is performed the
better it would be.

Ahem. Not unconditionally, I think. Sure, it has more information to make
the decision at that point, but e.g. link-time inlining is _not_ equivalent
to compile-time inlining. With compile-time inlining, inlining the function
call leads to longer stretches of code available to the optimiser, and this
may result in a more dramatic improvement than simply the inlining itself.

> And it is better done by the tools than us.

That I would tend to agree with. :)

Regards,

Terje




More information about the Effective-cpp mailing list