#[no_mangle]
fn internal(src: &[f32], any_variable: usize) -> Vec<f32> {
assert_eq!(src.len(), any_variable);
let mut dst = Vec::<f32>::with_capacity(src.len());
for i in 0..src.len() {
dst.push(-src[i]);
}
dst
}
I expect that there is notpanic_bounds_check in generated asm, but there is. However, if I remove assert_eq!(src.len(), any_variable); in the code, panic_bounds_check disappears. If I change assert_eq!(src.len(), any_variable); to assert!(src.len() == any_variable);, panic_bounds_check disappears too.
If I switch rustc to <= 1.79.0, no bound checking is generated, too.
I feel it might be a bug in rustc and it's related to assert_eq but I'm not sure. What makes it?
The original example is a bit complex: there are three input arrays and one output array, their sizes are dependent by an expression and it's a two-dimensional loop. Using iterations makes code harder to read and the compiler won't compile it to SIMD version automantically if I add -Ctarget-cpu=core-avx2. And I think it should get optimized code even if indexes are used.
TBH, for simple Copy types like this, I'd suggest using vec![0.0; src.len()] instead.
That way it calls the single alloc_zeroed function to allocate it, and you can just overwrite the fields instead of needing the push, and thus doesn't need to realize that the push can never actually reallocate.
If your real case is more complex, then please include the full thing. Otherwise you're always going to get the "well do the easier thing" replies.