Should I trust the compiler to replace &T with T in function arguments when T is small enough? (for static dispatch)

Like if I use this example function (not a real life scenario, just an example) for f64 or f32 will it still take pointers to f64 even in the release build?

add<N>(lhs: &N, rhs: &N) -> N where N: Add<&N, Output=N>;

I hope this explanation of my question was sufficient, but for anyone curious I'm thinking of removing this associated type which defined the type to be used in function arguments. I want to remove it because it historically has been a source of very strange lifetime errors and weird workarounds (this one is for simulating covariance). Since f64 is what I think will be used often with my library, I won't undo this solution if I can't trust the compiler to replace &f64 with f64.

1 Like

LLVM does have an optimization pass which promotes by-reference to by-value passing. It also inlines function calls aggressively, which eliminates the overhead of passing by reference as a side effect. To be really sure you'd have to look at generated assembly.

6 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.