Looking for clarification for some points of Why .NET String is immutable?
interview question: Why are strings immutable
My answer: so they can be treated more like objects then arrays of characters (wrong i should have said value types) but even that would of been a partial answer. see link above
Interviewer: its to stop them from filling up with googlygoop and returning junk plus it improves performance.
Now the way he explained googlygoop made it appear like he was talking about some kind of string metadata and that the object (a mutable string tracking this metadata hurt performance. now what confuses me:
the part where changing a mutable string would return junk. Stringbuilders are mutable, we change them and never get junk back unless we put junk in. What he may be referring to is a pass by reference into a function and that may return unexpected results. Is there some other way that could make sense?
i thought stringbuilders were performance improvements because we werent destroying them and reconstructing them with every change which in return makes less work for the gc.
Furthermore i am a little confused by the stack overflow answer:
Memory-saving optimisations are also possible. Interning and atomising being the most obvious examples, though we can do other versions of the same principle. I once produced a memory saving of about half a GB by comparing immutable objects and replacing references to duplicates so that they all pointed to the same instance (time-consuming, but a minute's extra start-up to save a massive amount of memory was a performance win in the case in question). With mutable objects that can't be done.
this seems backwards to me since:
A) if i declare
string x = "test";
string y = x;
i am producing a copy of x and storing it in y. therefore, i basically just doubled the memory i used whereas if y pointed to x's memory location, i am just using the memory to create the pointer to "test" as opposed to the whole string. So the only way i see the above making sense is if i did something like
string x = "1"
string y = "2"
if dbValue["col'] == x then
treenode[i].text = x
else
treenode[i].text = y
but that doesn't really make sense because treenode[i].text would still only be a copy of x and not point to x's location in memory. However if mutable objects are reference types, we could create a reference to the same string in memory as intended and save on memory.
In fact, i don't see how the op was able to save half a GB using immutable strings at all.
Update
It occurs to me that the string metadata he was referring to could impact performance for example having to copy string[i] = string2[i] for 1000's of characters plus having to track info on the array (indexes, counts etc) could impact performance. is that it possibly?
I assume i am just not understanding something so what is it i am missing?