3 ms·
I find this decision frustrating. They made it clear by making it trap in debug that this is a "bug" but denied the optimization benefits of calling it undefine
by kevincox 6y ago
I find this decision frustrating. They made it clear by making it trap in debug that this is a "bug" but denied the optimization benefits of calling it undefined behaviour on release.
I'm assuming the rationalization is "wrapping is better than undefined behaviour" but I have not seen any evidence that the average program is actually more likely to behave correctly in the face of wrapping.
The combinations that make the most sense to me are:
1. debug: trap, release: trap. Bad performance, correct.
2. debug: wrap, release: wrap. Good performance
3. debug: trap, release: undefined. Great performance.
I guess my point is make up your mind. Is "overflow" allowed and wrapping or a bug?
- eddyb 6y ago> but I have not seen any evidence that the average program is actually more likely to behave correctly in the face of wrapping. Rust takes a simple stance of "safe code should be UB-free". Yes, you may have logic bugs with wrapping, but you can't cause memory unsafety with it in, because things like array/slice bound checks aren't ever disabled. Also, C relies on undefined signed overflow for things like "`for` loops incorrectly using `int` instead of `size_t` because it's easier to type", which doesn't really apply to Rust (which require `usize`, the `size_t` equivalent, for indexing, and has pointer-range-based iterators for slices), so I doubt UB overflow in Rust would help performance much. It would be trivial to change `rustc_codegen_llvm` to set LLVM's `nsw`/`nuw` (in order to make signed/unsigned overflow UB), if you want to prove it improves performance somewhere.