News for hackers. Stuff that matters.
Re:C++ vs. Rust Build Times by oslac on Friday January 06, 2023 @01:23AM (#34271621)
Does this article account for the fact that a rustc compilation does more things than simple C++ default compilation, where you need to run address sanitizers etc. to do the same as rustc?
Re:C++ vs. Rust Build Times by strager on Friday January 06, 2023 @01:30AM (#34271650)
No.

If I was going to run sanitizers with the C++ code, for a fair comparison, I would also run the Rust code with Miri. (C++ sanitizers check unsafe code, but without Miri, rustc mostly checks only safe code.)

Re:C++ vs. Rust Build Times by vegerot on Friday January 06, 2023 @01:45AM (#34271734)
What about the other way around? Trying to cripple rustc until its features are closer to clang's? For example, removing some of the sanitizers or borrow checker in rustc?
Re:C++ vs. Rust Build Times by strager on Friday January 06, 2023 @01:57AM (#34271781)
That's a great idea! Unfortunately, I think there's no way to disable rustc's borrow checking or other safety features.
Re:C++ vs. Rust Build Times by insanitybit on Friday January 06, 2023 @02:20AM (#34271887)
Just fwiw you can run rust code with sanitizers, no need for miri, which works differently.
Re:C++ vs. Rust Build Times by strager on Friday January 06, 2023 @04:03AM (#34272578)
Oh, I did not know this.
Re:C++ vs. Rust Build Times by jupp0r on Friday January 06, 2023 @02:19AM (#34271880)
Compilation doesn't necessarily need strict checks, it's really just translating source code into machine code. It happens to be the case that we mostly do both using the same tools because compilers need to have a good understanding of type systems to do their job well, but it's not a strict requirement. A great counter example is esbuild, which will compile TypeScript but not perform actual type checks for performance reasons. This is possible because TypeScript types have no representation at runtime. and are really only a development tool.
Re:C++ vs. Rust Build Times by insanitybit on Friday January 06, 2023 @02:20AM (#34271885)
That doesn't seem relevant. The article isn't asking "is C++ better than rust?" or trying to make qualitative judgments on overall "worth" - it's an exploration of one area, an apples to apples (as close as possible, of course) comparison of compilation speed given a set of optimizations and constraints. Rust may do more analysis, c++ may have other tooling, but that's not relevant - the speed is the speed.

Sanitizers should primarily impact runtime performance anyways, but that's moot.

Re:C++ vs. Rust Build Times by strager on Friday January 06, 2023 @04:04AM (#34272588)
> Sanitizers should primarily impact runtime performance anyways, but that's moot.

In my experience, ASAN has a huge negative impact on build times for C++ code (Clang and GCC).

Re:C++ vs. Rust Build Times by insanitybit on Friday January 06, 2023 @09:40PM (#34284542)
I guess that makes sense since they have to inject an instrumentation pass.
Re:C++ vs. Rust Build Times by fooker on Friday January 06, 2023 @02:23AM (#34271896)
That’s a language design issue, by choosing your language you choose much you want the compiler to do. So it doesn’t make sense to force that choice for the sake of comparison.

Similarly, Swift code is slow to compile for because it has a weird type system.

Re:C++ vs. Rust Build Times by menaerus on Friday January 06, 2023 @03:05AM (#34272192)
And quite the reason why I think this was a poor choice of rust design because not being able to opt-out from static analysis, e.g. borrow checker, makes me having to pay the price for it every time even when I don't want to. This as a result has unfortunate consequences on iteration times, which C++ has been infamously known of but in this respect it plays better imo then rust.
Re:C++ vs. Rust Build Times by zozbot234 on Friday January 06, 2023 @04:42AM (#34272816)
Rust borrow checking is quick, as shown by cargo check. The problem with Rust compiles is (1) proc macros, when they are used and (2) the sheer amount of monomorphized generic code that rustc outputs for LLVM to deal with.

Monomorphized generics should always be written to hand off substantive work to a single function that can span multiple instantiations whenever possible, but this is not done to the fullest extent in current Rust. (Partly because Rust const generics are still at the MVP stage, so it's not possible to, e.g. write a generic function that depends on known features of a type, such as alignment or size.)

Re:C++ vs. Rust Build Times by vegerot on Friday January 06, 2023 @05:12AM (#34273012)
No need to speculate These timings are all in the paper! Check the “Cranelift backend” section
Re:C++ vs. Rust Build Times by menaerus on Friday January 13, 2023 @02:54PM (#34373026)
> Rust borrow checking is quick, as shown by cargo check.

Looks like monomorphization part is not caught during cargo check according to https://github.com/rust-lang/rust/issues/49292

Reporter of the bug says:

> All of those happens somewhere inside librustc_mir, most of them being monomorphization. This corresponds to translation item collection pass, which takes nontrivial amount of time.

Which essentially means that cargo check is not doing all the checks that cargo build will do, so the comparison seems to be a bit off, at least for the time being. And consequently this inconsistency can easily lead to the hypothesis of LLVM backend being the bottleneck.

I guess the only reasonable way to know for sure where are the biggest bottlenecks in build times is to have something similar to clang's -ftime-trace but I couldn't find anything similar existing in Rust.

From what I understand, monomorphization and rust macros are essentially C++ templates in a nutshell, and probably less than, yet C++ is compiled much faster. Given that both clang and rust share the same LLVM backend, this seems like an indication to me that the bottleneck is rather in the frontend and not in the backend. It also could be that Rust frontend pipeline is not quite optimized yet so that it puts more pressure to LLVM backend than what the clang does but seems like we can't really know that for sure.

Re:C++ vs. Rust Build Times by tumdum_ on Friday January 06, 2023 @04:54AM (#34272897)
Almost always the cost of borrow checker is trivial compared to code gen (and sometimes instantiation of generics). You can see that easily by comparing time of `cargo check` and `cargo build`.
Re:C++ vs. Rust Build Times by vegerot on Friday January 06, 2023 @05:13AM (#34273020)
The specific time borrow check takes is in the article. Check out the section on “ Cranelift backend”
Re:C++ vs. Rust Build Times by strager on Friday January 06, 2023 @05:43AM (#34273216)
Note: I wouldn't draw any conclusions from the -Zself-profile data in the article. It looks at just one rustc invocation, and is one sample.
Re:C++ vs. Rust Build Times by strager on Friday January 06, 2023 @04:01AM (#34272571)
The article does not account for this. I wanted the fastest build-test cycle. It sounds unfair to artificially slow down C++ compilation just because Rust has undisablable checks.

EDIT: Oops, I realized I already replied two hours ago.