Hacker News new | past | comments | ask | show | jobs | submit login

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.



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


This can even turn the following code into a no-op:

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




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: