boagz57
Jason
75 posts
|
#16618
What makes templates slow to compile? 3 months, 3 weeks ago Edited by Jason on Oct. 28, 2018, 1:37 p.m. Reason: Initial post
So I'm currently trying to implement some more generic classes (arrays, lists, vectors, etc.) and it seems for what I want to do templates would provide the easiest path forward. I know Casey often bashes them for their slow compilation times but it got me thinking about what exactly makes them slower than #define's? What do C++ compilers need to perform for templates that causes this slowness? I read somewhere that one of the main issues with slow compilation has to do with templates being declared in multiple compilation units but if that's the case then they shouldn't be too bad in a handmade hero style project given everything is compiled into essentially one translation unit.
|
mrmixer
Simon Anciaux
534 posts
|
#16620
What makes templates slow to compile? 3 months, 3 weeks ago
Have a look at why are templates bad.
|
Randy Gaul
54 posts
|
#16641
What makes templates slow to compile? 3 months, 2 weeks ago mrmixer That post misses the bigger problem. In my opinion the biggest culprit is linker spam. With templates many different functions are plastered into every single translation unit that includes and uses them. For a large project, as translation units are coalesced by the linker, duplicate symbols need to be churned and culled. Linking is more or less a single-threaded task, so it scales very poorly, and templates heavily exasperate this problem. |
boagz57
Jason
75 posts
|
#16647
What makes templates slow to compile? 3 months, 2 weeks ago
"That post misses the bigger problem. In my opinion the biggest culprit is linker spam. With templates many different functions are plastered into every single translation unit that includes and uses them. For a large project, as translation units are coalesced by the linker, duplicate symbols need to be churned and culled. Linking is more or less a single-threaded task, so it scales very poorly, and templates heavily exasperate this problem."
So if this is one of the main causes then would my original statement be accurate in terms of doing a unity build to help avoid template compilation slowness? Given the entire project is compiled as one translation unit? |
mmozeiko
Mārtiņš Možeiko
1880 posts
/ 1 project
|
#16648
What makes templates slow to compile? 3 months, 2 weeks ago
lld linker has multi-threading capabilities. It is not perfect, but people are working on it.
Here's the presentation from llvm conference last year: https://www.youtube.com/watch?v=yTtWohFzS6s Slides: https://llvm.org/devmtg/2017-10/slides/Ueyama-lld.pdf - check page 30 to see multi-threading improvements. |
notnullnotvoid
Miles
20 posts
/ 1 project
|
#16663
What makes templates slow to compile? 3 months, 2 weeks ago
Unity builds will definitely solve most of the linker spam problem, but you should also watch out for unnecessary template instantiations. For a (simplified) real-world example, recently in my code I replaced something like this:
with something like this:
Generating multiple equivalent versions of a template for different integer types is another common variant. This kind of mistake can be sneaky when template parameters are inferred from function arguments. |