News for hackers. Stuff that matters.
Re:The humble for loop in Rust by ridiculous_fish on Thursday December 12, 2024 @05:14PM (#42403966)
SIMD is true, but the original guess is correct, and that effect is bigger!

using_map is faster because it's not allocating: it's re-using the input array. That is, it is operating on the input `v` value in place, equivalent to this:

    pub fn using_map(mut v: Vec<i32>) -> Vec<i32> {
        v.iter_mut().for_each(|c| *c += 1);
        v
    }
This is a particularly fancy optimization that Rust can perform.
Re:The humble for loop in Rust by arllk on Thursday December 12, 2024 @08:03PM (#42405044)
Even when passing the array as a borrow instead of a clone[1], map still auto-vectorizes and performs the new allocation in one go, avoiding the bounds check and possible calls to grow_one

[1] https://godbolt.org/z/K9z6PvdYh

Re:The humble for loop in Rust by skitter on Thursday December 12, 2024 @06:27PM (#42404478)
This can even turn the following code into a no-op:

    vec_of_u32.into_iter().map(f32::from_bits).collect()