would ever passing a primitive to functions by reference be more effecient ?
In a scenario that a caller function needs to pass one of its local variables of a primitive type to a callee and doesn't necessarily need to pass it by reference to track that variable later after callee is done, I know that it'd be said to pass it by value and that makes total sense for primitive types with a size equal to or smaller than size of pointer; but I'm curious that from an assembly point(performance-wise or space-wise), wouldn't be there a situation when passing a variable of a primitive type with a size bigger than pointers like long double on my platform which has a size of 8 bytes and is bigger than pointers that have a size of 4 bytes; by reference would be more efficient? like an imagined situation where pointer can be loaded directly into some register by caller but the primitive itself not and thus no need to load the pointer from callee stack frame to some register by callee and there's 8 more free bytes of stack memory in the end comparing to pass by value where there'd be 8 more used bytes of stack memory.
If in this specific case, passing by reference might ever be more efficient, how can we know to pass by reference or value?
Re: would ever passing a primitive to functions by reference be more effecient ?
Design choices like this always hang off the application design requirements. If it's needed to pass by value then pass by value. If by reference then by reference. If pass by value is inefficient, first profile the code and find out where it's not performing to requirements, then work on the most inefficient areas first. You may find this particular "inefficiency" doesn't actually cause any significant problems.
If you're getting stack overflows and the stack can't be extended, then it's appropriate to look at such issues, but again this is requirements driven.
But to address your question directly, yes of course it's more efficient to pass by reference than by value when the value is bigger than a reference, for the simple reason that you're copying fewer bytes. The *important* question is whether that inefficiency is significant and is worth spending time on.
|All times are GMT +5.5. The time now is 04:01.|