3 ms·
It's not a priority for Rust to hoist subscript checks in loops, because Rust uses iterators pervasively and iterators are guaranteed to check the bounds of the
by kibwen 12y ago
It's not a priority for Rust to hoist subscript checks in loops, because Rust uses iterators pervasively and iterators are guaranteed to check the bounds of the array no more than once.
- Animats 12y agoWrite a matrix multiply. Or a linear equation solver.
- pcwalton 12y agoThe bounds checks for matrix multiplication over fixed length arrays are completely optimized out by LLVM, and LLVM will also optimize the bounds checks for non-fixed-length arrays in many cases. The slice length is an SSA value, so it's subject to constant propagation and SCCP.
- dbaupp 12y agoLLVM (used by rustc) is perfectly capable of hoisting and eliminating subscript checks, although I believe rustc doesn't necessarily drive LLVM in the optimal way, yet: https://github.com/rust-lang/rust/issues/22987 https://github.com/rust-lang/rust/issues/22987 E.g. this has no bounds checks, and is in fact vectorised: pub fn bad_sum(x: &[i32]) -> i32 { let x = x; // * let mut sum = 0; for i in 0..x.len() { sum += x[i] } sum } The line marked * is needed because of https://github.com/rust-lang/rust/issues/22924 https://github.com/rust-lang/rust/issues/22924 which is caused by https://llvm.org/bugs/show_bug.cgi?id=22786 https://llvm.org/bugs/show_bug.cgi?id=22786 .