Inlined function that pass POD by reference

Hi,

Because of the emphasis on generic programming, the C++ standard library has many weird signatures such as:

const int& std::min(const int& a, const int& b) (1)

where one would expect a:

int std::min(int a, int b) (2)

I thought that this would not matter from a performance point of view as those functions are inlined anyway. And it turns out that when you replace a signature such as (2) into (1) where the function is inlined, the assembly does not change at the call site: both are inlined properly.

But I came to realize that some side effects are cascaded to other parts of the code, and codes that use (1) end up being slower than code using (2), wether the compiler is clang, gcc or intel compiler (even though the loss of performance differs from compiler to compiler).

Is there any fundamental reason for this? Would you recommend avoiding signatures such as (1) which end up being used a lot when min is template-defined?

Best regards,
Francois

It would be good to have an example of code where this is actually affecting things. In general, clang (or rather LLVM) is good at “unreferencing” references that don’t need to be references when inlining, but of course, there are probably some cases where this does not hold true.

[I expect that the reason for things like const int& std::min(const int &a, const int& b) is because the function is actualy declared as "template const T& std::min(const T& a, const T& b), rather than as an int` function directly.

I certainly would expect something like:

int main()
{

int a, b;

std::cin >> a >> b;

std::cout << std::min(a, b) << std::endl;
}

to produce identical code whether std::min(int a, int b) or the const reference variant. But of course, with more complex constructs, the compiler may not realize what is going on and be able to remove the references. Making the compiler smarter in those cases is always an option…

LLVM has an argument promotion pass that does this exact transform, but it only works on internal functions.

Unfortunately, functions with vague linkage all need to have the same prototype across all TUs, so we can’t do this transform to a function without internalizing it. We should only internalize small functions, which are typically good candidates for inlining, which makes the exercise kind of pointless.

In theory, C++ programmers could create and use a type trait that indicates that pass-by-value is more efficient for a type than pass-by-const-reference. However, that would probably make really complicated code, so I wouldn’t suggest it.

Thanks,

I am quite sure I had this problem but now, I just can’t seem to reproduce it as I don’t have the exact code anymore. The problem appeared in a much more complicated code and I can’t make it show this difference again. I’ll try hard again and come back when I have a code that show this difference.

François Fayard