Inline Performance impact

Discussion in 'C++' started by GJ, Oct 18, 2005.

  1. GJ

    GJ Guest

    The performance can be DEGRADED by making a function "inline". Is it TRUE in
    any case ?

    --
    GJ
     
    GJ, Oct 18, 2005
    #1
    1. Advertising

  2. GJ

    Rolf Magnus Guest

    GJ wrote:

    > The performance can be DEGRADED by making a function "inline". Is it TRUE
    > in any case ?


    Yes.
     
    Rolf Magnus, Oct 18, 2005
    #2
    1. Advertising

  3. GJ

    GJ Guest

    "Rolf Magnus" <> wrote in message
    news:dj2frb$6lg$05$-online.com...
    > GJ wrote:
    >
    > > The performance can be DEGRADED by making a function "inline". Is it

    TRUE
    > > in any case ?

    >
    > Yes.
    >

    When I mean what scenario ?
     
    GJ, Oct 18, 2005
    #3
  4. GJ

    Guest

    I could think of:
    Huge inlined function, used (called) from many places
    -> program gets huge -> hooks up all memory and the
    machine starts to swap.

    Cheers,
    marc
     
    , Oct 18, 2005
    #4
  5. GJ

    Guest

    Rolf Magnus wrote:
    > GJ wrote:
    >
    > > The performance can be DEGRADED by making a function "inline". Is it TRUE
    > > in any case ?

    >
    > Yes.

    Is that the same story on template code, where the implementation is
    resident in a .hh file (or a .h file).

    class whatever {
    public:
    template<typename T>
    void some_method(T& var) {}
    };

    If memory serves though, I think implementations in a header file is
    already inlined?
     
    , Oct 18, 2005
    #5
  6. GJ

    Rolf Magnus Guest

    GJ wrote:

    >> > The performance can be DEGRADED by making a function "inline". Is it

    > TRUE
    >> > in any case ?

    >>
    >> Yes.
    >>

    > When I mean what scenario ?


    If the function gets called from many places, too much inlining can increase
    code size significantly, which will lower the cache efficiency on systems
    that do use caching (which is the majority these days). On modern
    workstation-like computers, the cache is extremely important for fast code
    execution. Reading a value from RAM can take several hundred clock cycles.
    Similarly, if there is conditional code or a loop in the inline function,
    the branch prediction unit gets stressed more.
    Unfortunatly, this is not an effect that you can simply measure on a per
    function basis to find out whether inlining it is faster or not. It's more
    like a tendency, and it likely depends on the CPU model, even for different
    CPUs of the same architecture.
     
    Rolf Magnus, Oct 18, 2005
    #6
  7. GJ

    ben Guest

    > Is that the same story on template code, where the implementation is
    > resident in a .hh file (or a .h file).
    >
    > class whatever {
    > public:
    > template<typename T>
    > void some_method(T& var) {}
    > };
    >
    > If memory serves though, I think implementations in a header file is
    > already inlined?
    >


    They are, in fact, candidate for inlining. There are lots of functions
    that are simply impossible to be inlined.

    Even the 'inline' keyword would not guarantee the function to be
    actually inlined. The compiler does a great job in deciding whether or
    not inlining such a function would give you a performance gain.

    Inlining is an optimization technique because it allows more thorough
    data flow analysis across caller/callee. Where there's not much to be
    optimized from caller/callee data flow analysis, there's not much to be
    optimized from inlining, simply.

    Ben
     
    ben, Oct 18, 2005
    #7
  8. GJ

    Greg Comeau Guest

    In article <4354fdff$0$20333$>,
    ben <> wrote:
    >> If memory serves though, I think implementations in a header file is
    >> already inlined?

    >
    >They are, in fact, candidate for inlining. There are lots of functions
    >that are simply impossible to be inlined.


    I think the OP was referring to implicitly inlines, which happen
    in classes_ not in headers per se. But OP, he's right: just having
    a function in a header does not automatically have it being inline'd.
    Ditto for using the inline keyword is not the sole determination
    for inline'ing nor is its absence (implicitly or otherwise)
    the sole determination for not inline'ing. In the end it's
    a hint and the compiler can get along w/o it, or use the advise.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Oct 18, 2005
    #8
  9. Rolf Magnus wrote:

    > If the function gets called from many places, too much inlining can increase
    > code size significantly, which will lower the cache efficiency on systems
    > that do use caching (which is the majority these days).
    > On modern workstation-like computers,
    > the cache is extremely important for fast code execution.
    > Reading a value from RAM can take several hundred clock cycles.
    > Similarly, if there is conditional code or a loop in the inline function,
    > the branch prediction unit gets stressed more.
    > Unfortunatly, this is not an effect that you can simply measure on a per
    > function basis to find out whether inlining it is faster or not.
    > It's more like a tendency, and it likely depends on the CPU model,
    > even for different CPUs of the same architecture.


    This is pure nonsense.
    You can't contrive an example that will show this.
     
    E. Robert Tisdale, Oct 18, 2005
    #9
  10. GJ

    Greg Guest

    wrote:
    > Rolf Magnus wrote:
    > > GJ wrote:
    > >
    > > > The performance can be DEGRADED by making a function "inline". Is it TRUE
    > > > in any case ?

    > >
    > > Yes.

    > Is that the same story on template code, where the implementation is
    > resident in a .hh file (or a .h file).
    >
    > class whatever {
    > public:
    > template<typename T>
    > void some_method(T& var) {}
    > };
    >
    > If memory serves though, I think implementations in a header file is
    > already inlined?


    If the implementation of a class method appears within a class
    declaration yes it has been declared inline.

    Function templates are not declared inline merely by virtue of being
    defined in a header file. It is still necessary to specify the inline
    keyword if the intent is to declare a function template inline.

    Greg
     
    Greg, Oct 18, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Cadillac
    Replies:
    0
    Views:
    354
    Scott Cadillac
    Oct 25, 2003
  2. Aaron Anodide
    Replies:
    9
    Views:
    464
    Aaron Anodide
    Jul 30, 2003
  3. Andrew James
    Replies:
    4
    Views:
    369
    Nick Coghlan
    Jan 15, 2005
  4. vinjvinj
    Replies:
    13
    Views:
    539
    Peter Otten
    Mar 11, 2006
  5. Rich
    Replies:
    6
    Views:
    361
    Tim Mackey
    Jan 24, 2007
Loading...

Share This Page