Re: [Boost-users] Question about template optimization
Date: Wed, 18 May 2005 17:32:49 -0700 From: "Elisha Berns"
Subject: [Boost-users] Question about template optimization To: "Boost" Message-ID: <000001c55c0a$4966cf30$651aa8c0@INSPIRATION> Content-Type: text/plain; charset="us-ascii" I need help with this one, even though it's a pure C++ question, it's about template optimization:
If a template class has methods that DO NOT depend on or use the type parameter(s) of the template will the code for the methods nevertheless get instantiated separately for each unique instance of the template class? For example, if there are 20 unique types used to declare 20 instances of template class X, do those methods get implemented 20 different times, needlessly, and cause a big code bloat?
what about 'non-dependent' methods with local statics: template <typename T> class C { int fooCount() { static int foo = 0; return ++foo; } }; C<int> a; C<long> b; int fooA = a.fooCount(); int fooB = b.fooCount(); what is the value of fooB? If there was only 1 implementation of fooCount() I would expect fooB == 2. However, I think you will find fooB == 1. I suppose a compiler could still make the code common, but with separate static foo's, but then in general, there is nothing stopping a compiler from looking at 2 unrelated functions and saying, 'hey, lots of the code here looks the same; I should just refactor some of it out and remove some code bloat'. Not sure much of that ever happens. Reality is that once upon a time there were compiler switches for size vs speed, but speed won.
Even if most compilers are smart about this kind of thing, is it still better practice to separate from the template class (or method) all of the code that does not use the template type parameters?
Short answer: I suspect code bloat happens, and you should keep non-dependent code out of your templates if you care about it enough.
Thanks,
Elisha Berns e.berns@computer.org
Tony
Thanks for the suggestion(s). Elisha
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Gottlob Frege Sent: Wednesday, May 18, 2005 8:58 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Question about template optimization
Date: Wed, 18 May 2005 17:32:49 -0700 From: "Elisha Berns"
Subject: [Boost-users] Question about template optimization To: "Boost" Message-ID: <000001c55c0a$4966cf30$651aa8c0@INSPIRATION> Content-Type: text/plain; charset="us-ascii" I need help with this one, even though it's a pure C++ question, it's about template optimization:
If a template class has methods that DO NOT depend on or use the type parameter(s) of the template will the code for the methods nevertheless get instantiated separately for each unique instance of the template class? For example, if there are 20 unique types used to declare 20 instances of template class X, do those methods get implemented 20 different times, needlessly, and cause a big code bloat?
what about 'non-dependent' methods with local statics:
template <typename T> class C { int fooCount() { static int foo = 0; return ++foo; } };
C<int> a; C<long> b;
int fooA = a.fooCount(); int fooB = b.fooCount();
what is the value of fooB?
If there was only 1 implementation of fooCount() I would expect fooB == 2. However, I think you will find fooB == 1.
I suppose a compiler could still make the code common, but with separate static foo's, but then in general, there is nothing stopping a compiler from looking at 2 unrelated functions and saying, 'hey, lots of the code here looks the same; I should just refactor some of it out and remove some code bloat'. Not sure much of that ever happens. Reality is that once upon a time there were compiler switches for size vs speed, but speed won.
Even if most compilers are smart about this kind of thing, is it still better practice to separate from the template class (or method) all of the code that does not use the template type parameters?
Short answer: I suspect code bloat happens, and you should keep non-dependent code out of your templates if you care about it enough.
Thanks,
Elisha Berns e.berns@computer.org
Tony
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 5/19/05, Gottlob Frege
template <typename T> class C { int fooCount() { static int foo = 0; return ++foo; } };
[snip]
I suppose a compiler could still make the code common, but with separate static foo's, but then in general, there is nothing stopping a compiler from looking at 2 unrelated functions and saying, 'hey, lots of the code here looks the same; I should just refactor some of it out and remove some code bloat'. Not sure much of that ever happens. Reality is that once upon a time there were compiler switches for size vs speed, but speed won.
I have one question: Isnt &C<int>::fooCount garanteed to be different from &C<long>::fooCount ? [snip]
Tony
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Felipe Magno de Almeida UIN: 2113442 email: felipe.m.almeida+spam at gmail com felipe.almeida+spam at ic unicamp br, felipe.m.almeida at gmail com, felipe+spam at synergy com (Remove the +spam from the email) I am a C, modern C++, MFC, ODBC, Windows Services, MAPI developer from synergy, and Computer Science student from State University of Campinas(UNICAMP). To know more about: Unicamp: http://www.ic.unicamp.br Synergy: http://www.synergy.com.br current work: http://www.mintercept.com "There is no dark side of the moon really. Matter of fact it's all dark."
participants (3)
-
Elisha Berns
-
Felipe Magno de Almeida
-
Gottlob Frege