4 ms·
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 r
by Rusky 7y ago
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.
- pcwalton 6y agoI 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.
- Rusky 6y agoModern 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 https://github.com/rust-lang/rust/issues/69715
- pcwalton 6y agoI 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.
- Rusky 6y agoIt'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.