5 ms·
Without undefined behavior, the compiler had to be really conservative and for some platform it is going to be really slow. For example, if you force C to have
by fooker 8y ago
Without undefined behavior, the compiler had to be really conservative and for some platform it is going to be really slow.
For example, if you force C to have defined overflow semantics, the code you'll get for a CPU which does not behave in the same way will use an order of magnitude more instructions for that purpose.
- kurthr 8y agoI am no expert on Rust, but I think targeted optimized algorithms are what the unsafe trait is for. That way you can profile and choose when programming and debugging.
- jake_the_third 8y agoI'd take predictable slow software on some platforms over unpredictable fast software any day.
- lachlan-sneff 8y agoIn rust, integer overflows are defined to be wrapping, which is how it works automatically on essentially every architecture Rust supports.
- adrianN 8y agoRust actually gives you access to all overflow behaviors that you could want. So if you know that your target CPU has saturating overflow you use https://doc.rust-lang.org/std/primitive.i32.html#method.saturating_add https://doc.rust-lang.org/std/primitive.i32.html#method.satu... or wrapping_add, or overflowing_add, or checked_add. The syntax isn't as nice as for normal + though.
- fooker 8y agoIs there a way to specify that I don't care? That's exactly what C undefined behavior is for.