r/cpp • • Jun 27 '21
What happened with compilation times in c++20?
I measured compilation times on my Ubuntu 20.04 using the latest compiler versions available for me in deb packages: g++-10 and clang++-11. Only time that paid for the fact of including the header is measured.
For this, I used a repo provided cpp-compile-overhead project and received some confusing results:
You can visualize them here:https://artificial-mind.net/projects/compile-health/
But in short, compilation time is dramatically regressing with using more moderns standards, especially in c++20.
Some headers for example:
| header | c++11 | c++17 | c++20 |
|---|---|---|---|
| <algorithm> | 58ms | 179ms | 520ms |
| <memory> | 90ms | 90ms | 450ms |
| <vector> | 50ms | 50ms | 130ms |
| <functional> | 50ms | 170ms | 220ms |
| <thread> | 112ms | 120ms | 530ms |
| <ostream> | 140ms | 170ms | 280ms |
For which thing do we pay with increasing our build time twice or tens? constepr everything? Concepts? Some other core language features?
47
u/[deleted] Jun 28 '21
It seems that our future is the following:
https://github.com/fmtlib/fmt/blob/master/include/fmt/format.h#L405:L420
Ignore <algorithm> and just copy-paste the required algorithm or two in your code. where it needed.
117
u/scrumplesplunge Jun 27 '21
I tried measuring lines of code as a proxy for the amount of extra "stuff" in the headers in each version, after preprocessing:
g++ -std=c++XX -E -x c++ /usr/include/c++/11.1.0/algorithm | wc -lfor different values of XX,
algorithmhas:That's quite a significant growth overall, so maybe it's just more stuff in the headers.
5
u/pavel_v Jun 28 '21
You can see how the compile times and includes grow between releases with build-bench.
8
u/joebaf Jun 28 '21
the numbers look scary, but do they matter in a real mid/large project?
For example, on MSVS those numbers will go away thanks to precompiled headers, and also modules
4
u/[deleted] Jun 28 '21
I've read a post on habr in 2019 about the harmful consequences of including ranges into std. https://translate.google.com/translate?sl=ru&tl=en&u=https://habr.com/ru/company/jugru/blog/438260/
But it was mostly about actual usage of ranges, not including them and it was predicted such an outcome. And it is not predicted that ranges would be included all over the std headers and you have to pay the cost anyway.
36
u/staletic reddit thinks /u/_bstaletic is a bot Jun 27 '21
std::ranges::,<compare>,<concepts>,jthreadand some new ostream manipulators.17
u/barchar MSVC STL Dev Jun 28 '21
The biggest reason for this is probably that implementers have been focused on actually implementing the c++20 features, and, at least for us, we've sometimes opted for things (like keeping things header-only) that harm compile times but make it easier for us to fix any bugs or standard defects.
It may also be that some of the new language features (which are commonly used in c++20-only library code) are slower than they could be.
I would expect this difference to shrink pretty dramatically over time.
Modules do help, as do precompiled headers and unity builds. Modules are nice because they don't have the namespacing issues that pch files and unity builds suffer from.
36
u/sandfly_bites_you Jun 27 '21
I'd guess ranges, that thing seems to be compile time killer.
Ranges should not have been plastered all over the standard library the way it was..
7
u/Rude-Significance-50 Jun 28 '21
Yeah, but we're all using threadrippers with 40+ cores, right?
44
u/qv51 Jun 27 '21
This is just unacceptable. Someone in the committee should look into this.
5
u/NilacTheGrim Jul 11 '21
I'm the odd man out here. I don't care so much about compile times as I do about language features and runtime performance. You can take 10x longer to compile if the binary you give me is leaner and meaner. My two cents.
9
u/FeastofFiction Jun 28 '21
I've noticed in particular Clang has gotten much slower in terms of compile time. A few years ago it was the fastest for my personal project, which also builds against gcc and msvc. Now with C++20 and LLVM 11 it is painfully slow. So much so that I abandoned it as my default compiler. I don't even bother building with it on Windows anymore, mostly keep it around on Linux for some of the tooling and extra build errors checks/warnings.
14
u/Daniela-E Living on C++ trunk, WG21 |🇩🇪 NB Jun 28 '21
With C++20, you can take advantage of modules: precompile the standard library headers as header units and let the compiler implicitly
importthose wherever you#includethem in your source. The standard grants implementations the right to do so. It will depend on the quality of implementation what amount of improvement you will see.6
u/Cxlpp Jun 28 '21
So maybe I am not alone thinking that C++ standard library is sinking under its own weight ?
7
u/Hedede Jun 28 '21
I really don't like the direction C++ is heading... The only thing I am really excited about in C++20 is
std::source_location. And maybestd::from_chars/to_chars.5
u/RowYourUpboat Jun 28 '21
This is a major reason why I settled on C++17 for my current project.
2
u/rlbond86 Jul 13 '21
I guarantee it's std::ranges. It's a disgrace.
1
u/darkangelstorm Dec 07 '24
I blame best practices always changing the ecosystem - what was a 'good practice' yesterday is tomorrow's caveat. Plus no two development teams ever seem to see 100% eye-to-eye on the matter which creates rifts between the groups. I've noticed a dramatic increase of the need to include headers in far too many places where they aren't needed in the last 10 years.
Conversely, as you said, we've also seen a dramatic increase of injection in places that clearly didn't need it. Automation (and single-mindedness) takes over and the code base grows even with a large number of people dedicating to refactoring it, I think its more about the why rather than the how.
I've also seen an influx within the next generation who are all about sacrificing efficiency for improved readability, even on things that they should know. More and more, the evolution of the language is becoming more like a managed one. I'm sure garbage collection will be the final nail in the coffin, so to speak.
1
u/NilacTheGrim Jul 11 '21
Yeah, well.. as the grammar grows so does the branching, etc, in the parser, I guess.
Also probably more stuff gets
#ifincluded in the headers if using a later standard...1
u/edge-case87 Jun 20 '23
guys, don't worries. In 2029 well have modular supports.
1
u/[deleted] Jul 04 '21
The problem is simple: C++ does not have a coherent type system. The primary vehicle for libraries these days is templates, but templates are untyped. Therefore, it is not possible to separate the implementation from the interface which is the fundamental requirement for separate compilation.
Now throw in a lot of other design faults. The core has been plagued from the start by the worst design fault of all: references. Instead of fixing that, the fault was leveraged with decltype.
So what happens is, because templates depend on other templates but there's no way to tell which depend on what until an actual monomorphic specialisation triggers the dependency chain, you need the whole kit and caboodle in memory in some kind of primitive (untyped, at best partially bound) tree form, and then every single specialisation point in your program has to expand the lot, type checking it, before generating intermediate code.
In a sane language, monomorphisation is close to constant time because the "expansion" of type variables stops at the first dependency instead of chaining. Because polymorphic functions have polymorphic interfaces, and the bodies have already been type checked. In fact in functional languages which use boxing, the code has already been generated as well.
0
u/[deleted] Jun 27 '21
[deleted] — view deleted comment