5 ms·
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
by edflsafoiewq 7y ago
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.
- steveklabnik 7y agoWe can compare them! https://godbolt.org/z/-Fzuqs https://godbolt.org/z/-Fzuqs (My screen is small so it's tough for me to read these results, to be honest...)
- edflsafoiewq 7y agoI believe that is the IR after the optimizer has chewed through it.
- steveklabnik 7y agoOh duh. Should be obvious that you can drop the -O flags, at least.
- edflsafoiewq 7y agoYes, which gives about 40 lines of IR for C, and about 1300 for Rust (Godbolt truncates it at 500).
- Rusky 7y agoPart 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 7y 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 7y 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 7y 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 7y 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.