Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > templates and (inline) optimization

Reply
Thread Tools

templates and (inline) optimization

 
 
leo
Guest
Posts: n/a
 
      05-10-2005
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.

 
Reply With Quote
 
 
 
 
Kanenas
Guest
Posts: n/a
 
      05-18-2005
On 10 May 2005 03:39:24 -0700, "leo" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Zero Optimization and Sign Optimization??? Ravikiran C Programming 22 11-24-2008 03:19 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Templates: "implicit typename is deprecated" error and typedef'ing templates Generic Usenet Account C++ 3 07-14-2005 08:02 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
Re: return value optimization on expression/meta templates E. Robert Tisdale C++ 0 06-25-2003 04:54 AM



Advertisments