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.
As a consequence, s2:=s1 will nearly always be followed by modification of
either string, which will result on copy anyway, according to copy-on-write
semantics. So, the initial gain at assignment time is soon lost. While the cost
I imagine in terms of type complexity remains (every builtin modification
method must ensure copying; no user-defined modification method should be
possible without using builtin ones -- else copy-on-write is lost and
consequences undefined).
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.
Example 2:
Procedure Bar;
var a, b: string;
begin
a:= SomeString;
b:=a;
if SomeCondition then
b:=b+"...";
if SomeOtherCondition then
b:=b+"???";
end;
So b may never be changed at all.
Martin
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal