9 ms·
Speed Up C++ Compilation
- hashtag-til 3y agoAny good guide on how to apply some of these tips on cmake?
- rewmie 3y agoCMake supports unity builds right out of the box. https://cmake.org/cmake/help/latest/prop_tgt/UNITY_BUILD.html https://cmake.org/cmake/help/latest/prop_tgt/UNITY_BUILD.htm... And support for ccache is not an option to some build systems. For instance, Visual Studio projects in general and the msvc compiler require a lot of fighting to get it to work. Also, ccache is renowned for not supporting precompiled headers.
- ninepoints 3y agoI wrote a comment in the linked thread as to why I really dislike Unity builds. Some folks make it work for them, and they have some attractive/tempting properties, but I think the siren's call of unity builds should be utterly rejected whenever possible.
- rewmie 3y agoI agree, in particular by the way unity builds can break code that relies on internal linkage to avoid conflicts between symbols. But it's nevertheless a technique that's supported by cmake and can be enabled by flipping a flag.
- Conscat 3y agoAt my job, it speeds up clean builds 3x on the CI server, but a portion of those gains are lost to code breaking frequently due to unity.
- ninepoints 3y agoYea, honestly these types of numbers really aren't surprising, but usually when you profile a build and dig into why the build perf was so bad to begin with, you generally find stuff like template abuse, bad header organization, and countless other untold sins that were better off fixed anyways.
- _0w8t 3y agoChromium once supported unity builds. For me it speeded up builds from 6 hours down to 1 hour 20 minutes on a Windows laptop. And Chromium tries to make their headers reasonable. Chromium eventually dropped support for such builds. At Google the developers have access to a compilation farm that compiles within like 5 minutes. With such farm unity builds makes things slower as they decrease parallelism. So Google decided not to support them not to deal with very occasional compilation breakage.
- saagarjha 3y agoApple doesn’t have this, and WebKit supports Unity builds ;)
- rewmie 3y agoBad header organization is perhaps the gravest yet unspoken cardinal sin of both C and C++ development. I lost count of the number of times that I had to deal with cyclic dependencies accidentally introduced by someone when they started to add #include without any criteria.
- rewmie 3y agoThe article mentions forward declararions but does not mention the age-old pimpl idiom. https://en.cppreference.com/w/cpp/language/pimpl https://en.cppreference.com/w/cpp/language/pimpl Pimpl greatly reduce build times by removing includes from the interface header at the expense of requiring pointer dereferencing, and it's a tried and true technique. Another old school technique which still has some impact in build times is to stash build artifacts in RAM drives.
- wiseowise 3y agoI’ll never understand why C++ developers insist on using this ugly hack instead of using pure virtual interfaces.
- rewmie 3y agoWhat leads you to believe that protocol class + concrete implementations + factory can replace pimpls? I'm going to make it simple for you: take a Qt widget. You define its widget layout with Qt a designer, save a UI form file, and get Qt's UIC to generate a header file that implements that widget tree. You have three options to include the object in that header: either inherit from it, composition, and pimpl. So you swore off a pimpl. Pray tell, how do you use a Pure virtual interface in this case?
- wiseowise 3y ago> What leads you to believe that protocol class + concrete implementations + factory can replace pimpls? What leads you to believe they can’t? > I'm going to make it simple for you: take a Qt widget. You define its widget layout with Qt a designer, save a UI form file, and get Qt's UIC to generate a header file that implements that widget tree. You have three options to include the object in that header: either inherit from it, composition, and pimpl. So you swore off a pimpl. Pray tell, how do you use a Pure virtual interface in this case? Never worked with Qt. Can you give me FooBar example where pimpl is superior to pure abstract class, please?
- jb1991 3y ago
- ladberg 3y agoWould recommend Build Bench to help gain a mental model for how different styles of code impact build times: https://build-bench.com/ https://build-bench.com/
- gauddasa 3y agoIf it's too long to read then you can safely skip to "C++20 Modules" section near the end of the article.
- GuB-42 3y agoAssuming you are using C++20, which as of now, few codebases use. As work, now, we mostly use C++11 and C++14, some projects are C++17, but I am not aware of a single C++20 project, I think all active projects that are C++03 or below have been migrated to something more recent. Just a data point.
- Snafuh 3y agoUnreal Engine actually switched to C++20 with their next release (preview available). I was quite surprised to read this in their roadmap, as Unreal's codebase is quite massive and also uses some custom build tools.
- nice__two 3y agoI invite everyone to have a look at compiling LibreOffice [0]. Doing a full compile, has been known to bring even the most powerful machines to their knees. They’ve employed a lot of the referenced article’s suggestions and it still is a massive code-base to compile. [0]: https://wiki.documentfoundation.org/Development/BuildingOnLinux https://wiki.documentfoundation.org/Development/BuildingOnLi...
- bravetraveler 3y agoIf on Fedora, you can give it a whirl like so: cd $(mktemp -d) fedpkg clone -a -b f38 libreoffice && cd libreoffice fedpkg mockbuild This requires the 'fedpkg' and 'mock' packages be installed, and that your user is in the 'mock' group Compiling is one of many benchmarks I do, lol Edit: note for posterity -- the branch will change as time goes by. Fedora 38 is the current release, so f38 is used. The source for the code and the target for the build don't have to match. Look into different 'mock roots' at /etc/mock/*.cfg
- wiseowise 3y agoFull compile is rarely an issue, because most of the time is spent in incremental build.
- gavinray 3y agoC++ Modules should hopefully significantly improve this situation
- flatline 3y agoI see no reason to believe this will actually be the case based on anecdotal evidence from folks I know who have tried out the early module implementations. I’m hoping they are wrong, but I do not think that modules or anything else is going to result in a significant speedup for the average case, and large C++ compile times will remain a perpetual feature of the language.
- pjmlp 3y agoAs per Microsoft talks, C++23 import std; is faster than a plain #include <iostream>.
- bluGill 3y agoBut clang and GCC have not reported anything (it doesn't work), but they have always been careful to not get people's hopes up
- pjmlp 3y agoHow could they? Their module support barely works, and will take a couple of years to catch up to VC++. Other C++ compilers are even more behind in regards to catching up to ISO C++.
- bluGill 3y agoExactly. They have been cautious about claiming if modules will actually speed up compilation when they arrive. They might be a visual studio only win for speed. (Still useful for simplicity over include files)
- jlokier 3y agoI think that says more about Microsoft's poor optimisation of #include <iostream> semantic caching. There's no good, fundamental reason why #include caching can't be done with similar mechanisms to import, resulting in similar perfornance. Certainly, modules are cleaner than headers. That's a language improvement and a fine reason to switch to them. Headers leak more definitions, macros etc that are part of the implementation, and their interpretation is affected by definitions before the header is included. But in normal situations those header semantic leaks need only add a very small or negligible time overhead to symbol resolution compared with modules, both from a compiler-friendly precompiled form. Articles I've see explaining why modules are faster tend to talk about parsing headers, repeatedly, and if not parse then still process a precompiled syntax in some complicated way, as opposed to modules storing a compiler-friendly data structure. But those are merely describing how things are already done, not what's possible.
- MarkSweep 3y ago> Multiple Git Checkouts You don’t need to do a full clone to have multiple working copies of your repo checked out. You can use the git worktree command to create these working copies: https://git-scm.com/docs/git-worktree https://git-scm.com/docs/git-worktree
- billyzs 3y agoI use worktrees, but some builds which uses git hash will fail in worktree checkouts, thinking that "it's not a git directory" (forgot the exact error, it's been a while); haven't found a solution to this
- dragly 3y agoOne way to get an error when retrieving a git hash is by building inside a Docker container. If you mount the root directory of a work tree, but not the “git-common-dir”, git will fail to give you the commit hash.
- derriz 3y agoWhat is the advantage of worktrees over multiple clones? Using multiple clones is trivial - there are no new git concepts to learn - and works perfectly with all build tools particularly ones that heavily use caching. What productivity improvements do worktrees bring?
- andromeduck 3y agoProbably makes it a bit easier to propagate changes between copies
- dragly 3y agoYes, exactly. Git worktrees will for instance make it easier to merge changes between multiple checked out branches without pushing upstream. For big repositories with long histories (like Qt), worktrees also save some disk space.
- 3y ago
- ligurio 3y agoI've collected the most popular methods for speeding up compilation and testing in https://github.com/ligurio/sqa-wiki/wiki/speedup-your-ci https://github.com/ligurio/sqa-wiki/wiki/speedup-your-ci
- jheriko 3y agohow about we stop the module and linker nonsense and just do it right? stick everything in a single compilation unit automagically and have a flag to let bad legacy code that reuses global symbols produce errors/warnings so they are easy to fix, or be ignored so that we can still have backwards compatibility. that is just the "hacky" way of doing things and it indeed works wonderfully, although often breaking any kind of incremental build - this is because we implement incremental build wrong, which is suitably easy to fix as well.
- mostlylurks 3y agoC++ compilation times can become quite slow even when you're doing what you describe manually (known as a "unity build", and not particularly rare in some niches), even if you avoid including the same headers multiple times. Of course, a lot of this depends on what features you use; template-heavy code is going to be slower to compile than something that's more-or-less just C fed into a C++ compiler.
- SmoothBrain12 3y ago^^^ This programmer gets it
- LoganDark 3y agoI used to spend lots and lots of time finding and working on header-only libraries that didn't have other dependencies or weird linking requirements - you'd just `#include` them, and that was the code, and you could use it just like that. But in large projects, this starts to get a bit unwieldy and the whole "every file is its own unit, and can depend / be depended on by any other units without issue" thing is actually super useful.
- memefrog 3y agoSeparate compilation enables parallelism. This approach might save some time but also makes compilation single-threaded. I have many cores on my CPU. What are the others meant to be doing when I'm compiling? So then what does the goal become? Splitting the codebase into 8 equally-difficult-to-compile translation units? What about someone with 6 cores, or 12? Or 32?
- speps 3y agoSome people might not know this but Epic actually did a lot of this automatically within Unreal Engine by writing their own equivalent of CMake. The whole codebase is going through UnrealBuildTool which auto generates headers as well. It calls compilers, linkers, etc. for each supported platform. It allows them to enable the "unity build" stuff automatically for example, and on a per file basis as well. If you have a file checked out locally (in Perforce terms), it won't include it into the usual unity compilation unit for it and will compile it on its own as it's likely it's one you're going to edit a lot if it's one you've got checked out. Anyway, I thought it was an interesting datapoint as I think it's quite unique. To the level of Boost using a modified fork of Jam at some point...
- maccard 3y agoUBT is a blessing and a curse. I've spent a lot of time with it, and contributee to it. It's definitely not the fastest of build tools, the change detection takes a few seconds even on powerful hardware. The way it handles UHT (the codegen step of unreal) isn't great either. It has a nasty habit of getting itself stuck, and it's got a horrific architectural decision of only allowing one instance of the process to run at a time (and see previous point, it gets stuck). Combine those two with modern IDEs that call the build tool in the background and you bet a messy situation. But the adaptive unity build is probably one of the coolest features I've ever seen in a build system
- 8n4vidtmkvmk 3y agoDo they offer the header generator as a standalone tool? I figured this was possible but haven't seen anything that does it. I hate writing function signatures twice with subtle differences like default args and overrides. Waste of my time.
- dataflow 3y agoIs header generation substantially faster than compilation? I guess it skips codegen and instead replaces it with header generation, but isn't most of the time spent in parsing/semantic analysis? Or is header generation done in some hacky way that's much faster than full parsing?
- ndesaulniers 3y agoBuild times of the Linux kernel with clang are dominated by the front end. 30 years of crufty headers is a lot to parse. What we actually need is tooling to help recommend how to refactor or split headers in a way to reduce having to parse a ton of dead code. Or even better, tooling that would generate one off headers per TU. I think it could be done.
- nine_k 3y agoClang supports precompiled headers (pch). Does this not help?
- ndesaulniers 3y agoI'll have to figure out how best to wire it up into the kernel's build system, kbuild. I haven't tried yet. Anyone have experience with PCHs? Good or bad?
- ra1231963 3y agoHe mentions some tools at the bottom (ClangBuildAnalyzer, Build Insights in Visual Studio, include-what-you-use). Looks like https://include-what-you-use.org/ https://include-what-you-use.org/ might do that.
- ndesaulniers 3y agoI'm having an intern start in the fall to work on integrating IWYU into the kernel's build system, kbuild. One major deficiency in IWYU that we will have to solve is that IWYU only understands post-preprocessed sources. If you have preprocessor guards for some code (as is done extensively in the Linux kernel) IWYU can recommend changes that break the build for other configurations than the one used when running IWYU.
- jokoon 3y agoI love C++, but compile time prevent me from enjoying it. I would gladly use a new version of C++ that breaks compatibility but solve this problem. This is the largest barrier for C++ right now.
- jcelerier 3y agoIn https://ossia.io https://ossia.io with PCH, using clang, ninja, mold, and some artificial split in shared libraries for development builds, I get a compile-edit-run cycle of a couple seconds in general... I wouldn't say it's too much of a problem if you use the tools already available
- thechao 3y agohttps://news.ycombinator.com/item?id=36148475#36153561 https://news.ycombinator.com/item?id=36148475#36153561
- diamondlovesyou 3y agoThe power of the optimizations available to C++ are what make it so fast (see how slow debug mode is vs -O2/etc), and what allow C++ to be fast in the face of common/easy-to-understand, but technically perf-hostile, patterns. Bit counting loops vs popcnt, auto-vectorization, DCE, RCE, CSE, CFG simplification, LTCG/LTO, and so on. These things let you write "high level" (to a point - there are some ways to do "high level" paradigms and absolutely eviscerate the compilers ability to optimize) code/algos and still get great hardware level performance. This is so much more important overall than the time it takes to compile your program, and even more so once you consider that often such programs are shipped once and then enter maintenance mode. It doesn't really have anything to do with compatibility (not entirely, but the things that are the biggest issue to good optimization quality and are fixable are things that need a system-level rethinking on how hardware exceptions happen). It just isn't reasonable to expect developers to know how to optimize, and it doesn't scale.
- cvoss 3y agoIn many contexts, one should rarely pass -O2/-O3. A project that is built thousands of times during development may only be run on intensive workloads (where -O2 performance is actually a necessity) a handful of times by comparison. A dev build can usually be -O0, which can dramatically improve compilation time.
- 7e 3y agoBazel.
- xvilka 3y agoIf only it wasn't Java. It's ridiculous that a relatively simple build tool requires such a monstrous dependency for no reason.
- dilyevsky 3y agoBazel? Simple? Hmm… Also fyi - bazel ships as single binary with 0 dependencies
- pjmlp 3y agoAt least is faster than Python based build systems.
- xvilka 3y agoCan't argue with that.
- McP 3y agoI was expecting its Java based nature to be a problem but in practise found bazelisk to be remarkably self contained and fast. Bazel has issues but Java isn't one of them.
- sluongng 3y ago1. I don’t think anyone who know enough about Bazel to describe it as simple. 2. Why do you dislike Java? Most of FAANGMULA uses it in some capacity no?
- xvilka 3y ago1. Not Bazel itself is simple, the concept of build systems is simple enough to be implemented in native and more portable languages. 2. I was unfortunate to write some Java in the past. Strong dislike of it since. Nowadays I prefer natively compiled languages.
- deleted 3y ago[deleted]
- andrewmcwatters 3y agoThis reminds me of an article I once read here about writing code in such a way that headers were included once and only once, but it required you to manually write the code in such a fashion. The catch was that it was distinctly not #pragma once, nor did the style require the use of header guards. I'm wondering if anyone here remembers that article.
- lelanthran 3y agoDunno which article it was, but if you're saying that header guards don't completely alleviate the compile times, you're correct (In order to hit the guard or #pragma, the file still has to be opened and read). The way to do it is to never have any `#include` directives in headers. Put them all in the implementation source files. You have a function in a `foo.h` which returns a `bar_t` type, that is defined in `bar.h`? Then each source (.cpp or .c) file that includes `foo.h` must first include `bar.h`. While this does not completely alleviate the problem of reading the same header 10x in a project with 10 source files, it does mean that at least you don't read the same same 50x in a project with 10 source files. Each header is read once, and only once, for each translation unit. When using header guards or #pragmas, a single header file will be read multiple times because it can (and usually will) include other headers.
- andrewmcwatters 3y agoThanks for mentioning this, I believe it was a part of the strategy they used for this article I’m thinking about.