Is all this Invarient **** er... stuff, premature optimisation?

Walter Bright newshound1 at digitalmars.com
Mon Apr 28 23:21:38 PDT 2008


Me Here wrote:
> If a piece of code needs to know that the subject of a reference
> (string, int array, hash, whatever), isn't going to change, it is
> (and should be) *its responsibility* to ensure that--by taking a
> private copy.

There are two ways of doing it. One is COW, where those who make the 
change make the copy. The other way doesn't have a name, but it's making 
a copy "just in case" someone else might mutate it. I think you're 
proposing the latter. Invariant strings is a way of enforcing COW, 
rather than relying on documentation.

There's no doubt you can make JIC work successfully. I've used it myself 
for decades. But I always find myself expending effort trying to 
optimize away those copies, and so find it more productive to go the 
other way and use COW.

While I am comfortable using COW with mutable strings, the many many 
discussions of it in this forum made it clear that most would like to 
have some compiler help with it. Invariant strings fit the bill nicely.



More information about the Digitalmars-d mailing list