3 ms·
I do not believe that in a programming language there is any need to specify if a parameter is passed by value or by reference, and requiring the programmer to
by adrian_b 5y ago
I do not believe that in a programming language there is any need to specify if a parameter is passed by value or by reference, and requiring the programmer to choose this (like in Pascal, C and derived languages) is a misfeature.
The right way to specify parameters appeared in the DoD IRONMAN specifications, which were first implemented by the Ada language.
There are 3 kinds of parameters, in, out and in-out, and this is what the programmer must specify.
Any of them may be passed by either value or reference, this is transparent for the programmer. E.g. when an in-out parameter is passed by value, it is copied to the parameter location (which might be a CPU register or registers, or a stack location) before calling the function and then it is copied back after return. For "in" by value there is only 1 copying, before function call, and for "out" by value there is only 1 copying, after function return. (In many cases the copyings can in fact be no-ops, e.g. for register variables.)
If implemented correctly, there is no observable difference depending on how the arguments are passed, except in timing.
The decision to pass by value or by reference must be taken by the compiler, because it depends strongly on the target CPU, i.e. on the relative costs of copying and indirect addressing. Even for 2 Intel/AMD CPUs from different generations the optimal decision can be different (i.e. the length threshold for the size of a structure/class to be passed by value or by reference can be different).
For external functions, how to pass the parameters should be determined by the ABI for that processor family, also taking into account the typical relative costs of copying and indirect addressing.
There might be some very seldom cases when the programmer could increase the speed by choosing how to pass the parameters instead of allowing the compiler or ABI to decide, but if indeed such a decision could have a measurable effect on performance, it is likely that inlining the function or rewriting that function in assembly language would have an even larger effect on performance.
Even in C/C++, the apparent control of the programmer on how the parameters are passed is just an illusion, because the performance is mainly determined by the ABI, which decides, depending on parameter size, whether the parameters specified as passed by value shall be passed on the stack or in registers and whether the return value shall be passed by value or by reference.
Therefore if a parameter is considered by the compiler as too large, specifying that it should be passed by value will have no effect on performance, because it will still be passed as a stack location and addressed indirectly.
The lack of 3-way parameter specification has caused a lot of unnecessary complications in C++, like the distinction between constructors and any other functions (which is needed because in C/C++ output parameters are assumed to be in-out, they cannot be specified as out) and the need for both move & copy constructors/assignments and also the many other tricks that are frequently needed to achieve move semantics.
- pjc50 5y ago> 3 kinds of parameters, in, out and in-out .. or, in C#, (no specifier), "out", and "ref". However there's still a difference between plain types (copied) and objects (mutable).
- pjmlp 5y agoYou are missing "in", "ref structs", "readonly ref structs" and the Span variants.
- gpderetta 5y agoIn C++ return values have exactly the same semantics as your 'out' parameters, so I'm not sure what they would add. Also pass-by-reference vs in-out has very visible semantic differences on the face of aliasing, exceptions and abnormal termination (when referring to shared memory).
- adrian_b 5y agoAfter 2011, they kind-of have it, but not previously. For non-const parameters the compiler normally assumes that they are in-out. Whether a parameter is out or in-out is very important for types with destructors. Before calling a function, an "out" parameter is raw memory, on which a destructor should never be called. After the function returns, the "out" parameter contains a new object, possibly with allocated resources. Before calling a function, an "in-out" parameter contains an existing object. If inside the function a new object is assigned to the parameter, the destructor must be invoked first, usually implicitly, inside an "operator=". Before C++ 2011, most C++ compilers treated the return value as an in-out parameter, like every other non-const parameter, so they allocated a temporary and constructed a default object in the temporary, before calling the function. One of the major changes in C++ 2011 was a collection of extra syntax features and library tricks to achieve the so-called move semantics, i.e. to avoid unnecessary constructions and destructions of temporaries, all of which had their origin in the lack of distinction between out and in-out parameters. Aliasing of "out" or "in-out" function parameters is something that does not have defined behavior in any programming language. When 2 aliased in-out parameters are passed by reference, the value after the function returns is unpredictable, because it depends on the order of interleaving of the changes to the 2 parameters inside the function. When 2 aliased in-out parameters are passed by value, the value after the function returns is unpredictable, because the function receives 2 copies, whose values are correct immediately before return, but the final value seen after return depends on the order in which the 2 values are copied over the original aliased location, which is undefined. Similar effects exist for exceptions and abnormal termination, the details may differ between pass-by-value and pass-by-reference, but the result is always undefined, so it does not matter. While aliasing of "out" and "in-out" parameters cannot have defined behavior in any language, regardless of the method used for parameter passing, aliasing of "in" parameters is always OK, and here again it does not matter whether they are passed by value (when multiple copies are passed) or by reference (when multiple references to a single immutable object are passed), the behavior is indistinguishable in this case.