MisconceptionsSome people have confused copy-on-write with issues of call-by-value versus call-by-reference and/or the use of upvar. This is wrong. Tcl command arguments are always passed by value at the Tcl script level and by reference at the C implementation level. Sometimes the value might be the name of a variable but this is not the same thing as passing by reference in C (and even if it were, it would still have nothing to do with copy-on-write).The only time that copy-on-write is visible at the script level is when an extension has been coded wrongly and copy-on-write hasn't happened when it should have. Also you can see it by using the [representation] command, which does not conform to Tcl value semantics.Should anyone still be confused by values, references, variables and names I refer you to Lewis Carroll's "Through the Looking Glass" :
|"The name of the song is called 'Haddocks' Eyes.'"|
|"Oh, that's the name of the song, is it?" Alice said, trying to feel interested.|
|"No, you don't understand," the Knight said, looking a little vexed. "That's what the name is called. The name really is 'The Aged Aged Man.'"|
|"Then I ought to have said 'That's what the song is called'?" Alice corrected herself.|
|"No, you oughtn't: that's quite another thing! The song is called 'Ways and Means': but that's only what it's called, you know!"|
|"Well, what is the song, then?" said Alice, who was by this time completely bewildered.|
|"I was coming to that," the Knight said. "The song really is 'A-sitting On A Gate': and the tune's my own invention."|
ThreadsC/C++ programmers discovering copy-on-write initially tend to see it as a guaranteed performance improvement and beginners' C++ books are full of examples of string classes using it. After a while they will use their copy-on-write implementations in a multi-threaded application and then things start to go wrong. The problem is that checking the reference count and copying needs to be performed in a thread safe manner which typically requires the use of mutexes. Since the mutex must be locked for every access, not just ones that turn out to need a copy, the performance tends to actually be worse than for a straightforward implementation that always copies. Threaded Tcl avoids this problem by never sharing values between threads.
NickHounsome - 2009-07-08 03:30:04None of the Tcl examples here have anything to do with copy-on-write since copy-on-write only has meaning in the C API.