May 11, 2004
hellcatv@hotmail.com wrote:

> you have some good points
> but what is a good way for a new person (or someone reading someone else's
> code) to know if the function is exhibiting copy on write

You should always assume that it may do it, unless it is explicitely documented as dowing something "in place". You should be careful to expect that a routine guarantees to do a copy. Like the tolower example (as I understand from Ben's post): it will make a copy if there were any uppercase letters in the original. Otherwise, there is no reason to do so, and it will just return a reference to the original string.

If you want to make sure to have a unique copy, you have to call .dup yourself. I hope, the compiler is intelligent enough to detect and drop unnecessary .dups

> I also noticed that
> char [] blah="1234567";
> char [] bleh=blah;
> bleh~="";
> bleh[0]='A';
> blah[0] is still '1'

> perhaps ~= also guarantees copy-on-write semantics? :-) that would make phobos a quite consistent library then

The implementation is likely to do so, but currently, the language spec does not guarantee it. Actually, in your example, bleh~="" might be optimized away completely. (At least, that's how I understand the specs.)


May 11, 2004
"Norbert Nemec" <Norbert.Nemec@gmx.de> wrote in message news:c7psqp$2f7r$1@digitaldaemon.com...
> You should always assume that it may do it, unless it is explicitely documented as dowing something "in place". You should be careful to expect that a routine guarantees to do a copy. Like the tolower example (as I understand from Ben's post): it will make a copy if there were any uppercase letters in the original. Otherwise, there is no reason to do so, and it will just return a reference to the original string.

COW coupled with gc enables D string processing programs to smoke C++ std.string ones in the performance department. The combination of the two enables one to do things like mix slices, static data, and gc'd data without worrying about which is which. C++ std.string has to worry, and the implementations I've looked at resolve the problem by always copying.


1 2
Next ›   Last »