I looked at many posts and I did not found a satisfactory answer.
Lets say I have this simple struct
struct Point {
int x,y;
};
void work(const Point& p);
void work2(Point p);
I am questioning now whether it is faster to pass by value or reference IF teh function cannot be inlined by the compiler as it is like in another TU or just too big.
So I think and I am not sure it would be faster to pass by copy since a const-reference can be stripped by const cast inside the function and the value could change
int main() {
Piont p{2,3};
work(p);
return p.y; // has to reload it since It could have changed! but if it passed by copy it couldnt have!
}
I know for big structs with non trivial copy consturcotrs should be always passed by const ref
template<typename T>
using pass_arg = typename std::conditional<(std::is_trivially_copyable<T>::value && (sizeof(T) <= sizeof(void*)*2)),T, const T&>::type;
it will then auto-determine the way to pass arguements but that is a convulated way though.
in ASM is it faster to load from a register or loading from a pionter if the pionter is in a register?
mov eax ,DWORD PTR[REG]
vs
mov eax ,REG
which is faster?