views:

323

answers:

5

It seems that in C++ and D, languages which are statically compiled and in which template metaprogramming is a popular technique, there is a decent amount of concern about template instantiation bloat. It seems to me like mostly a theoretical concern, except on very resource-constrained embedded systems. Outside of the embedded space, I have yet to hear of an example of someone being able to demonstrate that it was a problem in practice.

Can anyone provide an example outside of severely resource-limited embedded systems of where template instantiation bloat mattered in practice and had measurable, practically significant negative effects?

+1  A: 

I think you'll need to find an older compiler to see the template code bloat in practice. Modern C++ compilers (and linkers) have been able to optimize it away for a while.

Nemanja Trifunovic
+1  A: 

I think it's mainly mental bloat. The next programmer to work on your code will first need to figure out what subset of it matters.

le dorfier
+3  A: 

Template bloat is NOT an issue (It is a mental problem not a code problem).

Yes it can get big. But what's the alternative?
You could write all the code yourself manually (once for each type). Do you think writting it manually will make it smaller. The compiler only instanciate the versions it actually needs and the linker will remove multiple copies spread over compilation units.

So there is no actual bloat.
It is just building what you use. If you use a lot of different types you need to write more code.

Martin York
That's not quite true - the compiler may generate identical code for the different functions `f<signed int>(signed int)` and `f<unsigned int>(unsigned int)`, and probably won't notice that they are identical. Whereas writing it manually you'll only get one of those functions.
Mike Seymour
Ideally you should use a compiler/linker that recognizes and removes duplicate generated assembly code. Visual Studio does that (COMDAT folding).
vividos
This is true if you are just using templates as generics.But it is not true if you are doing metaprogramming with templates. Templates used in metaprogramming can instantiate all kinds of crazy intermediate types that you don't really need. I think the boost::spirit parser generator may be a good example of this.
Baxissimo
+4  A: 

There's little problem in C++, because the amount of template stuff you can do in C++ is limited by their complexity.

In D however ... before CTFE (compile-time function evaluation) existed, we had to use templates for string processing. This is also the reason big mangled symbols are compressed in DMD - the strings used as template arguments become part of the mangled symbol names, and when instantiating templates with longer segments of code (for instance) the resulting symbol sizes would spectacularly explode the object format.

It's better nowadays. But on the whole, templates still cause a lot of bloat for a simple reason - they parse faster and are more powerful than in C++, so people naturally use them a lot more (even in cases that technically wouldn't need templates). I must admit I'm one of the prime offenders here (take a look at tools.base if you like, but be sure to keep a barf bag handy - the file is effectively 90% template code).

FeepingCreature
A: 

Template instantion bloat IS a matter in practice, because it can increases ( a lot!!! ) compile and link time.

I personnaly thinks that c++ #1 problem is compil time, and it's mainly due to template.

I worked on a project with about 50 libs. We had our own rtti system using templates. I had to rewrite because of the template bloat

Here is some numbers:

  • libs went from 640 mbytes to 420 mbytes
  • temps went from 4.3 gbytes to 2.9 gbytes
  • full rebuild went from 19:30 to 13:10
benoitj