5 ms·
Are there theoretical boundaries that prevent Rust from ever reaching C performance or is this just a practical question of compiler optimization?
by k__ 8y ago
Are there theoretical boundaries that prevent Rust from ever reaching C performance or is this just a practical question of compiler optimization?
- thsowers 8y agoNot qualified to answer, but I did notice recently that Rust appears to generate more compiler code than equivalent C, but I don't think this realistically has a negative performance impact. You can check this out on godbolt (https://godbolt.org/ https://godbolt.org/)
- raphlinus 8y agoIt's possible to set compiler flags to optimize for size. That said, I've also seen large binaries, and monomorphization can contribute to that. Some projects are taking that more seriously, for example miniserde is designed to explicitly minimize monomorphization and this improve both code size and compile time.
- thsowers 8y agoHadn't heard of miniserde before, awesome find, thanks!!
- steveklabnik 8y agoIt really depends on exactly what you mean. There's one significant thing that's in C that's not in Rust: alloca. If you need that, well, we've talked about it, but haven't accepted a design, and some people never want to gain it directly. EDIT: another comment pointed out goto; I have no idea what the performance implications are but I’ll have to add that to my list of “stuff C has that Rust doesn’t.” Sometimes, you need nightly-only features. This means that, in some sense, today's Rust is slower, but tomorrow's Rust may not be. Sometimes, "equivalence” is the issue. You can turn off bounds checking for array access, for example, but most Rust code doesn't, for hopefully good reason. Sometimes those checks are elided, and so it's exactly the same. Sometimes they're not. Sometimes they're not and that inhibits other optimizations. Is unsafe Rust "equivalent" to the C? Or does that not count? (Unsafe Rust is not always faster than Safe Rust...) At the theory level, I can't think of anything off the top of my head that would inherently make Rust slower than C, generally speaking. There's also some degree of argument that in theory, Rust should be faster than C, thanks to how much more we know about aliasing, etc. That's a whole other can of worms...
- simcop2387 8y ago> Sometimes those checks are elided, and so it's exactly the same. Sometimes they're not. This is something I've personally thought about a lot and I have no idea how I'd actually want it implemented either on the backend or in the syntax, but I think it'd be useful to have some way to say "I want these bound checks to be elided, if they can't be then make that an error". Similar to how you'd decorate a pure function in other languages to say that it can't cause side effects or depend on side effects for what it's doing. I think that's probably a high level ask that's probably significantly more difficult that it seems initially too.
- edflsafoiewq 8y agoCan this sort of information be gotten out of LLVM?
- steveklabnik 8y agoYeah that'd be very interesting.
- andrewflnr 8y agoYou probably just want a dependently typed language. Proving bounds at compile time is an introductory exercise for the likes of Idris.
- zem 8y agoats [http://www.ats-lang.org/ http://www.ats-lang.org/] is a C-level language with dependent types
- lodi 8y agoNote that ATS has a limited version of dependent types as compared to Idris/Agda/Coq/etc. As I understand it, you can't run arbitrary (recursive, etc) functions at the type level. But even this 'lite' version lets you express preconditions like "n is a multiple of 4" or "the source array and destination array must not overlap in memory".
- pcwalton 8y agoWell, the answer should be "no" for unsafe Rust, since you can mechanically translate any C to unsafe Rust and vice versa. For safe Rust, sure, there are limitations: you can't turn off bounds checks, for instance. But all Rust is partially unsafe Rust, because the standard library uses unsafe code ⃰. So it's a fuzzy distinction. ⃰ ⃰You wouldn't want it any other way. Implementing low-level functionality as unsafe code allows us to write code, not compiler-code-that-generates-code.
- gameswithgo 8y agonote that when you use iterators, bounds checks do get elided.
- edflsafoiewq 8y agoIn a sense you can mechanically translate between any Turing complete languages, but there's not a "naive" translation, so to speak, because C can have irreducible CFGs and Rust cannot. It's not clear to me that reflowing a CFG can always be done without a performance impact.
- pcwalton 8y agoTrue, I forgot about goto.
- edflsafoiewq 8y agoNot only true goto but also switch and computed goto. AFAIK C/C++ are essentially the only games in town for these. No modern language has even corrected the purely syntactic limitation that prevents nested switches (by eg. allowing you to attach label names to switch blocks and cases).
- legulere 8y agoYou can convert an irreducible CFG to a reducible CFG with a loop and a switch case(/match in case of rust): In pseudocode Goto = Start; While(Goto!=End) { Match(Goto) { ... } } It’s a pretty basic obfuscation technique. Compilers will happily unroll this leaving you with an irreducible CFG.
- nwmcsween 8y agoBounds checking will inhibit vectorization, panicking will bloat code, maybe I'll make a comparison against a rust coreutils bin vs something like sbase.