3 ms·
One of the strengths of C++ is just that, that the more information you can provide, the better opportunities for optimizations. If you're saying that Rust adds
by fwsgonzo 3y ago
One of the strengths of C++ is just that, that the more information you can provide, the better opportunities for optimizations. If you're saying that Rust adds more information than C++, then sure, it would be possible for it to optimize better. But is that the case right now?
- hmry 3y agoYes, for example Rust has significantly more detailed aliasing information than C++, since Rust has the borrow checker while C++ has to get by with weird type-based heuristics (and doesn't even have a way to override that in the standard, though most compilers allow restrict in C++ mode as an extension)
- Yujf 3y agoThe borrow checker gives the compiler significantly more information about memory. The rust compiler exposed some bugs in LLVM because LLVM tried to use this extra information, but because it could never do so for cpp the code was buggy.
- tialaramex 3y agoHere are just a couple of examples related to having better information: 1. Take one of those most popular basic types provided by the C++ stdlib, std::vector<T> a growable array type and compare the analogous Rust type Vec<T> std::vector<T> has a reserve method, but it's actually not equivalent to Vec::reserve instead it's equivalent to Vec::reserve_exact and there is no analogue for Vec::reserve provided, as a result without re-implementing the amortized exponential growth trick yourself you can't achieve similar performance benefits. The reserve vs reserve_exact distinction allows programmers who know something to help the type perform well. 2. The Guaranteed Niche Optimization. If Rust's compiler can see why type T doesn't need all possible bit patterns for its memory layout, then Rust guarantees that sum types consisting of a single value plus type T are optimised to the same size as T. C++ can approximate this using specialization for library types such as std::optional (but it typically doesn't) however it cannot provide the guarantee and in practice doesn't deliver the resulting performance. Rust's &T has such a niche (the "null" pointer value), so do (Unix) OwnedFd and (Windows) OwnedSocket and the non-zero integers like NonZeroU32. Thus all of these types have the guaranteed optimisation when used in your own types and such sum types can be used exactly like "sentinel" values in C except that they're type safe so you'll write fewer bugs. There are a lot of these leaks in the C++ language and standard library, and they add up. If you want to counteract them your C++ development gets slower and more expensive which wasn't a problem when there was no alternative, but now there is.