[cfe-dev] Inlined function that pass POD by reference

mats petersson via cfe-dev cfe-dev at lists.llvm.org
Wed Jan 11 03:41:45 PST 2017


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<typename T> 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...

--
Mats

On 11 January 2017 at 09:49, Francois Fayard via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> 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
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170111/7f15887b/attachment.html>


More information about the cfe-dev mailing list