templates and (inline) optimization

Discussion in 'C++' started by leo, May 10, 2005.

  1. leo

    leo Guest

    I had the believe that the use of templates didn't affect performance.
    That is, that from the performance point of view it's the same:

    a)
    template <class X>
    class Y{
    X attrib;

    ...
    };

    Y<int> myclass;

    b)
    class Y{
    int attrib;


    ..
    };

    Y myclass;

    However, some weeks ago I discovered that this is not exactly true, at
    least for gcc 3.2. Specifically, I had one class of the type b) and I
    wanted to converted to the type a) so as that the resulting X class
    could have an attrib of any type and not only int. And then,
    surprisingly for me, the program turned out to slow down considerably.
    I took a look at some basic profiling data and I saw that now some
    previously effectively inlined functions where not anymore... (and I
    did'nt make any further modification in the code).

    I would appreciate any help on this. I would like to know if this is a
    limitation of the gcc 3.2 compiler, and then, if this is solved in 4.0
    version, or it's a general limitation... and if there is some
    workaround more elegant than using macros.

    Thank you very much!

    Leo.
     
    leo, May 10, 2005
    #1
    1. Advertising

  2. leo

    Kanenas Guest

    On 10 May 2005 03:39:24 -0700, "leo" <> wrote:

    >I had the believe that the use of templates didn't affect performance.
    >That is, that from the performance point of view it's the same:

    [...]
    >
    >However, some weeks ago I discovered that this is not exactly true, at
    >least for gcc 3.2. Specifically, I had one class of the type b) and I
    >wanted to converted to the type a) so as that the resulting X class
    >could have an attrib of any type and not only int. And then,
    >surprisingly for me, the program turned out to slow down considerably.
    >I took a look at some basic profiling data and I saw that now some
    >previously effectively inlined functions where not anymore... (and I
    >did'nt make any further modification in the code).
    >

    Without a complete, concise example it's hard to say for sure what's
    happening; do you have such sample code?

    Here's a not-too-probable cause: if the function definition is not
    visible when the function is called, the function won't be inlined.
    As the functions were inlined before you templatized the class, this
    probably isn't the cause.

    It's also possible that something else causes the slowdown while
    turning on profiling disables inlining (try google or gnu.g++.help to
    check whether profiling will disable inlining).

    >I would appreciate any help on this. I would like to know if this is a
    >limitation of the gcc 3.2 compiler, and then, if this is solved in 4.0
    >version, or it's a general limitation... and if there is some
    >workaround more elegant than using macros.
    >

    The gang at gnu.g++.help would know more about gcc's limitations.
    Offhand I can't think of any restriction in C++ which would prevent a
    function from being inlined merely by templatizing it.

    As for workarounds, doubtful. In-language, inlining code is achieved
    solely through in-class definition or use of the 'inline' keyword and,
    in any case, is only a suggestion to the compiler; if the compiler
    won't inline template functions, that will be a sticking point.
    Pre-language you have macros (as you noted).

    Post a complete example and we can do more.

    Kanenas
     
    Kanenas, May 18, 2005
    #2
    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. JKop
    Replies:
    3
    Views:
    492
  2. Sean
    Replies:
    4
    Views:
    679
    Rolf Magnus
    Apr 30, 2006
  3. recover
    Replies:
    2
    Views:
    831
    recover
    Jul 25, 2006
  4. Rahul
    Replies:
    3
    Views:
    463
    James Kanze
    Feb 28, 2008
  5. Ravikiran

    Zero Optimization and Sign Optimization???

    Ravikiran, Nov 17, 2008, in forum: C Programming
    Replies:
    22
    Views:
    891
    Thad Smith
    Nov 24, 2008
Loading...

Share This Page