3 ms·
There are tricks/an art to Rust's iterator semantics. One example I know of is that when trying to calculate a dot product, you can improve the generated code
by gavinray 6y ago
There are tricks/an art to Rust's iterator semantics.
One example I know of is that when trying to calculate a dot product, you can improve the generated code and elide the bounds check only by a very particular syntax:
// Must slice to equal lengths, and then bounds checks are eliminated!
let len = cmp::min(xs.len(), ys.len());
let mut xs = &xs[..len];
let mut ys = &ys[..len];
let mut s = 0.;
for i in 0..xs.len() {
s += xs[i] * ys[i];
}
https://users.rust-lang.org/t/how-to-zip-two-slices-efficiently/2048/10 https://users.rust-lang.org/t/how-to-zip-two-slices-efficien...
https://github.com/rustsim/nalgebra/blob/b1b18d17ee64d3fd28b67ac581d28af4d20ec817/src/base/blas.rs#L334-L342 https://github.com/rustsim/nalgebra/blob/b1b18d17ee64d3fd28b...
- bad-apple 6y agoDoes it still elide the bounds check if you replace the first three lines with assert_eq!(xs.len(), ys.len()); ?