Surprising Auto-Vectorization/Optimization Behavior

EDIT: It seems there is an optimizer regression in current nightlies. Compare the ASM for this playground example, which I wrote for a previous topic on a similar matter, in beta and nightly builds.

I would also be curious about this one. I don't understand why, for example, rustc doesn't manage to extract bounds checks out of vector index loops automatically. I guess that for some reason, the optimizer doesn't manage to prove that a vector's length will not change during iteration (which would be why using a constant-length slice improves performance), but why doesn't it, and can it be changed?

The first rule of code optimization is to have no observable effect on the code's behaviour. If your code may panic at iteration N of the loop, then the optimizer is not allowed to vectorize because it could start the processing of iteration N+1 before the panic, changing the behaviour of the program with respect to the scalar version.

1 Like