C++ 64 bit int: pass by reference or pass by value

后端 未结 5 1113
悲哀的现实
悲哀的现实 2020-12-31 05:46

This is an efficiency question about 64 bit ints. Assuming I don\'t need to modify the value of a \"int\" parameter, should I pass it by value or reference.

Assu

5条回答
  •  别那么骄傲
    2020-12-31 06:05

    For argument's sake, lets ignore the trivial case of optimisers removing differences. Let's also say you're using Microsoft's Intel 64-bit calling conventions (which do differ from the Linux ABI), then you've got 4 64-bit registers for passing such values before you have to resort to pushing them on the stack. That's clearly better.

    For a 32-bit app, by value and they'd go straight onto the stack. By-reference may instead put a pointer in a register (again, a few such register uses are allowed before resorting to the stack). We can this in some output from g++ -O3 -S, calling f1(99) by value and f2(101) by const reference:

    void f1(int64_t);
    void f2(const int64_t&);
    
    int main()
    {
        f1(99);
        f2(101);
    }
    
    ...
    
        pushl   0
        pushl   $99
        call    _Z2f1x    // by value - pushed two halves to stack
    
        leal    -8(%ebp), %eax
        movl    %eax, (%esp)
        movl    $101, -8(%ebp)
        movl    $0, -4(%ebp)
        call    _Z2f2RKx   // by const& - ugly isn't it!?!
    

    The called function would then have to retrieve before first usage (if any). The called function's free to cache the values read in registers, so that's only needed once. With the stack approach, the value can be reread at will, so the register need not be reserved for that value. With the pointer approach, either the pointer or 64-bit value may need to be saved somewhere more predictable (e.g. pushed, or another less useful register) should that register need to be freed up momentarily for some other work, but the 64-bit int parameter be needed again later. All up, it's hard to guess which is faster - may be CPU/register-usage/optimiser/etc dependent, and it's not worth trying.

    A node to pst's advice...

    "efficiency" :( KISS. pass it how you pass every other bloody integer. - pst

    ...though, sometimes you apply KISS to template parameters and make them all const T& even though some may fit in registers....

提交回复
热议问题