Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   templates and (inline) optimization (http://www.velocityreviews.com/forums/t290944-templates-and-inline-optimization.html)

leo 05-10-2005 10:39 AM

templates and (inline) optimization
 
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.


Kanenas 05-18-2005 08:41 PM

Re: templates and (inline) optimization
 
On 10 May 2005 03:39:24 -0700, "leo" <leonorfm@wanadoo.es> 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


All times are GMT. The time now is 09:57 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.