templates and (inline) optimization

L

leo

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.
 
K

Kanenas

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top