Dick Hendrickson <[log in to unmask]> wrote:
...
>Van Snyder wrote:
>>
>[snip, interesting question about what real compilers actually do]
>
>> Presumably, the compiler
>> would be expected to "deep copy" instead of "shallow copy" an anonymous
>> temporary variable.
>>
>
>I've seen something like this before and never understood it. Could
>someone give a brief explanation of "deep" and "shallow" copies. And
>maybe explain why one could or couldn't be used in some common cases.
...
This usually arises with respect to pointers, but in this case
Van Snyder is referring to an internal implementation of
call-by-reference semantics.
Deep copy is when you are passed a pointer to an object and you
create spece for the object itself and copy it all. Shallow copy is
when you just make a copy of the pointer and use that. The only
time there's a semantic difference between these strategies is when
one or both of the copies are modified later in the program. With
shallow copies, a modification to one actually modifies both.
Pragmatically, there's always a difference in speed.
Van snyder's case is a bit unusual in that he's trying to get an idea
of what's the most common technique for the compiler to use
internally when both seem semantically correct. In this case, he
wants (for speed) to have a pointer to the variable on the left
of an assigment used instead of a temporary location that will
then have to be copied out. But this has pitfalls:
x = f(x)
If the function has side-effects on its argument, this is an illegal
call. But, even without such side-effects, if the compiler were to
pass the variable on the left of the assignment to the function
(by reference) for the function to build its result directly into,
you'd have a situation just like:
call fs(x,x)
Where fs() is f() made into a subroutine and using its first argument
as a return value. This call would violate the rules of Fortran. In
the same way, the optimization Van Snyder wants would be a
problem in this case.
At least, that's how I read this situation. I probably misremember
what Van wants or something. But I believe that the only safe
action the compiler can take is to make a temporary into which to
build the function, and then deep copy the result in the assignment.
It could do the other only with special extra analysis, which would
be a lot of work for (generally) little benefit.
--
J. Giles
Ps. By the way, is there an archive I can go to in order to check
what was said earlier in this thread? I *think* the above situation
was the one that Van Snyder was interested in. But I didn't save
his article. Is this mailing list archived somewhere?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|