Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are templates so slow to compile?

Large templated projects are slow to compile, the STL being a main culprit of this it seems from empiric evidence. But, why is it slow to compile?

I've optimized builds before by watching for header includes and by combining compilation units, but I don't get why template libraries are quite so slow to compile.

like image 659
Richard Fabian Avatar asked Sep 03 '10 08:09

Richard Fabian


1 Answers

C++ in general is slow to compile because of the ancient include mechanism, which causes the compiler to recursively re-parse every header with all its declarations and definitions and all that's included for every translation unit.

Templates just build on that "feature". But they require all code to be in headers, too, forcing the compiler to also re-parse all implementations of all templates included.

like image 181
sbi Avatar answered Oct 29 '22 00:10

sbi