Consider the case that the right-hand-side of an assignment consists
only of a reference to a function (perhaps by way of a defined operation),
with the result of the function having a derived type.
Presumably, most compilers pass into the function a reference for a place
to put the result, in the same way that arguments are passed, rather than
having the function create its own place to put the result, and then
copying it.
Question:
Is it more common to create an anonymous temporary variable, which is then
passed to the function as a place to store its result, and then copied
to the left-hand-side, or is it more common for the left-hand-side to
be passed (by reference) to the function as the place to store its result?
The former solution is easy to implement but could have performance
problems. The latter solution requires some more sophistication in the
compiler's optimizer, but is more efficient in almost every case (the
exception being when the derived type has only one component, so the
result of the function could in principle be returned in a register).
The difference will become even more pronounced as more vendors implement
the technical report on allocatable components: Presumably, the compiler
would be expected to "deep copy" instead of "shallow copy" an anonymous
temporary variable.
The answer affects my development strategy: Should I write
call copy ( x, y ) ! Copy y to x
or
x = copy(y) ! Create a copy of y and assign it to x
The former avoids the question, but is not as clear as the latter (does
it do x=y or y=x?), and therefore has a potentially higher maintenance
cost.
One might be tempted to answer "Use defined assignment," but in my
application, I occasionally need intrinsic assignment as well, and once
one has a defined assignment accessible in a scoping unit, intrinsic
assignment no longer exists for that type.
Best regards,
Van Snyder
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|