|
![](/i/fill.gif) |
Darren New <dne### [at] san rr com> wrote:
> Warp wrote:
> > Darren New <dne### [at] san rr com> wrote:
> >> For example, you have to parse and compile the
> >> templates each time from source
> >
> > A compiler which supports export templates could conceivably have
> > precompiled templates for certain (although obviously not all) types,
> > such as for example for all basic types and STL classes. Thus it could
> > avoid having to compile them each time.
> Yes. If you restrict templates to doing what generics do, you don't have
> to compile them each time. But that's a restriction on templates.
At least Java generics do not support basic types, so I don't really
understand what you are talking about.
I suppose that no compiler so far uses this optimization, but I can
perfectly imagine it being absolutely possible: When a template class or
function is instantiated for a certain type, the compiler could compile
it into some type of object file. Next time that template is used with
the same type (be it in the same compilation process, by another compilation
unit, or in a future one) it can simply use that object file instead of
recompiling the template code. The mechanism to do this could be similar
and related to export templates (or if technical reasons mandate it, it
could only work with export templates).
> In any case, what *I* meant was that you get a new blob of code compiled
> every time you instantiate a template. Not for each different type, but
> even for the same types in different compilation units. Yes? Or am I
> confused about that?
Currently yes, but as I said, it's perfectly conceivable that this
could be optimized by compilers.
> If I have std:vector<MyType> having functions
> called in one module, then I compile another module with
> std:vector<MyType>, does the linker see that it's the same code and
> function on the same type and only include one copy of the object code
> in the linked result?
The linker always merges all template instantiations of a given type
into one (this is required by the C++ standard, AFAIK). In other words,
the final executable will have only one instance of those functions.
That's not the problem.
(The only "problem" with template code is the increased compilation
time and object file sizes.)
> > Templates just make it easier to avoid code repetition. (In other words,
> > if you want, for example, a vector of ints and a vector of doubles, you
> > don't need to write the vector code twice.)
> Right. Generics do the same thing.
Except that they don't work with basic types, at least not in Java,
so they only solve part of the problem. :P
> > Thus any argument that
> > templates cause memory bloat is usually false. (Yes, there may be a few
> > situations where templated code causes more memory usage, but normally
> > it's the opposite.)
> I think they likely cause code file bloat.
The amount of code "bloat" caused by templates is usually much smaller
than the extra amount of RAM required if templates were avoided. Usually
template functions are relatively small, like some hundreds of bytes at
most, while the data containers they are instantiating handle even
megabytes of memory at a time. With C++ templates the optimization is
in the right place: In the memory usage of the data containers, not the
machine code in the functions of that data container.
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |