On 01/06/2010 16:13, spir ☣ wrote:
On Tue, 01 Jun 2010 15:00:47 +0100
Martin<f...@mfriebe.de> wrote:
On 01/06/2010 11:23, spir ☣ wrote:
What is the actual benefit of copy-on-write? I ask because of the following
reasoning:
* If a string is just used at several places, for example in output or into
bigger strings, then there is no reason reason to copy it into a new variable.
* If a programmer explicitely assigns an existing string to a new variable, the
intent is precisely copy-semantics, to make them independent for further
changes. If there is no change, there is also no reason for such an assignment.
[...]
No, an assignment, or increase in ref count is not necessarily followed
by a change:
Procedure Foo(a:String);
begin ... end;
Procedure Bar;
var s: string;
begin
s:= SomeString;
Foo(s);
end;
Now a copy of s has to be given to Foo, because Foo *may* changes the
string. But Foo may also *not* change the string => so leave it till later.
For me, parameter passing is just an implicit assignment "param:=s" (with the
variant of var params). The same applies to return values when assigned back in calling
code (*). I would have thus expected s the pointer to be passed. Just checked it is not
the case. More secure, esp since legacy shortstrings do not behave that way.
This example rather backs my pov, I guess, since here copy-on-write does not
apply. But it certainly could, since its application should prevent bad
surprises, no? (unexpected change of original variable)
I don't know all the internals of FPC, but yes to my understanding, your
quote:
"parameter passing is just an implicit assignment"
is absolutely true.
So why do you then say "copy on write" would not apply?
The assignment creates a 2nd reference, and a copy only happens if
inside Foo a change is made to the string.
=> So according to your own words, copy-on-write is exactly what happens.
Shortstrings have nothing to do with it, they are not reference counted.
They are a completely different data type.
Martin
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal