0

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:

  1. 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?

  2. 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?

Community
  • 1
  • 1
Muckeypuck
  • 549
  • 2
  • 14
  • 3
    In your example 'string y = x;' y is reference to the same string instance as x. There is only one instance of the string 'test' both x and y point to it. – Chris Taylor Jul 29 '16 at 03:13
  • i dont think thats true because if i were to change x (x = "test1") y would still be "test" – Muckeypuck Jul 29 '16 at 03:17
  • 3
    x = "test1" make x point to a new string, y still points to the original string, but y does not have its own copy of the entire string. Since strings are immutable you are not changing the content of the string x is pointing to, you are changing where x points ie. to the new string instance. – Chris Taylor Jul 29 '16 at 03:19
  • 3
    this is the best answer if you want a deep understanding http://stackoverflow.com/a/2365305/4827151 – Anonymous Duck Jul 29 '16 at 03:24
  • 3
    "would still only be a copy of x and not point to x's location in memory" - No, that's wrong - it **does point to the same location in memory**. – Enigmativity Jul 29 '16 at 03:26
  • I will read Sherlocks link and maybe it will clear it up for me but everything ive always read says they do not point to the same place, x is copied to y see the accepted answer here: http://stackoverflow.com/questions/6236569/string-assignment-in-c-sharp . When you assign a variable of type string to another of type string, the value is copied. – Muckeypuck Jul 29 '16 at 03:28
  • 3
    @Muckeypuck - No, the reference is copied, not the string itself. – Enigmativity Jul 29 '16 at 03:29
  • 1
    x and y hold a pointer to a memory address. When you code x = y, you are copying the pointer value from one variable (x) to the other variable (y). so in that sense y is a copy of x, both pointing to the same memory address containing the string content. So yes x is a copy of y, but not a copy of the string data that they point to. I hope that helps... – Chris Taylor Jul 29 '16 at 03:31
  • @Muckeypuck - Where in the link to the accepted answer you provided does it say that a string is copied? – Enigmativity Jul 29 '16 at 03:31
  • See? I do not understand what language feature makes a copy of valueA when I assign it to valueB. It doesn't copy the string, it copies the reference. strings are reference type. This means that variables of type strings are storage locations whose values are references. In this case, their values are references to instances of string. When you assign a variable of type string to another of type string, the value is copied. In this case, the value is a reference and it is copied by the assignment. This is true for any reference type, not just string or only immutable reference types. – Muckeypuck Jul 29 '16 at 03:33
  • ok that explains exactly what you guys are saying i got it thanks i just didnt read past that where he says the value is the reference – Muckeypuck Jul 29 '16 at 03:34
  • Is this a bad question? it seems like my questions always get downvoted with no explanation as to why. i want to build a good rep and be a valued member of the community but i can never tell how to improve my contributions without feedback – Muckeypuck Jul 29 '16 at 11:59

0 Answers0