36

Follow-up to Rust minimal compiled program size.

rustc hello.rs
> 600 ms

Why does rustc compile a simple Hello World 5-10 times slower than gcc/clang?

Rust uses LLVM so it should be on par with clang. Anyway we are talking about a program that has only three lines of code.

rustc hello.rs -C opt-level=0 -C prefer-dynamic
> 400 ms

gcc hello.c
> 60 ms

clang hello.c
> 110 ms
exebook
  • 32,014
  • 33
  • 141
  • 226
  • 2
    Type-checking and borrow-checking I guess. Also note that `println!` expands to some pretty complex code, unlike C's `printf` which is a simple function call. – kennytm May 21 '16 at 12:09
  • 1
    @kennytm, 500 ms to type-check one line of code? – exebook May 21 '16 at 12:19
  • 2
    See [this](https://www.reddit.com/r/rust/comments/2uxt46/rust_vs_c_inc_compile_speed/) discussion and take a look at this [compilation times](https://ruudvanasseldonk.com/2014/10/20/writing-a-path-tracer-in-rust-part-7-conclusion) of a C++ program ported to Rust. – malbarbo May 21 '16 at 13:07
  • 5
    You can check how long each pass of the compiler takes by passing `-Z time-passes` to the compiler. Experience suggest you'll find that the majority of time is spent in the backend (translation to LLVM IR, LLVM's optimization and codegen, linking). –  May 21 '16 at 13:22
  • 6
    A single data point is pretty much irrelevant, especially considering that very few real-world programs are only one line of code. Perform the same analysis with multiple like-for-like projects. How long does a 10KLOC program compile in both? Then you can start getting into the discussion of whether the overhead (if there is any) at compile time prevents programmer time elsewhere, such as in debugging (see all previous discussions of "it takes too long to write and run tests" for similar arguments). – Shepmaster May 21 '16 at 14:00
  • 1
    Why do you think they should compile in comparable times? LLVM is used for the code generation phase. Compilation is more than just code generation and Rust isn't as simple as C. – Theodoros Chatzigiannakis May 21 '16 at 14:05
  • 1
    There are massive C++ template header files like CGAL which, when used, can take 30 minutes to compile a hundred line file... hard locking cloud build machines by eating all the RAM. I actually worked on a project that got kicked out of Ubuntu because it crashed compiling templates on MIPS so debian said it was broken. With header libs its never really 'a hundred line file'. There are precompiled headers but of course they dont always work. I know "plain rust" can be slow, but Im looking at Rust precisely to get away from C++ template headers slowness. – don bright Jan 08 '19 at 03:01
  • 1
    In light of this question, it is inspiring for the Rust compiler team to write a conditional compiler that converts to an inefficient binary generation when input is less than 5 lines just to get this kind of critics more fair. – SOFe Mar 16 '19 at 07:53
  • @SOFe although I feel your sarcasm, I still wish every compiler out there had an option for "favor compilation speed". – exebook Mar 16 '19 at 23:49

2 Answers2

72

First of all, I don't think it's very meaningful to compare the compile time of two extremely simple programs and expect the result to be representative of compilation times between two languages more generally.

That said, I do expect that Rust, being a language that provides a level of abstraction much more common to higher level languages with little to no runtime performance cost, would have to pay for that to some extent at compile time.

This excerpt is taken from the Rust FAQ:

Rust compilation seems slow. Why is that?

Code translation and optimizations. Rust provides high level abstractions that compile down into efficient machine code, and those translations take time to run, especially when optimizing.

But Rust’s compilation time is not as bad as it may seem, and there is reason to believe it will improve. When comparing projects of similar size between C++ and Rust, compilation time of the entire project is generally believed to be comparable. The common perception that Rust compilation is slow is in large part due to the differences in the compilation model between C++ and Rust: C++’s compilation unit is the file, while Rust’s is the crate, composed of many files. Thus, during development, modifying a single C++ file can result in much less recompilation than in Rust. There is a major effort underway to refactor the compiler to introduce incremental compilation, which will provide Rust the compile time benefits of C++’s model.

Aside from the compilation model, there are several other aspects of Rust’s language design and compiler implementation that affect compile-time performance.

First, Rust has a moderately-complex type system, and must spend a non-negligible amount of compile time enforcing the constraints that make Rust safe at runtime.

Secondly, the Rust compiler suffers from long-standing technical debt, and notably generates poor-quality LLVM IR which LLVM must spend time “fixing”. There is hope that future MIR-based optimization and translation passes will ease the burden the Rust compiler places on LLVM.

Thirdly, Rust’s use of LLVM for code generation is a double-edged sword: while it enables Rust to have world-class runtime performance, LLVM is a large framework that is not focused on compile-time performance, particularly when working with poor-quality inputs.

Finally, while Rust’s preferred strategy of monomorphising generics (ala C++) produces fast code, it demands that significantly more code be generated than other translation strategies. Rust programmers can use trait objects to trade away this code bloat by using dynamic dispatch instead.

Community
  • 1
  • 1
TheInnerLight
  • 12,034
  • 1
  • 29
  • 52
  • 23
    Too bad that C++ compilation times are unacceptably slow. "Look, when we compare to the worst language out there, we are on par." That's not a convincing argument. – Roland Illig Feb 26 '20 at 23:32
  • 9
    @RolandIllig but it's kind of good to know if you are comparing Rust to C++, which many people would. – matanster Aug 29 '20 at 13:40
-5

It is because the compiler checks for errors before the runtime and tells you what's wrong. On the other hand C, C++, etc. compile fine but throw errors at the runtime. Also rust's compiler is smart so i lets you know what you did wrong.

  • 2
    While this is true, this is only part of the equation, and some measurements show that this is a small part. There is already an excellent answer, what does yours add? – Chayim Friedman Feb 20 '23 at 12:29