Unless you have measured performance benefits, please undo these changes: it is agreed[1][2][3] that there is no benefit in passing built in types (such as int, float, double) by reference instead of by value. The code merely becomes uglier to read and potentially more annoying to modify (what if we'd like to be able to modify arg inside the function as a temporary value?)
Only pass by reference objects (such as std::string, std::vector<float>, a_big_struct, etc.) Or a better thought, only pass by reference things whose size in bytes exceeds the size of a pointer: on a 32-bit machine a pointer will be 4 bytes, on a 64-bit machine it will be 8 bytes; a float is typically 4 bytes on both 32-bit and 64-bit [4].
Hi! Sorry for the delay in reverting my mistake (@Andrei thanks for fixing it!) and for the mistake itself. I was absolutely sure it was better to pass by reference any type, but as you pointed, I was wrong.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Emanuele, regarding your commit [r949]: in some places you have passed
float
by const reference instead of value, as in:Unless you have measured performance benefits, please undo these changes: it is agreed[1][2][3] that there is no benefit in passing built in types (such as
int
,float
,double
) by reference instead of by value. The code merely becomes uglier to read and potentially more annoying to modify (what if we'd like to be able to modifyarg
inside the function as a temporary value?)Only pass by reference objects (such as
std::string
,std::vector<float>
,a_big_struct
, etc.) Or a better thought, only pass by reference things whose size in bytes exceeds the size of a pointer: on a 32-bit machine a pointer will be 4 bytes, on a 64-bit machine it will be 8 bytes; afloat
is typically 4 bytes on both 32-bit and 64-bit [4].[1] https://stackoverflow.com/q/4112914
[2] https://scotchi.net/cpp-pitfalls/#references
[3] https://stackoverflow.com/a/898844
[4] https://stackoverflow.com/a/25524605
EDIT: added an extra link.
Related
Commit: [r949]
Last edit: Andrei 2019-04-13
I have reverted these changes in [r950] considering 6 days without a response.
Related
Commit: [r950]
Wouldn't it have been easier to just fix the data types? Or would that have messed things up?
It was easier to "fix" the commit by reverting it entirely.
Hi! Sorry for the delay in reverting my mistake (@Andrei thanks for fixing it!) and for the mistake itself. I was absolutely sure it was better to pass by reference any type, but as you pointed, I was wrong.
@Emanuele: no worries, every C++ programmer makes this mistake at least once in life, I did too.