9 ms·
Debunking that C++ is faster and safer than Rust
- brenden2 7y agoStrictly speaking it's the compiler that generates faster code.
- mschuetz 7y agoYou'll never get faster code out of JS, though. Language design matters a lot.
- dathinab 7y agoWell, there are some close to native code speeds for some (very constrained) use-cases. They way it's done is that if you use a certain stile of C the compiler will speculative do assumptions about the code allowing it to basically add all the C optimizations. Except that it always has to check if the assumptions are uphold and then fallback and that because it's a JIT it has much less time to optimize the code and do cross-code-section optimizations.
- moonchild 7y agoSounds like the 'sufficiently smart compiler[1] myth. 1. https://wiki.c2.com/?SufficientlySmartCompiler https://wiki.c2.com/?SufficientlySmartCompiler
- dathinab 7y agoIt's both. With bad language design you won't get fast code (at least without going through insane loops). But even with good language design the compiler need to use them, which needs time to be implemented etc. So in practice it's often more a mixture between how easy/hard the language makes optimizations and how much work (with given expertise) was put into the compiler optimizations. Through there are insane optimization which need to high amount of knowledge about the code and as such which you will have a really had time to ever realize with Asm,C or similar. But most time they aren't worth it as getting them right is hard and the time is often better spend with adding more straight forward optimizations, maintaining the compiler code etc.
- saagarjha 7y agoThey both depend on the same compiler backend, so it's really the language here.
- kibwen 7y agoThis is an endlessly perplexing headline, as its core assertion, "C++ is faster and safer than Rust", is what the body of the article spends its whole time attempting to refute. A more accurate title for the content of this article would be "Debunking the myths that Rust is not safer or as fast as C++".
- gautamcgoel 7y agoYeah I was really confused too. You expect that author to bash Rust in favor of C++, but he does the exact opposite.
- dochtman 7y agoSome scare quotes might do the title some good, too.
- scottLobster 7y agoClickbait for software engineers.
- dang 7y agoOk, we've debunked the title above.
- junke 7y agoAre comments about the title of articles offtopic? Because sometimes comments that discuss titles, without being inflammatory, are flagged, and sometimes they aren't.
- dang 7y agoI suppose they are in principle, but we don't have an explicit rule against them, and user passions around titles burn so hot that it wouldn't make a difference if we did. There are also so many of them that you're not likely to see consistency around what gets flagged vs. not. Are there specific links we could look at?
- aninteger 7y agoThis article is a bit click-baity. It's more about busting myths by a particular C++ programmer against rust. Anyway for a truth (well maybe it is a myth?) that we can't bust yet... Rust is simply not available on all platforms that C++ is. Two platforms that I think are missing: * 16-bit MS-DOS * 32-bit PowerPC (Linux)
- jefft255 7y agoHow much active development of new software is being done for MS-DOS? Really curious to know.
- dathinab 7y agoIs anyone besides embedded doing _any_ 32-bit dev outside of maintaining legacy code? I mean 64-bit PowerPc is by now around 17 Years old and even 15 years ago some of the most widespread users of PowerPc (Xbox) switched to using 64 bit PowerPc architecture...
- nv-vn 7y agoImportant to note that PVS-Studio is a static analysis tool for C++. As such it would definitely be in their interest to have more people use C++, but since they're arguing in favor of Rust here it definitely speaks to the fact that their analysis is unbiased.
- steveklabnik 7y agoThey have also previously posted some negative ones too, so I was surprised to read this for that reason. I think it also contributed to my confusion around the title vs text, that others have expressed in this thread.
- dathinab 7y ago> The bug has been present in LLVM since 2006. It's an important issue as you want to be able to mark infinite loops or recursions in such a way as to prevent LLVM from optimizing it down to nothing. Fortunately, things are improving. LLVM 6 was released with the intrinsic llvm.sideeffect added, and in 2019, rustc got the -Z insert-sideeffect flag, which adds llvm.sideeffect to infinite loops and recursions. Now infinite recursion is recognized as such (link:godbolt). Hopefully, this flag will soon be added as default to stable rustc too. Be aware that this isn't a LLVM bug but a direct consequence of the insanity of C++ specification (wrt. forward progress induced undefined behaviour). The C++ rules around forward progress allow C++ compilers to faster eliminate code which doesn't produce any observable side effects (without the code triggering undefined behaviour) but it also removes code which intentionally or not hangs the process in a busy loop or is intended to cause a stack overflow... (e.g. for testing protections). The flag currently isn't added to rust as the penalty effect on compiler time (needs to run more analysis) and runtime (doesn't eliminate all code it should) is currently pretty high. So this might take a while until _fully_ fixed (you always can pass in the flag yourself if you want). Through some fixes which make it harder to hit the bug until a proper solution is found _might_ not be so far of (I hope).
- steveklabnik 7y agoWe're currently talking about this in Rust-land: https://blog.rust-lang.org/inside-rust/2020/03/19/terminating-rust.html https://blog.rust-lang.org/inside-rust/2020/03/19/terminatin... (comment thread: https://internals.rust-lang.org/t/resolving-rusts-forward-progress-guarantees/12003 https://internals.rust-lang.org/t/resolving-rusts-forward-pr...)
- Arnavion 7y agoIt's indeed not a bug for C++, but to be clear it is a bug for C and Rust that use LLVM but don't have that same guarantee as C++. LLVM assumes that guarantee holds for all frontends, and added the sideeffect opcode so that frontends for languages that don't have that guarantee have a way out.
- sneeuwpopsneeuw 7y agoVery interesting article. Most of the time I do not like myth busting articles because they are to much focused on opinions and taking things out of context but this one is very well written and fully based on facts on both sides. Thanks for sharing.
- kpp 7y agoThank you!
- saagarjha 7y ago> As you can see, the documented behavior and the absence of undefined behavior due to signed overflows do make life easier. Not having undefined behavior does make life easier, but having it be defined and then giving the example which benefits from the way that Rust chooses to define it is not really fair. > With less effort, Rust generates less assembly code. And you don't need to give any clues to the compiler by using noexcept, rvalue references and std::move. When you compare languages, you should use adequate benchmarks. Actually, the issue is that C++ just can't match Rust's semantics here. By default it will allow for exceptions, by default it will copy; if you watch the talk rvalue references cause the double indirection and fixing this would require some changes in the language to accommodate the use case.
- mehrdadn 7y ago> the example which benefits from the way that Rust chooses to define it I'm actually struggling to see what the practical benefit is in having it wrap around. The program is still producing garbage at that point, which you're not handling, so why not let the compiler just forget about that case just like you already did?
- saagarjha 7y agoI mean, if you're doing two's complement arithmetic as the example relies on then it's certainly useful. If you want any other behavior, then it is not. (Rust's differing behavior between different optimization levels IMO makes it basically impossible to use usefully, FWIW.)
- mehrdadn 7y agoHow likely is that "if", though? How often when you see signed multiplication in code do you find people are actually paying attention to the negative results and planning around 2's complement behavior? I know there are occasions in which people do this, but it's so incredibly rare compared to when they multiply two numbers. And on the rare occasions when the bit patterns actually matter to them and they're paying attention, it's not like they have no way to solve the problem in C++.
- deleted 7y ago[deleted]
- acqq 7y agoJust looking from afar, I don't have time to analyze every other claim, but this one: > Both C++ and Rust have generated identical assembly listings; both have added push rbx for the sake of stack alignment. Q.E.D. seems to be completely wrong: a decent compiler is able to align the stack without "touching" it. For the variables inside of the function to be pushed to the aligned stack position, only different offsets have to be calculated. For the stack itself to get to be aligned, only the register has to be updated, surely nothing has to be pushed. So something else must have been happening there, and I don't have time to analyze what, but I'm sure push is surely not necessary for alignment alone.
- pitaj 7y agoIt may just be for alignment. A push may be just (having a specific case in the CPU) as fast as updating the SP.
- ridiculous_fish 7y agoYou are correct, it has nothing to do with alignment. rbx is a callee-saved register, so the callee saves it.
- deleted 7y ago[deleted]
- thayne 7y agoDo you mean thar rax is caller-saved?
- ridiculous_fish 7y agorax is indeed caller-saved, but I was referring to rbx. In this code: int bar(int a, int b) { int z = a * b; foo(); return z; } https://godbolt.org/z/a8Y35r https://godbolt.org/z/a8Y35r Where to put `z`? It has to be someplace where `foo` won't clobber it, like a callee-saved register - in this case ebx. But now `bar` is responsible for saving the value of ebx for its caller. That explains the push and pop. Alignment is unrelated; this is just about calling conventions.
- ridiculous_fish 7y agoHere's a case I stumble on: Rust seems to generate unnecessary branches. Compare copying an optional range: C++: https://godbolt.org/z/VCf638 https://godbolt.org/z/VCf638 Rust: https://rust.godbolt.org/z/jRFiw_ https://rust.godbolt.org/z/jRFiw_ I think the key difference here is that C++ allows specializing optional on trivial types - can anyone shed more light?
- sigwinch28 7y agoIt looks like the C++ version always does the copy, regardless of whether or not the optional is empty, whereas Rust only bothers copying if there's anything there. Is this a remnant of #[inline] on Option's Clone impl methods? With "larger" types (e.g. an optional several-GiB array) it seems like this could save some time depending on where things are in memory.