I tend to use const reference
parameters when calling functions assuming this would be efficient since copies of the same wouldn't not be made. Accidentally I changed the function parameter in a function that previously had a const reference
parameter to const
now, and I observed that the code size is reduced upon compilation.
To check this, I looked into the assembly of a MWE:
#include <cstdint>
void foo(const int n)
{
int a = n + 1;
}
void foo(const int& n)
{
int a = n + 1;
}
On line 19 of the generated assembly code, I see an additional step ldr r3, [r3]
in the case of void foo(const int& n)
when compared to void foo(const int n)
. I assume this is the reference to variable n
(I could be wrong, please forgive me).
So, my question is, why is the code larger when passing via reference and also what method is efficient?