When does template instantiation bloat matter in p

2020-02-28 03:13发布

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?

标签: c++ templates d
5条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-02-28 03:26

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

查看更多
劳资没心,怎么记你
3楼-- · 2020-02-28 03:36

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.

查看更多
女痞
4楼-- · 2020-02-28 03:44

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.

查看更多
叼着烟拽天下
5楼-- · 2020-02-28 03:45

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.

查看更多
▲ chillily
6楼-- · 2020-02-28 03:51

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
查看更多
登录 后发表回答