Trait / inline interaction, force functions to be inlined

This is a followup to Inlined code for operating on scala -> operating on vec - #8 by alice
(but the topic has shifted as the implementation strategy changed thanks to @alice 's strategy)

pub trait MessyT {
    fn do_scalar(lhs: i32, rhs: i32) -> i32 { todo!(); }

    fn do_vec(lhs: Vec<i32>, rhs: Vec<i32>) -> Vec<i32> {
        let n = lhs.len();
        assert!(n == rhs.len());
        let mut ans = Vec::with_capacity(n);

        for i in 0..n {
            ans.push(
                Self::do_scalar(lhs[i], rhs[i]), // Fniii(lhs[i], rhs[i])
                                                 // how do I call this function on these two args?
            )
        }

        ans
    }
}

pub struct ScalarPlus {}
pub struct ScalarMul {}

impl MessyT for ScalarPlus {
    #[inline(always)]
    fn do_scalar(a: i32, b: i32) -> i32 {
        a + b
    }
}

impl MessyT for ScalarMul {
    #[inline(always)]
    fn do_scalar(a: i32, b: i32) -> i32 {
        a * b
    }
}

The goal of this code here is:

  • we have some functions defined on scalars (say plus, mul)

  • we want to auto generate ways to run them on Vecs

  • we want to 100% inline the scalar functions

Question is: is the above guaranteed to inline? Why / why not? If not, how do we fix it?

Thanks!

There is no way to guarantee inlining. Same as any other optimization. However, I would be surprised if those don't get inlined.

If you want to provide a stronger hint to inline a function, you can use #[inline]

1 Like

They are already #[inline(always)], as far as I can see. This is the strongest requirement we can get, right?

Yes, but it's easy to misuse and just increase code bloat. I would stick to #[inline] unless you have some significant performance gains.

1 Like

This seems like an obvious case to inline always, no?

If it's inline always, we execute one instruction (add or mul)

If it's not inlined, we have to execute a function for every add/mul.

This case is so simple that I would be surprised if LLVM didn't already inline it. You can use tools like cargo asm to inspect the assembly to check.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.