Related articles |
---|
Templates in C++ jcohen@juniper.tc.cornell.edu (1995-05-11) |
Re: Templates in C++ litsios@iis.ee.ethz.ch (1995-05-12) |
Re: Templates in C++ jsgray@ix.netcom.com (1995-05-12) |
Re: Templates in C++ shepherd@schubert.sbi.com (1995-05-12) |
Re: Templates in C++ norman@flaubert.bellcore.com (1995-05-14) |
Re: Templates in C++ davidm@flora.Rational.com (1995-05-16) |
Re: Templates in C++ jgmorris@cs.cmu.edu (Greg Morrisett) (1995-05-17) |
Re: Templates in C++ jplevyak@violet-femmes.cs.uiuc.edu (1995-05-17) |
Re: Templates in C++ bill@amber.ssd.hcsc.com (1995-05-25) |
Re: Templates in C++ kanze@gabi-soft.fr (1995-05-29) |
[4 later articles] |
Newsgroups: | comp.compilers |
From: | shepherd@schubert.sbi.com (Marc Shepherd) |
Keywords: | C++, performance |
Organization: | Compilers Central |
References: | 95-05-081 |
Date: | Fri, 12 May 1995 12:34:32 GMT |
jcohen@juniper.tc.cornell.edu (Jeffrey Cohen) writes:
>With all this discussion of the performance differences between
>C and C++, I didn't see one reference to C++ templates. How does
>this effect performance?
[...]
>[I was under the impression that the biggest practical problem with templates
>is avoiding redundant instantiation when you have separately compiled modules.
>Other than that, they act very much like macros, which don't present great
>compiling problems. -John]
The other challenge with templates is the "code bloat" that can
result if many very similar templates are instantiated. (I guess you
could call this "performance related," in that it can lead to
larger-than-normal executables.)
In the pre-template days, if you wanted a general linked list class,
you crated a list of generic pointers, and anyone using the class had
to cast their objects to and from void*. (At least, this was one
common way of doing it.) This required lots of unsafe casting, but
only one copy of the list management code was generated.
Now, if you write a linked list template class, and instantiate it
for a variety of object types, every one of those instantiations will
cause a separate instance of the template code to be compiled into
the executable. In applications that make heavy use of templates,
this can be significant.
One way to avoid the code bloat is to organize your code into two
classes-- the template, and a private non-template base class that
does most of the real work. In many cases, the template can be
implemented as a series of inline forwarding functions to the private
base (of which only one copy exists in the executable, since it is
not a template).
If you use this strategy, the space costs of templates are minimized.
---
Marc Shepherd
Salomon Brothers Inc
shepherd@schubert.sbi.com
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.