3 ms·
I don't understand their logic. If "for X in" gets rid of many cases where overflow checking might hurt performance, what is the argument for disabling it (by d
by Someone 11y ago
I don't understand their logic. If "for X in" gets rid of many cases where overflow checking might hurt performance, what is the argument for disabling it (by default) in release builds, in a language that aims to be safe? Do they think it is that important to win benchmarks run with default compiler flags?
Also, I expect Rust will still do bounds checking, and would guess many of the cases where the release build removes checks for overflow will now require some extra out of bounds checks: those for negative indices.
- Filligree 11y agoThe article talks about three different levels of optimization. - Detect and trap overflows. - Assume that two's complement math is a thing, and overflow can lead to additions producing negative values. - Assume that overflow never happens, and optimize on this basis. Rust does #1 in debug mode, #2 in release mode except when explicitly told not to. C compilers do #3. #3 is what leads to developers screaming at their compilers.
- dbaupp 11y agoArithmetic still happens a lot in programs, and especially so in tight loops (most tight loops will be filled with arithmetic, rather than anything else), even if it doesn't happen as the loop counters. You are correct that Rust does bounds checks in release mode, but fortunately there's no extra bounds checks needed: - all indexing is done with unsigned values, and, - even if signed values were used, the '< 0' comparison is still not necessary because one can first cast to unsigned (a noop) and then do a single '< length' comparison: negative values will be huge, at least as large as MEMORY_SPACE/2, which is in fact the limit on array lengths in Rust[1]. [1]: http://doc.rust-lang.org/stable/nomicon/vec-alloc.html http://doc.rust-lang.org/stable/nomicon/vec-alloc.html search for isize::MAX.
- pcwalton 11y ago> in a language that aims to be safe There are different levels of safety. Rust does not compromise on memory safety. Rust does compromise on integer overflow safety. This is not to say that memory safety is all that matters: rather it's to say that the line has to be drawn somewhere, and memory safety is where it was drawn. > I expect Rust will still do bounds checking, and would guess many of the cases where the release build removes checks for overflow will now require some extra out of bounds checks: those for negative indices. No, for two reasons: (1) you can only index arrays by unsigned values in Rust; (2) twos complement means that at the CPU level you can check for both negative indices and positive out of bounds indices by simply using one unsigned compare instruction.