8 ms·
Rust Optimizations That C++ Can't Do
- DannyBee 10y agoErrr, C++ can do this. It just requires analysis to do it in some cases (not all). As a trivial example, the point about const is wrong (at least as far as i understand it). you can const cast stuff, but you still can't modify it, to do so is UB: Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue both result in undefined behavior. So yeah. As for the abi, any abi arguments are silly. really. the compiler can always clone the function, rename it, and do what you want. or inline it. or .... If it really matters, you'd also just change the ABI. There is no C++ standard ABI. There's one a lot of compilers use. If we could get x% better performance using a different one, people would do that :) The corollary is "If you think you can only get x% better performance by doing that, you are probably wrong". (I'm excluding things that are low level abis, like x32, etc, since we are talking about language abis)
- Galanwe 10y ago+1 for the ABI part. Nothing prevents you in C++ to use multiple ABIs depending on how you plan to build / distribute your library. You can use e.g. `fastcall` inside you library, because you know that you will be using the same compiler to build it and don't expose these symbols publicly, and use `cdecl` for the public facing functions.
- snovv_crash 10y agoEspecially for anything templated or inlined. Seriously, I'm getting sick of these Rust/Go/Hype is better than $competitor because $edgecase. Yes, but what if I want to do sparse matrix manipulation? What if I want to interact with hardware, and set registers? What if I want to use the library Bob wrote back in 2001, because it is really well tested and we've never encountered a bug since 2003?
- stestagg 10y agoRust is actually lovely for peripheral register manipulation. Check out the zinc project for some great examples. Likewise, linking to external libraries is trivial. I'm not saying that rust is better than any other language. But those examples aren't factors in this religious debate
- pathsjs 10y agoLinking to external C libraries is trivial. If the library is written in C++ and does not expose a C api, things can become much more difficult
- snovv_crash 10y agoNever mind using a C++ header-only library.
- int_19h 10y agoThe point about const is not wrong. What you said about const is true if, and only if, the object was originally const - i.e. you can't do this: const int n = 1; const_cast<int&>(n) = 2; However, when the function receives a pointer or reference to const, it doesn't know whether the referenced object is originally const or not. It only knows that it cannot write through that pointer; but someone else still might be able to do so. Example: int n = 1; void foo() { n = 2; } int bar(const int& x) { int y = x; foo(); return y + x; } bar(n); Even though x is reference to const here, the compiler has to do two reads from x here, because foo can - indeed, does - change the value referenced by x. Worse yet, the function can often change it itself via aliasing: int n = 1; int bar(const int& x, int& y) { y = x; return x; } bar(n, n); Again, this is perfectly legal. The problem is that both C and C++ lack a way to say "this is a pointer or reference to something that is immutable", as opposed to "... something that you can't mutate". The other problem is that C++ (but not C, thanks to "restrict") lacks a way to say "the object referenced by this pointer or reference is not aliased in any other respect that matters to this function". Rust, OTOH, does let you say that something that's referenced is immutable. In fact, it is the case by default. So the compiler can aggressively optimize around the fact. In C++, they can only optimize as well when the compiler can do a full program analysis, and determine that the referenced value is immutable in practice. Which can be hard to do for non-trivial programs, and outright impossible to do across dynamic library boundaries.
- gpderetta 10y agotemplate<class T> struct const_ { const T value; }; const_<T>& freeze(T&); T& thaw(const_<T>&); do_something(const_<T>& x) { /* the language guarantees that x can't be mutated from anywhere */ }
- pjmlp 10y agoYou can cast away const.
- gpderetta 10y ago'value' is a const qualified object, not a const qualified reference. If you cast away the const and write to the object you are into UB, ergo the compiler can assume that isn't done. Note that the trick still doesn't help much in practice: GCC conservatively still won't optimize based on const qualification of values.
- berkut 10y agoExactly - C++ compilers definitely can do this, and it's one of the things ICC's noticeably better than GCC and LLVM at, especially where intrinsics are involved. If you have a struct/class type union wrapper for SIMD instrinsics which contains something like __m128 and pass that by reference, GCC and LLVM will often implement this as an effective pointer onto the stack (or wherever the struct is). ICC can often detect this, and keep it as a by-value __m128, which is often more efficient and keeps stuff in the registers.
- Const-me 10y agoAnd MSVC has __vectorcall keyword for ICC-like calling convention that passes arguments in those SSE registers.
- wfunction 10y agoI find it funny and think it's pretty telling that there are bold claims made that are only crossed out after the author is corrected. I feel like a C++ compiler (or any compiler, really) could do practically any optimization that any other one can do, unless that language has explicit restrictions prohibiting such an optimization (e.g. like if the language prohibits the implementation from generating multiple copies of code). The question is just how smart the compiler would need to be, and that changes as a function of how strict the language is. The more loose the language is, the more smart the compiler needs to be. The more information you encode in the type (or make easy to infer), the less smart the compiler needs to be. Whether it's practical to develop a smart-enough compiler in a reasonable amount of time is probably the real issue.
- Galanwe 10y ago"unless that language has explicit restrictions prohibiting such an optimization" It is indeed true that C++ is not able to easily express aliasing. It is extremely cumbersome and error prone to properly specify the aliasing rules when using pointers (especially when you start using char* and void*). So either you force the programmers to place a lot of `restrict` everywhere (expect 1/100 programmer is able to understand/do this properly), or you just fallback to not optimize these cases to their maximum (safe), or allow the compiler to aggressively try to guess the aliasing by itself (and enjoy mind crushing debug sessions later on).
- DannyBee 10y agoWe used to have languages, like fortran, that were incredibly explicit, and people didn't like that either :) " or you just fallback to not optimize these cases to their maximum (safe), " No, you fallback to runtime aliasing checks if it's important enough. If something can later prove they alias or not, it eliminates the conditional.
- flamedoge 10y agoQuestion is when do you stop? At what point does adding runtime checks become too expensive? I doubt you want to end up with gazillion checks for non-overlap. C and C++ both seem to lack user-specified type aliasing relations. You have to work with standard base types. I want to be able to declare my own type that is unique on its own and aliasing relations with other types.
- dkarapetyan 10y agoIt's not just about the compiler being smart. There is nothing more you can do with x -> 2 + x other than compile it as is. Now if I somehow prove that x will never be greater than 10 then all of a sudden the compiler can in theory just turn that addition into a lookup table or do all sorts of other things I couldn't think of. So it's more than just sufficient cleverness. It is also about the language providing you the tools to express certain invariants that the compiler can mechanically verify and then use in an optimization pipeline. Edit: This is mostly out of context now and can be ignored.
- lomnakkus 10y ago> There is nothing more you can do with x -> 2 + x other than compile it as is. You can often inline it. Now that may or not yield improvements in speed, but more to the point: It's quite possible that it's used in a context where x is bounded sensibly, and you can optimize further from there.
- dkarapetyan 10y agoAt which point you are hoping the black box known as the compiler is "sufficiently smart". It's nice to hope but not in the context of compilers.
- Rusky 10y agoThat kind of optimization is well outside the bounds of "sufficiently smart." It happens routinely.
- DannyBee 10y agoUh? This happens all the time.
- dkarapetyan 10y agoWe must be using different compilers then. Last time I checked there are some heuristics about what gets inlined and when exactly those heuristics are triggered. Something about compilers being terminating programs and stack depth not being an unbounded resource. I don't know though. Maybe I was reading the wrong literature. What are your sources?
- kccqzy 10y ago> a "const" value can be modified through other mutable references, so copies induced by pass-by-value could change program behavior. This doesn't mean the compiler can't make this optimization. Unless we are talking about multiple threads here (which we aren't), there is no other code that is executing that can change what this const reference is pointing to. If we are talking about threads, then data races like this are UB in C++, and the compiler can do anything it wants, including emitting nonsense code.
- Ace17 10y ago> Unless we are talking about multiple threads here (which we aren't), there is no other code that is executing that can change what this const reference is pointing to. ... except if your code calls potentially 'impure' functions. void f(const int& value) { const int a = value; g(); const int b = value; assert(a == b); } One can't tell if the assertion will succeed. Indeed, 'value' might have been modified as a consequence of calling 'g'. You don't have this uncertainity if 'value' is marked as immutable.
- DannyBee 10y ago"One can't tell if the assertion will succeed. " If value was originally a const object (IE immutable), it is well defined and guaranteed to always succeed. IE const char* myString = "Hallo World!"; const_cast<char*>(myString)[1] = 'e'; This is illegal, the same as anything modifying value would be if it was originally const. If value was originally not a const object, it wasn't immutable, so anything goes. "You don't have this uncertainity if 'value' is marked as immutable. " You can, in C++ mark the original immutable in a way that you can tell if the assertion will succeed.
- int_19h 10y agoThe compiler can't tell the difference (in general), because what it sees is a const int&, but the actual referenced object may be a non-const int.
- 10y ago
- joaodlf 10y agoI love new languages (unlike a lot of HN, I'm excited about Rust AND Go), but please remember that C/C++ compilers have been around, sometimes for more than 30 years. Do not think new compilers will just waltz in and suddenly topple all this previous work - That will be a long march.
- adamnemecek 10y ago> unlike a lot of HN, I'm excited about Rust AND Go If there is one thing there isn't enough of on hn, it's discussions about Go
- kibwen 10y agoRust uses LLVM, which is a backend developed with C and C++ in mind that has been under development for a bit over a decade now. As long as you basically pretend to be C++ at a low level (which Rust does), then the two remaining factors to consider are optimizations that happen in the front end and language-level semantics that enable language-dependent optimizations. (We should also consider that LLVM isn't as venerable as GCC, but it seems that (for C and C++ code) there's no longer any clearly superior choice between the two wrt general-purpose code generation (would be happy to hear arguments to the contrary)).
- Manishearth 10y agoAlias analysis has been the holy grail of compiler development for a while. In C++ it's a global, imperfect, and expensive analysis. In Rust you get it for free. C++ can't do the same thing Rust does because it would be a breaking change to have finer-grained distinctions between the types of references. This is a matter of language semantics, not compiler implementation. rustc isn't doing anything new here, LLVM already knows how to make this optimization and is making it.
- tmccrmck 10y agoWhat specifically makes you think alias analysis is the holy grail of compiler optimization?
- simion314 10y agoAFAIK there are optimization that Java/C# can do at runtime that compiled languages can't do.
- hellofunk 10y agoSomeone should fairly offer as a counterpart any article showing the myriad of reasons why C++ generates much faster code than Rust in most cases. There are many such articles out there I've seen from time to time.
- AstralStorm 10y agoTypically by not generating code, such as when more advanced templates are used or constexpr, or by using a specialized or partially specialized version. Traits, while cool, ate not potent enough at times.
- hellofunk 10y agoIt's more than that. While I understand that technically it should be possible to write code in either language that creates the same machine code as the other, the common idioms of each language lead to, in most cases, the C++ generated code being faster. I think it is a classic tradeoff in the safety a language provides vs. its overall performance. For example, the Mozilla team has to use unsafe blocks in the Rust codebase to get the same performance as C++ for those areas where it is important. That is not idiomatic Rust. Mozilla resorts to unsafe blocks just to handle basic doubly-linked lists, as well as optimized hashtables, in order to get similar performance to C++ data structures. My point is that when you stick to the philosophy of Rust, you will in general have slower code that when you write normal idiomatic C++.
- kibwen 10y ago> That is not idiomatic Rust I'm not sure where one would get the idea that `unsafe` would be entirely forbidden in idiomatic Rust. "Idiomatic" Rust acknowledges the danger and subtle pitfalls of unsafe code, and thereby discourages the use of `unsafe`, but also acknowledges that it is still possible to build safe abstractions out of internally-unsafe code. > in order to get similar performance to C++ data structures By Rust's definition, every line of code in a C++ codebase counts as unsafe code. :P If Rust can get equivalent performance to C++ while only using exposing 1% of the code to unsafety, that's an enormous win.
- filmor 10y agoPlease drop the ?m=1 from the URL such that the correct page version is resolved on Desktop.
- socmag 10y agoI think this is a very objective and in-depth article on the matter. "Criticizing the Rust Language, and Why C/C++ Will Never Die" https://www.viva64.com/en/b/0324/ https://www.viva64.com/en/b/0324/ Not sure why certain members of the Rust community aren't happy with just having a neat language. Making one false claim after another isn't doing them any favors. It's just aggravating, especially when people who should know better start to believe the hype. FAKE NEWS!
- kibwen 10y ago> certain members of the Rust community Fortunately the overwhelming majority of the community isn't afraid to call this post out, judging by the comments at https://www.reddit.com/r/rust/comments/63ijkw/rust_optimizations_that_c_cant_do/ https://www.reddit.com/r/rust/comments/63ijkw/rust_optimizat... :) I've never seen the author engage with the Rust community in any great capacity (they're not a regular in any forum that I frequent, and I can't name any libraries that they've written), so I wish he'd take the time to learn Rust more before blogging about it.
- socmag 10y agoThanks for posting this because this has been getting out of hand recently, and it's very good to see some sanity. There is an opportunity to do some very cool things with Rust and I think most people would love to see a "better together" mindset being shared.
- dbaupp 10y agoIt's worth noting that the sanity you're appreciating is, IME, very typical of the Rust community. Mistakes/overblown claims will get called out (if someone notices them), and "better together" is something people generally embody: e.g. even the language itself incorporates good ideas from "competitors".
- notacoward 10y ago> Not sure why certain members of the Rust community aren't happy with just having a neat language. Saying this isn't going to do my karma any good, but it's almost certainly because they're feeling competitive pressure from the even more numerous Go developers doing the exact same thing with their language. A lot of people feel like it's time to move on from C and C++, especially to something with better memory-safety properties. (This is for low-level infrastructure code, mostly. Other kinds of code have been better written in higher-level languages for a long time.) Rust developers/advocates feel that it has the best current answer to that need. They might even be right. Then they see the total deluge of Go propaganda, encouraging people to adopt what they feel is a less-good approach. They know that "just having a neat language" won't be enough to overcome that. Therefore, out of a completely laudable desire to help others avoid a mistake, they ratchet up their own rhetoric to match. Some get a bit carried away. If the goal is to discourage overly ardent advocacy for Rust, we also need to discourage the same for other languages as well - not just Go but also Swift, Erlang/Elixir, *ML, and so on. The pattern of people becoming tiresome about their choice of programming language (or paradigm) has existed for a long time.