Hacker News new | past | comments | ask | show | jobs | submit login

I think you might be surprised just how much time is spent in codegen- and optimization-related code.

For example, a bit over 75% of the time needed to compile the regex crate can be attributed to codegen- and optimization-related events, with a bit over 64% of that time spent in LLVM-related events specifically [0]. Granted, I'm not certain whether this is a release or debug build, but it does show that there is room for significant wins by switching backends.

As for why C can compile quickly with an LLVM backend while Rust can't, I'm not sure. I've read in the past that rustc generates pretty bad LLVM IR to pass to the backend, and it takes time for LLVM to sort through that, but there's probably some other factors in there too.

[0]: https://blog.rust-lang.org/inside-rust/2020/02/25/intro-rust...




I wonder how much of it is just code style. A simple for-loop in C is probably going to be an iterator blob in Rust with an order of magnitude more code for the backend to chew through.


Part of it may be code style, but another part comes from just how much LLVM IR the frontend generates for just about any code style.

Part of the reason LLVM runs so much faster on C than on Rust is that Clang is smarter about generating less/better IR from the start, so LLVM's optimizer has less of a hole to dig itself out of.


I think it's actually mostly the language. Rust idioms just generate a lot of code.

Clang is in a worse spot than rustc is in terms of emitting good LLVM IR, since it has no IR aside from the AST. By contrast, rustc has MIR which is more amenable to optimizations. At this point I'm fairly sure the problem is just that C code naturally generates less IR than Rust code does. All those function calls that go into iterators, array indexing, containers, etc. etc. add up quick.


Modern C++ is in a similar situation with iterators, indexing, etc. and it's still often faster for LLVM to get through.

MIR optimization can help close the gap but that still involved generating "garbage" that takes extra time to codegen (in Debug) or optimize out (in Release).

Being smarter about generating IR, whether MIR or LLVM IR, is still an area rustc has a lot of room to improve, even given Rust's idioms. E.g. stuff like this: https://github.com/rust-lang/rust/issues/69715


I think people are way too hard on rustc. That linked issue is pretty esoteric and is a good example of the low-hanging fruit being picked. I doubt it'll move the needle much.


It's hardly esoteric when it affects some of the most common (and commonly-inlined) methods in the entire language, which are a large part of Rust's unique idioms.


We can compare them! https://godbolt.org/z/-Fzuqs

(My screen is small so it's tough for me to read these results, to be honest...)


I believe that is the IR after the optimizer has chewed through it.


Oh duh. Should be obvious that you can drop the -O flags, at least.


Yes, which gives about 40 lines of IR for C, and about 1300 for Rust (Godbolt truncates it at 500).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: