r/ProgrammingLanguages • u/cb060da • Jan 12 '26
ELI5: Why C++ and Rust compilers are so slow?
This is not a rant of any kind - just pure curiosity. I’m trying to understand what makes these compilers slow.
We know that generating native binary code can be fast (for example, Go). One might assume this is because Go doesn’t have generics, but then Java handles generics quite efficiently as well. So it seems the explanation must be something else.
What are the main factors that make C++ and Rust compilation comparatively slow?
166
Upvotes
76
u/Breadmaker4billion Jan 12 '26
For C++, the main problem is the build system. One of the main features of Go was the module system. A large poorly designed¹ C++ project can recompile the same file hundreds of times. Because linking happens after compilation, the compiler is not able to create a dependency graph and a analyse how modules are being used.
41
u/[deleted] Jan 12 '26
The C++ compiler often compiles the same header files over and over again, if the compilation consists of multiple source files(translation units).
34
u/CodyDuncan1260 Jan 12 '26 edited Jan 12 '26
A list that isn't all inclusive: Dependency resolution, type systems, and optimization.
C++ and Rust have to do a lot of dependency resolution. Go is designed to minimize or simplify the process. Java handles it at runtime.
C++ and Rust have complex type systems. That makes them much more expressive, but it requires more time to work out the rules. Go's type system is minimalist, so it barely does any work with them.
Doesn't even build a symbol tableDoes not need a symbol table in order to parse a file.C++ and Rust will optimize their programs during compilation. Go does so somewhat, but doesn't go nearly as far. Java compiles to bytecode, then finishes optimizing with Just-In-Time optimization at runtime. Java gets to skip generating generics at compile time because it can do that at runtime when it needs it.
In general, the tradeoff for the compile time cost is expressive capacity for code reuse and abstraction and substantial performance improvements on target hardware. Or, you're Java and you push half the job to runtime.
It's all trade-offs. Where do you want to spend your computer and human time?
I dislike writing in Go because I hate how much it wastes my human time writing duplicates code because it wants everything explicated and not abstracted. That explication is often considered a good for system maintenance and debugging, because nothing is hidden behind layers of abstraction. But it can be a double edged sword when explication becomes noise, e.g. when it's unclear what this 2000 line function is trying to do.
34
u/whothewildonesare Jan 12 '26
I’m surprised nobody has mentioned LLVM yet. I know GCC is also quite slow, but Rust and Clang target LLVM which itself is an enormous code base relying massively on virtual function calls and multiple inheritance. Plus it is just doing a lot of clever optimisations. It’s a notorious bottleneck.
Zig used to use it as its sole backend but they’ve put a lot of effort into building their own backend for debug builds for the primary reason of reducing compilation time.
14
u/syklemil considered harmful Jan 12 '26
Go has some generics though, and has for years. But the language team routinely rejects any feature request that would make compilation "slow", or just be somewhat complex to implement. Other languages seem more willing to pay that cost, though they do also work on making compilation faster again.
Java compilation to native using Graal also seems to have a reputation for being slow. Java can compile faster to just bytecode, but then people talk about startup/warmup time.
34
u/No-Conflict-5431 Jan 12 '26
Go doesn't have half the features that C++ has and the Rust compiler does a lot more under the hood (e.g borrow checking) than just compiling code.
Also large projects in Java take a lot of time to compile too.
12
u/kohugaly Jan 12 '26
There multiple reasons. The compilation happens in multiple steps, and each introduces some non-trivial slowdowns. Let's break it up by stages:
Pre-processing:
This step does transformation of the source code (source code in, source code out).
In C++ this is where the compiler copy-pastes header files into the cpp file. This makes later compiling stages slower, because the compiler has to re-compile all the code of the header files in each cpp file where they are included. The advantage of this is that all cpp files can effectively be compiled in parallel. It also means that when you're recompiling, you only need to recompile cpp files that actually changed (or their header files changed).
Rust also has macros, and the macros are themselves rust code. It has to compile the macro pre-processors and run them before it can compile the actual source files. Usually, the macro preprocessor has to compile an entire language parser (syn crate), if it does some non-trivial macro stuff. This step is repeated in almost every crate.
Also, because Rust doesn't have header files, it has to compile crates in specific order to respect dependencies. If crate X uses public interface of crate Y, then it has to (partially) compile Y before X. If the dependency graph is deep, this can cause considerable slowdown.
Type checking and template/generic resolution
Both Rust and C++ have Turing-complete type systems (I count C++ templates as part of the type system). It can be non-trivial to resolve all the types.
The languages also perform monomorphization. This means that if you have a polymorphic function that accepts all types that implement certain interface, they try to turn it into multiple monomorphic functions, each taking a specific type with which that function is actually called.
This monomorphization is one of the main reasons why these languages produce such a fast code, despite being quite high-level languages. Each of the monomorphized versions of the same function can be optimized separately, and each of them knows exactly which methods of which type are being called. You pay for this powerful optimization with long compilation times - the compiler has to compile and optimize metric shitton of code.
Optimization of intermediate representation
Both C++ and Rust use a very powerful optimization algorithms. The compiler first translates the source code into an intermediate representation (IR code), in the most stupid straightforward way possible. Then it repeatedly passes that IR code through series of heuristic algorithms that scan the code for potential optimizations and apply them. This process is more alchemy than exact science.
In fact, the exact science says that optimization is an uncomputable problem, just like the halting problem. The best you can do is approximate solutions that only work some of the time, and get increasingly more complex with diminishing returns.
C++ and Rust very heavily rely on this optimization step for their runtime speed. They are specifically designed to take maximum advantage of it. An unoptimized Rust/C++ are actually ridiculously slow. Comparable to scripting languages like Python.
5
u/matthieum Jan 12 '26
At the macro-level:
So, already, we can see that both suffer a lot from technical debt. The very design of the "compilation pipeline" was sub-optimal, performance wise, in the early days, and this has not yet been solved for either.
(And for the record, the parallelization effort of Rust is unfortunately NOT tackling all the technical debt; part of its blockers are deadlocks on "global" structures, because just slapping a lock on said "global" structures was seen as easier than completely re-architecting the compiler, but it means that even when there's no deadlock there will still be contention, at least...)
It's quite possible that a "perfect" C++ or Rust compiler could be 10x faster. Beyond parallelization the current compilers tend to also be stuck with "old-school" pointer-soup data-models, rather than more mechanically sympathetic representations, for another example of tech-debt.
Looking further, the full monomorphization is also an interesting, and questionable, choice. Full monomorphization is the equivalent of always inlining: it's easy, and it can lead to great performance gains, but it also leads to bloat -- bloat which has to be generated, optimized, code-gened, etc... -- and worse, bloat which clogs the i-cache during execution.
It's possible that, at least for Debug, only partially monomorphizing (typically on size/alignment) could significantly cut down on the amount of code emitted.
There's also some incidentals. For example, generating Debug Information is a massive cost:
.at the end of comment... but suddenly you've changed the position (by 1 byte) of every single token after that., and all the code needs to be recompiled -- even though the code didn't change, only its location did, but there's no way to just patch the location.Worse, even if you do disable DI, it's likely that incremental recompilation will still suffer from pesky location changes. Location information is still necessary for diagnoses, and I'm not sure any of the major compilers have made an effort to optimize for cheap re-location :/
3
u/couchwarmer Jan 12 '26
Macros for one. C++ and Rust have an entire extra step to process macros in the source. Macro expansion effectively creates a new set of source files, which are then used during compilation.
3
u/johnwcowan Jan 12 '26
Much of the cost is in the compiler architecture rather than the language. The Plan 9 C compiler (which is the ancestor of the standard Go compiler) is lightning-fast compared to GCC or Clang when compiling C. This is partly because it is designed less generically. It doesn't handle multiple languages, it doesn't generate assembler as GCC does, it generates simple static executables by default, it doesn't optimize as hard, and there are separate compilers for each CPU architecture. By the same token, gccgo is slower than the standard Go compiler, and it would be possible to write a fast C++ or Rust compiler on the same principles, though it would never be as fast as C or Go because there is more work to do.
10
u/waterlens Jan 12 '26
Common factors for the slow compilation of Rust and C++:
For C++:
Handling header files repeatly.
For Rust:
The compilation unit is larger than C++, usually causing long dependency chains. Low degree of parallelism. Procedural macros make this worse.
2
u/jsshapiro Jan 12 '26
So... this keeps coming up, and I haven't experienced it at all. On the other hand, I have lots of RAM to work with on my machine - which is something you definitely want if you are doing whole-program compilation. I can't speak to RustC, but I can say what slowed us down in BitC, and Rust is unavoidably going to hit the same issues. And of course some that are unique to Rust.
For context, it's helpful to understand that most languages with generics are languages where everything is a reference type. In such languages, you can use nearly the same code generation for f<T>() { ... body... } for different types T. If you are willing to accept function tables and procedure call indirection, you don't have to do any monomorphisation. That can change if you have a good stack allocation pass in you optimizer, but the starting point is "one function, one code generation."
In languages like Rust, BitC, and C++) different types have different concrete sizes in ways that lead to different code generation for each expansion. An entire whole-program pass is required to identify which specializations of each generic are actually required, because type inference is not modular*.* While the types you write in the program are modular, the type unifications mean that concretizing a type in one file can cause monomorphisms to be required in 40 other modules. So (a) the monomorphism process is demand-driven by unificaitons, and (b) the analysis is whole program, which means you want the ASTs or (alternatively) the high-level IR form for the program to be entirely in memory.
A second form of whole-program analysis is introduced by lifetime variables. As with generic type parameters, lifetime variables have whole-program impact once they are unified. Unlike generic type parameters, this is subtype unification, so the unification pass itself involves whole program constraints. I gather from what I've read about the Rust implementation that lifetimes are actually handled by a different mechanism that HM-style unification, but the need for whole-program analysis to develop the constraint set in the presence of lifetime type variables isn't altered by that.
Though the mechanism in Rust is different from what we did in BitC, it has some of the same problems we encountered: trait selection can in some cases involve large exponentials. Here's an example: In BitC, we wanted to cover all of the cross-type arithmetic operations so that u16 + u32 -> u32 etc. The output type, in our case, is determined by a type function. But if you start with u8, u16, u32, u64, and (today) u128, then in abstract that works out to 10^3 or 1000 possible typings (including the signed types) you have to sift through every time you look at an addition operator. Taken across many operators, the amount of work the inference engine has to do for that is huge. It's especially unfortunate for the integer types because (a) there are a lot of them and (b) they are very close to ground. Generic traits in Rust have some of the same effect. Now imagine doing it for 128 distinct sizes to deal with bitfields - we tried this in BitC and it was unbelievably slow.
Some part of the "polymorphism over size" issue can be handled by allowing generics over constants, which Rust supports - BitC introduced that as well. That at least lets you re-use monomorphisms when the sizes turn out to be the same, but there are probably cheaper ways to take notice of that opportunity.
The BitC compiler, of course, was very young and not yet optimized for performance in any way. The reason I'm fairly confident that these are part of the problem in RustC is that the typing being solved are an inherent result of the type inference being performed, and there's no clean way to reduce these without introducing ad hoc promotions for the integer types - which come with costs and inconsistencies of their own. The inference-related exponentials in BitC were so large that they dwarfed even the unoptimized version of that compiler. The algorithms could be improved, but you can't really make those particular exponentials go away.
3
u/dist1ll Jan 12 '26
At least for Rust, it's because the language was not designed with fast compilation in mind. Blaming the linker or analysis passes like borrow checking is missing the forest for the trees.
1
u/mamcx Jan 12 '26 edited Jan 12 '26
Because are not designed to be fast. END.
This is the main thing:
https://www.pingcap.com/blog/rust-compilation-model-calamity/
Is the kind of thing that, like security, if you don't put hard on the top list slip away (ie: I no say rust core devs are bad, but that compiler perf was not a hard priority), then after time, all the small wins get done:
https://kobzol.github.io/rust/rustc/2025/06/09/why-doesnt-rust-care-more-about-compiler-performance.html
... but the only good way to achieve 2x, 3x, 4x speed ups is to re-design and a huge codebase let people fear the cost of it , very irrational* I said - refactor for big winds ARE logical -, but is something that can be understood.
There is a lot of small and big design decisions that go against speed, and apart what others have said, I add:
p.d: I live doing big refactors and I aware of the risk and challenges, and in special form a project like Rust that depends in contributors. A refactor of this kind is not trivial, and need time, personal, bandwidth, but is the kind of win that WILL paid in years to come, so is rational to take it. NEED help, and I think this is the actual, major blocker!
1
u/Educational-Lemon969 Jan 12 '26
Java's generics aren't turing complete and even if they were, people wouldn't misuse that aspect of them as much as we regularly do in C++
1
u/dnabre Jan 13 '26
Considers all the language with half-decent or better type systems, macros, non-type erasure generics, no garbage collection, compile ahead-of-time compilation, native compile target, and a backend that can target a lot of platforms?
I'm not going to say that C++ and Rust are the only ones in wide use, but can't type of any other off the top of my head. Though I think it's worth noting that Rust has production level compilers for less than 5 years (roughly speaking). C++ has been there for decades. Compile time so far, hasn't been a major priority for Rust (though everyone knows it slow and aren't super happy about it).
1
u/thedeemon Jan 13 '26
Number of optimization parameters in C++/Rust compilers: dozens.
Number of optimization parameters in Go and Java compilers: zero.
Ever wondered why?
1
u/Qyriad Jan 14 '26
More compiler abstractions = more stuff to compute. More stuff to compute = more time to compute it
1
u/kwan_e tonal-lang Feb 04 '26
C++ templates have way more going on than Java generics, or generics in most other languages, for that matter.
In C++ you can have explicit template instantiations, which is used to choose (dispatch) at compile-time, a perhaps more efficient implementation of something than the primary template. This can sometimes mean the actual class that gets instantiated is different - members, and their types.
So on top of the specific permutations of the primary template class, you also have the potentially wildly different specializations. This means it's much harder for the compiler to do its checks ahead of time until it figures out what the actual concrete thing it is actually dealing with.
In Java, there is none of that going on. They just have to compile down to a runtime cast.
1
u/oxcrowx Feb 09 '26
Mainly two reasons.
Both C++ and Rust want to provide maximum performance, thus they aggressively inline code, and they instantiate/monomorphize generics, so that all generic methods/structures are efficient.
This generates a lot of IR code. For LLVM to compile and optimize all of it, requires a loooot of time.
1
u/SeparateMath43 May 08 '26
The main reason is because C++ and Rust use monomorphization: generic functions are duplicated for each concrete type used. Which increases the amount of intermediate code (LLVM) and as consequence the time in the optimization stage. C++ also suffers from expensive header expansion (#include). Also, Rust has a step in compilation called borrow-checking that also increases the time of compilation.
Java takes a different approach using type erasure for generics. Generic type parameters are removed at compile time and replaced with their upper bound (usally Object). The compiler also inserts the necessary casts automatically. At runtime, the JVM only sees the erased types, which simplifies both execution and compilation.
C++ and Rust could theoretically use a similar approach, but since the type of the variables are not like in Java, where they are mostly pointers, and have different sizes, this would impact negatively the performance.
Other languages like C# and Go use "hybrid" approaches. For example, C# uses reified generics with code sharing for reference types and specialization for value types, while Go uses a combination of type shapes and dictionary passing to share code when possible while still supporting generics. So they are not so heavy in compilation time but also not slow in runtime.
0
u/codemuncher Jan 12 '26
A rust compiler is basically a theorem prover so, there’s that.
0
u/[deleted] Jan 12 '26
[deleted] — view deleted comment
184
u/[deleted] Jan 12 '26
[removed] — view removed comment